Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include jQuery into Symfony2

I am new in Symfony2 framework and not fully understand how to work with javascripts and how to include theirs in the best way.

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>

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?

like image 968
Alex Pliutau Avatar asked Aug 17 '12 14:08

Alex Pliutau


1 Answers

Assuming your jquery.min.js is placed under src/Acme/FooBundle/Resources/public/js/

You can use either

<script type="text/javascript" src="{{ asset('bundles/acmefoo/js/jquery.min.js') }}"></script>

or

{% javascripts
    '@AcmeFooBundle/Resources/public/js/jquery.min.js'
%}
    <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

Into your twig template.

Make sure you installed the assets afterwards or run this command

php app/console assets:install web --symlink
like image 86
Touki Avatar answered Oct 19 '22 03:10

Touki