Twig inheritance: passing assets to base at different levels

New topics about Symfony 2 should go here

Moderators: dcobalt, tiagojsag

Twig inheritance: passing assets to base at different levels

Postby Mindphreaker » Wed Apr 11, 2012 10:02 pm

Hello everyone,

What we want to do is, passing assets to a base template block from a certain include level.

We have a setup similar like this:

A simple base template with a javascripts block...
Code: Select all
#base.html.twig

{% block javascripts %}{% endblock %}


An index template which extends the base, where we want to include different boxes...
Code: Select all
#index.html.twig

{% extends '::base.html.twig' %}

{% include 'MyBundle:Box:welcome.html.twig' %}


Welcome box...
Code: Select all
#welcome.html.twig

{% block javascripts %}
    <script type="text/javascript" src="/bundles/myotherbundle/js/welcome.js"></script>   
{% endblock %}


At the level of the welcome box we want to add some welcome box specific javascript code.
It would be nice if the js code could be passed to the javascripts block defined in the base template. Unfortunately it seems that this is not possible with our approach.
Is our setup such an uncommon practice or is there a reason why this code does not work? Any hints/hacks/best practices on how to handle this problem would be appreciated!
wuchthosting.com Webhosting!

Anyone who can give you orders can stop you from solving whatever problem you're being fascinated by. - Eric. S. Raymond
Mindphreaker
Senior Member
 
Posts: 159
Joined: Wed Dec 03, 2008 10:02 pm
Location: Austria

Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 12 guests