3

I'm trying to add stylesheets to an array so that as twig templates extend through a second and third levels the aggregated styles will carry through.

This topic is related to Combining Assetic Resources across inherited templates

From config.yml, I made a global array mystyles so that we can add to the list of necessary styles as we "bubble up" through the rendering process.

twig:
debug:            %kernel.debug%
strict_variables: %kernel.debug%
globals:
  mystyles: []

The first template called from my action is from CommunicatorBundle/Resources/views/Admin/Workspace.html.twig and I've set the specific style for this page called admin.workspace.css.

{% extends "DJCommunicatorBundle::base.html.twig" %}
{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/admin.workspace.css"]|merge(mystyles) %}

It extends CommunicatorBundle/Resources/views/base.html.twig which has it's own requirement data-table.css.

{% extends "DJSharedBundle::base.html.twig" %}
{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/data-table.css" ]|merge(mystyles) %}

Finally, we load the outermost template, SharedBundle/Resources/views/base.html.twig, which has it's own styles to add before all others.

<head>
{% set mystyles = ['@DJSharedBundle/Resources/public/css/global.css', '@DJSharedBundle/Resources/public/css/grid.990.9-col.css']|merge(mystyles) %}
{% stylesheets {{ mystyles }} %}
    <link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %} 
</head>

However, it breaks at this line

{% stylesheets {{ mystyles }} %}

inspite of this type of test that prints the array that I expect in the proper order

{{ mystyles|join(', ') }}

It seems that the {% stylesheets %} tag wants something like the following snippit to work (which is understandably not an array object, but a whitespace separated list of delimited strings).

{% stylesheets 
    '@DJSharedBundle/Resources/public/css/global.css'     
    '@DJSharedBundle/Resources/public/css/grid.990.9-col.css' 
    '@DJCommunicatorBundle/Resources/public/css/data-table.css'
    '@DJCommunicatorBundle/Resources/public/css/admin.workspace.css'
%}
    <link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}

Even then, I tried setting a string to such a long value and printing it, but this doesn't work either:

{%
    set str = "@DJSharedBundle/Resources/public/css/global.css 
    @DJSharedBundle/Resources/public/css/grid.990.9-col.css 
    @DJCommunicatorBundle/Resources/public/css/data-table.css
    @DJCommunicatorBundle/Resources/public/css/admin.workspace.css"
%}
{% stylesheets {{ str }} %}

I feel like the global should be a viable solution, though not currently working. Hopefully I'm close. What might fix this?

Community
  • 1
  • 1
ojreadmore
  • 1,465
  • 2
  • 17
  • 33

1 Answers1

4

This is not possible right now because Assetic statically analyzes your templates to find any assets you've defined there. Supporting these dynamic cases is on the roadmap.

Kris Wallsmith
  • 8,945
  • 1
  • 37
  • 25