Adding JS and CSS cleanly from included Twig templates Adding JS and CSS cleanly from included Twig templates symfony symfony

Adding JS and CSS cleanly from included Twig templates


I know I'm a little late to the party, but with Twig 1.2, you can utilize the use tag and the block function:

GenericBundle:Generic:page.html.twig

{% block javascripts %}    <script src="..."></script>{% endblock %}{% block included_content %}   Bar{% endblock %}

layout.html.twig

{% use 'GenericBundle:Generic:page.html.twig' with javascripts as page_javascripts %}{% block javascript %}    {% javascripts        '@GenericBundle/Resources/public/js/app/jquery/jquery.min.js'        '@GenericBundle/Resources/public/js/lib/bootstrap/bootstrap.min.js'    %}        <script src="{{ asset_url }}"></script>    {% endjavascripts %}    {{ block('page_javascript') }} // Don't forget the 'braces'{% endblock %}...{{ block('included_content') }} // Don't forget the 'braces'