将jQuery包含在Symfony2中 [英] Include jQuery into Symfony2

查看:70
本文介绍了将jQuery包含在Symfony2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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 under src/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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆