Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a variable to an Assetic asset URL in Symfony2

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.

like image 979
ed209 Avatar asked Nov 27 '11 12:11

ed209


2 Answers

It is possible through this way :

<link rel="stylesheet" href="{{ asset('bundles/yourbundle/css/'~ SOMEVAR ~'/css/' ) }}" />
like image 52
Chopchop Avatar answered Sep 20 '22 04:09

Chopchop


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.

like image 27
Matt Avatar answered Sep 23 '22 04:09

Matt