3

I am trying to understand how do you use jquery or other scripts in Symfony2. I read the Assets documentation, however that didnt help me that much.

So I have a base.html.twig file in which I add the jquery library and my script file that contains a simple script.

I add it like this:

{% block javascripts %}
    {# Le Javascript #}         
    {% javascripts 
        'bundles/mpFrontend/assets/js/jquery-1.11.2.min.js'         
        'Mp/ShopBundle/Resources/js/scripts.js'   
    %}
        <script src="{{ asset_url }}"></script>       
    {% endjavascripts %}

Now I have a index.html.twig in which I extend the base.html.twig.... Is that all I need? For me the script doesn’t work, do I need to somehow access it in index.html.twig too?

4
  • 1
    are the file loaded by the browser? if not, check in the web folder if the path are correct, if it is not present launch the command assets:install. Hope this help. Commented May 5, 2015 at 7:15
  • Symfony is backend, JavaScript frontend. JS works just like it did with another backend. Commented May 5, 2015 at 7:26
  • @Matteo I cant access the code right now but im pretty sure the paths are correct, and i did the assets install too. So I dont need to do anything in index.html.twig to access the script? It works automatically if i extend the base.html.twig? Commented May 5, 2015 at 7:30
  • of course you are using assetic so you must dump the content with the correct CLI command Commented May 5, 2015 at 7:33

1 Answer 1

1

You are messed up with Asstic library and Assets commponent. It is not the same. You should use command:

php app/console assetic:dump

This command will compile your js file and will put it in right place.

Sign up to request clarification or add additional context in comments.

1 Comment

I cant access the code right now, but is assetic:dump the only thing I need to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.