Pass a variable to an Assetic asset URL in Symfony2
Asked Answered
R

5

13

Is there a way to pass a variable to the Assetic method in templates

{% stylesheets
    '@SomeExampleBundle/Resources/views/ SOMEVAR /css/*'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

So what I want to do is pass SOMEVAR from the controller.

Randy answered 27/11, 2011 at 12:32 Comment(1)
Use PHP as template language and just script it. Otherwise implement the logic in PHP and inject that into the twig language.Cauterant
B
18

For now, I don't think it is possible at all. The reason behind this is that Assetic is run upfront to dump the assets, so it does not run the Twig template to compute the variable. This is probably the same if you do it in a PHP template.

This means that runtime variables will not be computed and expanded. Thus, this make it impossible to generate the assets if a variable is used. This may change in the future, but this would incur an overhead in production each time the assets are requested by the user because Assetic would need to generate the assets.

I know it is possible to programmatically defines and generates the asset by using the code found in Assetic directly (not by using the AsseticBundle). You will need to experiment, read the source code, and do trials and errors to work out off this problem.

There is little to no documentation on Assetic at the moment. The only link I can give is the README found on the github page of Assetic here. I hope this will change soon.

Hope this helps.

Burnsides answered 27/11, 2011 at 15:4 Comment(0)
B
25

It is possible through this way :

<link rel="stylesheet" href="{{ asset('bundles/yourbundle/css/'~ SOMEVAR ~'/css/' ) }}" />
Brilliancy answered 28/4, 2012 at 23:22 Comment(1)
this doesn't use assetic, but symfony's asset function. you can not use filters with thisSpanos
B
18

For now, I don't think it is possible at all. The reason behind this is that Assetic is run upfront to dump the assets, so it does not run the Twig template to compute the variable. This is probably the same if you do it in a PHP template.

This means that runtime variables will not be computed and expanded. Thus, this make it impossible to generate the assets if a variable is used. This may change in the future, but this would incur an overhead in production each time the assets are requested by the user because Assetic would need to generate the assets.

I know it is possible to programmatically defines and generates the asset by using the code found in Assetic directly (not by using the AsseticBundle). You will need to experiment, read the source code, and do trials and errors to work out off this problem.

There is little to no documentation on Assetic at the moment. The only link I can give is the README found on the github page of Assetic here. I hope this will change soon.

Hope this helps.

Burnsides answered 27/11, 2011 at 15:4 Comment(0)
B
3

To elaborate a bit on Chopchop's answer:

First you need to include all files that assetic needs to dump, as it needs to know what you need dumped. What you can make in a conditional manner is the inclusion of the asset itself at runtime.

So first put in the assetic part:

{% javascripts 
'@ExampleComBundle/Resources/public/js/module1.js'
'@ExampleComBundle/Resources/public/js/module2.js'
%}
{% endjavascripts %} 

Now you can put in the condition that you wanted. Both those script will be dumped at deployment time but you will be able to choose at runtime which one to include:

<link rel="stylesheet" href="{{ asset('bundles/examplecombundle/js/module' ~ WHICH_MODULE_TO_INCLUDE ~ '.js ) }}" />

The ~ character is just the concatenation operator in Twig templates.

Works of course the same with CSS and JS.

Bradway answered 20/11, 2013 at 23:42 Comment(0)
I
1

Another alternative, which works with limited ranges of options (Piotr's solution didn't work for me in dev mode):

{% javascripts
    '@AcmeDemoBundle/Resources/public/js/module_A.js'
    output='js/module_A.js'
%}
    {% if myVar == "A" %}
        <script src="{{ asset_url }}"></script>
    {% endif %}
{% endjavascripts %}

{% javascripts
    '@AcmeDemoBundle/Resources/public/js/submodule1_B.js'
    '@AcmeDemoBundle/Resources/public/js/submodule2_B.js'
    '@AcmeDemoBundle/Resources/public/js/submodule3_B.js'
    output='js/module_B.js'
%}
    {% if myVar == "B" %}
        <script src="{{ asset_url }}"></script>
    {% endif %}
{% endjavascripts %}

...

That way, each module will be dumped on deployment or dynamically handled by assetic, AND you can choose which module to be included, using myVar.

Note: I used the javascripts block here, but it will work the same with stylesheets.

Innovate answered 15/9, 2015 at 9:45 Comment(0)
K
-1

Maybe I didn't understand, but... are you trying to do this?

{% stylesheets
    '@SomeExampleBundle/Resources/views/' ~ somevar ~ '/css/*'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

Because, as far as I know, the string passed to stylesheets is a valid Twig expression, so you're free to use variable interpolation.

Anyway, I don't think it's a good practice to have dynamic assets. What do you exactly want to achieve? There may be a better solution.

Kao answered 27/11, 2011 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.