Search code examples
templatesresourcessymfonytwigassetic

Combining Assetic Resources across inherited templates


We are building a new site using Symfony2, and Assetic looks very promising for resource management, in particular for combining and processing all js/css files together automatically.

We wil have some resources that are used site wide, and some that are specific to particular pages. We will also be using a three tiered inherited approach to templates.

Is there a way to combine the two concepts, i.e. to automatically add additional resources in inherited templates so that they are all output as a single resource?


Solution

  • Unfortunately, you can't :(

    You can't override the assetic tags to add more assets. You can however do the following:

    {% block stylesheets %}
        {% stylesheets 'your_assets_here' %}
             <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

    Then, when you extend the template:

    {% block stylesheets %}
        {% stylesheets 'your_old_assets_here' 'your_new_assets_here' %}
             <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

    In the overridden block, you can use parent() to include the parent block, but you would have 2 links then: you can't combine the old assetic tag with the new one.

    You could however make a twig macro that would output the {% stylesheets %} assetic tag with your old assets, and as input it would contain new asset locations.

    More info here.