Good day.

In the index.html.twig file I have

{% block javascripts %} {% javascripts 'assets/vendor/jquery/dist/jquery.min.js' 'assets/vendor/bootstrap/dist/js/bootstrap.min.js' %} <script src="{{ asset_url }}"></script> {% endjavascripts %} {% endblock %} 

here are the main scripts.

But in another file I would like to insert my own scripts. How can I add them to existing ones?

If it were just

 {% block javascripts %} <script src="{{asset(path(...))}}"></script> {% endblock %} 

you could use {{parent ()}}. But in this case it does not work for some reason.

What should the addition look like?

    1 answer 1

    in twig add 1 more block. In index.html.twig :

     {% block javascripts %} {% javascripts 'assets/vendor/jquery/dist/jquery.min.js' 'assets/vendor/bootstrap/dist/js/bootstrap.min.js' %} <script src="{{ asset_url }}"></script> {% endjavascripts %} {% endblock %} {% block additional_javascripts %}{% end block %} 

    And in another file:

     {% block additional_javascripts %} {% javascripts 'PATH/ADDITIONAL/SCRIPT.js' %} <script src="{{ asset_url }}"></script> {% endjavascripts %} {% end block %} 

    You can also add an intermediate template, let's say index_with_additional.twig.html in it inherit from index.twig.html and redefine the javascript block:

      {% extends '::index.html.twig' %} {% block additional_javascripts %} {% javascripts assets/vendor/jquery/dist/jquery.min.js' 'assets/vendor/bootstrap/dist/js/bootstrap.min.js' 'PATH/ADDITIONAL/SCRIPT.js' %} <script src="{{ asset_url }}"></script> {% endjavascripts %} {% endblock %} 

    And in the file "friend" file do so

     {% extends '::index_with_additional.twig.html' %} 
    • Thank. Or maybe there is an opportunity to add this to an existing file? Just asset compiles everything later in one file, which is what I need. The client is very sensitive to the results of google speed test. - Vladislav
    • Add - no, but you can rewrite. Ie you insert all the same, only with additional scripts. - Kostiantyn Okhotnyk
    • And the javascript block will be overwritten, with previous and additional scripts - Kostiantyn Okhotnyk
    • Then the first option is definitely better. Otherwise, with the addition and census in the end, wild porridge will work. In a lot of places you need to insert the code will. - Vladislav
    • or you can make a template with additional scripts and inherit from it where you need it (if additional scripts are always the same) - Kostiantyn Okhotnyk