I’m working on a Symfony2 application and I’m trying to import resources (here, css and js) in a specifig twig template, instead of in the layout templates. However, what I’ve tried doesn’t seem to work when I use the extends property. Yet it does work when there is no layout inheritance.
Here’s the code I used :
{% block stylesheets %}
{% stylesheets 'bundles/mybundlename/css/style.css'
filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
{% endblock %}
{% block javascripts %}
{% javascripts 'bundles/mybundlename/js/jquery-1.7.2.min.js'
'bundles/mybundlename/js/jquery-ui-1.8.20.custom.min.js'
%}
<script src='{{ asset_url }}' type='text/javascript'></script>
{% endjavascripts %}
{% endblock %}
{% extends "MyBundle::layout.html.twig" %}
... the rest of my template ...
Bottom line, how would I import those resources in my inheriting template?
Or is it simply better to just import them in the general layout (but then it loads for every page :/) ?
Edit : I have used @MyBundle route types as well, and they are being searched by the engine (if the path is wrong, I get an error), but the resources are not added the the html section.
Second edit : The reason why it was not working is likely that I hadn’t declared the {{% blocks %}} in my top level layout. I did and used {{ parent() }} (as a matter of “cleanliness”) in my sub layouts and pages, and it works.
{% extends ... %}must come first.You might also want to use the
parentfunction to append to the stylesheets and javascripts defined in the layout instead of overwriting them.