问题描述
我是Symfony2框架的新手,但并不完全了解如何使用javascript以及如何以最佳方式包含它们.
I am new in Symfony2 framework and not fully understand how to work with javascripts and how to include theirs in the best way.
我需要:在每个页面中包含jQuery脚本.
What I need: to include jQuery script to each page.
我所拥有的:我有这样的通用布局:
What I have: I have common layout like this:
<!DOCTYPE html>
<html>
<head>
{% block javascripts %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
应将jquery.js放在何处. web/bundles/jquery 怎么样?还是有一些特殊的官方jquery捆绑包?我应该使用asset()以及如何使用吗?
Where jquery.js should be placed. What about web/bundles/jquery? Or there are some special official jquery bundles? Should I use asset() and how?
推荐答案
假设您的jquery.min.js位于src/Acme/FooBundle/Resources/public/js/
Assuming your jquery.min.js is placed undersrc/Acme/FooBundle/Resources/public/js/
您可以使用
<script type="text/javascript" src="{{ asset('bundles/acmefoo/js/jquery.min.js') }}"></script>
或
{% javascripts
'@AcmeFooBundle/Resources/public/js/jquery.min.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
进入您的树枝模板.
请确保您随后安装了资产或运行此命令
Make sure you installed the assets afterwards or run this command
php app/console assets:install web --symlink
这篇关于将jQuery包含在Symfony2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!