Combining Assetic Resources across inherited templates
Asked Answered
D

2

26

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?

Delisle answered 5/8, 2011 at 15:42 Comment(0)
D
18

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.

Divinadivination answered 5/8, 2011 at 16:23 Comment(3)
Drat, I suspected as much. Thanks for the pointer to twig macros.Delisle
Doesn't Assetic run first though?Microscopy
I think this is a correct behavior: combining assets that have to be on a single page/route sin't smart I think. This is exactly the behaviour I desire. So, thank you for the solution on how to insert files only for a single route! :)Atwood
T
23

You can actually do the following:

In layout.html.twig (or whatever your layout is)

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

And in any template that extends that layout:

{% block stylesheets %}
    {{ parent() }}
    {% stylesheets 'additional_assets_here' %}
         <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

Then you wouldn't need to retype all the old assets as suggested by Nemanja Niljkovic

Thegn answered 26/7, 2012 at 22:7 Comment(2)
Yes, but (cite from Nemana Niljkovic answer): "but you would have 2 links then as you can't combine the old assetic tag with the new one"Firry
There isn't much point in combining the new files into the set of stylesheets/scripts from the parent, because that would defeat the purpose of caching as you may have many such pages with their own set of additional files... combining them would mean a new set of combined files for every page! Say bye bye to browser cache. Therefore it is actually better to have a separate set of combined files for pages with tier own additional files, so at least the more generic set of stylesheets/scripts will always be cachedThegn
D
18

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.

Divinadivination answered 5/8, 2011 at 16:23 Comment(3)
Drat, I suspected as much. Thanks for the pointer to twig macros.Delisle
Doesn't Assetic run first though?Microscopy
I think this is a correct behavior: combining assets that have to be on a single page/route sin't smart I think. This is exactly the behaviour I desire. So, thank you for the solution on how to insert files only for a single route! :)Atwood

© 2022 - 2024 — McMap. All rights reserved.