Pass a Variable to an Assetic Asset Url in Symfony2

Pass a variable to an Assetic asset URL in Symfony2

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.

Symfony2 Javascript Assets - variables as arguments

From Pass a variable to an Assetic asset URL in Symfony2 is a link to this blog article http://jmsyst.com/blog/asset-variables-in-assetic

it shows you how to do it, although you need to have a finite number of variables and Assetic 1.1+

Symfony2 Assetic get asset urls from inside controller, not template

You can call ($packageName is optional):

$this->container->get('assets.packages')->getUrl($path, $packageName);

For older Symfony versions service is called templating.helper.assets, so you use:

$this->container->get('templating.helper.assets')->getUrl($path, $packageName);

It's used the same way as twig function (in fact this is called in the twig function).

Add parameter in symfony2 twig asset function

Assets are generally static files and not dynamic scripts, are you sure you're doing the right thing? If you have an asset that accepts a parameter maybe it should be a in a controller with a route defined so you can act on it within code and a {{ path('...', {key: value}) }}

Otherwise, The only thing assetic lets you do is auto-append a version number to assets using assets_version

Asset cache busting by parametrized url with Assetic

I've managed to solve this after some further research:
one needs to specify the package name when calling the asset() function, like so:

{% stylesheets output="css/global.css" "@AppBundle/Resources/assets/scss/frontend.scss" filter="scss" filter="?uglifycss" %}
<link rel="stylesheet" href="{{ asset(asset_url, 'css') }}">
{% endstylesheets %}

The package name "css' needs to be defined in config:

framework:
assets:
packages:
css:
version: '2'
version_format: '%%s?version=%%s'


Related Topics



Leave a reply



Submit