I have a 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 %}
I production mode page returns two js files (two requests). Symfony2 merges two files from layout and outputs as one request and it does the same for index - it merges 4 files and outputs as another request.
My question is: Is is possible to have layout and index files like in my example output all js as one request? Or at least append javascripts from index file into layout...
Thanks for any help guys!
I have a 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 %}
I production mode page returns two js files (two requests). Symfony2 merges two files from layout and outputs as one request and it does the same for index - it merges 4 files and outputs as another request.
My question is: Is is possible to have layout and index files like in my example output all js as one request? Or at least append javascripts from index file into layout...
Thanks for any help guys!
Share Improve this question asked Jan 23, 2012 at 7:58 TroodoN-MikeTroodoN-Mike 16.2k15 gold badges59 silver badges78 bronze badges 2- Duplicate?. From there: it's impossible right now. – meze Commented Jan 23, 2012 at 12:27
- @TroodoN-Mike I'm facing the same issue. what's the solution for this?? – Yash Parekh Commented Mar 29, 2018 at 13:13
2 Answers
Reset to default 3You would have to remove the call to {{ parent() }}
, copy the parent asset definition to the template, and add more inputs 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 not better support for this because it is not a good practice. The user has already downloaded the first two entries when the layout was first rendered. It makes more sense to set far-future expiration headers than to merge those Javascripts into the child template's.
For insert js files in the twig i write:
{% block javascripts %}
{{parent()}}
<script src="{{ asset('public/js/userprofile.js') }}" type="text/javascript"></script>
{% endblock %}
public is in Symfony/web/ directory.
Is this all that you want?