Symfony 2 adds Javascripts, just one request

I have layout.html.twig with:

{% block js %} {% javascripts 'Resources/public/js/jquery/jquery-1.7.1.min.js' 'Resources/public/js/jquery/jquery.namespace.js' %} <script src="{{ asset_url }}" type="text/javascript"></script> {% endjavascripts %} {% endblock %} 

And I have index.html.twig with

 {% extends "MichaelMikeBundle::layout.html.twig" %} {% block js %} {{ parent() }} {% javascripts '@MichaelStoreBundle/Resources/public/js/index.js' 'Resources/public/js/jqueryui/jquery.ui.core.js' 'Resources/public/js/jqueryui/jquery.ui.widget.js' 'Resources/public/js/jqueryui/jquery.ui.button.js' %} <script src="{{ asset_url }}" type="text/javascript"></script> {% endjavascripts %} {% endblock %} 

The first page of the production mode returns two js files (two requests). Symfony2 merges two files from the layout and outputs as one query, and it does the same for the index - it merges 4 files and exits as another request.

My question is: Is it possible to have a layout and index files, as in my example, output all js as a single request? Or at least add javascripts from the index file to the layout ...

Thanks for the help guys!

+4
source share
2 answers

You will need to remove the call {{ parent() }} , copy the definition of the parent asset to the template and add additional files there.

 {% extends "MichaelMikeBundle::layout.html.twig" %} {% block js %} {% javascripts 'Resources/public/js/jquery/jquery-1.7.1.min.js' 'Resources/public/js/jquery/jquery.namespace.js' '@MichaelStoreBundle/Resources/public/js/index.js' 'Resources/public/js/jqueryui/jquery.ui.core.js' 'Resources/public/js/jqueryui/jquery.ui.widget.js' 'Resources/public/js/jqueryui/jquery.ui.button.js' %} <script src="{{ asset_url }}" type="text/javascript"></script> {% endjavascripts %} {% endblock %} 

There is no better support for this because it is not a good practice. The user has already uploaded the first two entries when the layout was first displayed. It makes sense to set headers with a great future than to combine these Javascripts into a child template.

+3
source

To insert js files into twig I write:

 {% block javascripts %} {{parent()}} <script src="{{ asset('public/js/userprofile.js') }}" type="text/javascript"></script> {% endblock %} 

public is located in the symfony / web / directory.

Is that all you want?

+3
source

Source: https://habr.com/ru/post/1392419/


All Articles