How can I use Twig variable in javascript Assetics tag?

I use Assetic to include my scripts and need to pass the current locale. How can I do it?

{% javascripts '@MyBundle/Resources/public/components/moment/moment.js' '@MyBundle/Resources/public/components/moment/lang/' ~ app.request.locale ~ '.js' %} 

String concatenation does not work and throws an unexpected token "operator" value of "~"

+4
source share
1 answer

You can use athletic variables as described in the Ryan Weaver answer here . This function is a variation, but will work in your case since you use the locale variable as a variable.

The answer is that by default only two variables work (locale and env, and their values ​​are pre-configured in Symfony: https://github.com/symfony/AsseticBundle/blob/master/DefaultValueSupplier.php#L31 .

config_dev.yml

 assetic: use_controller: false 

config.yml

You will also need to set your assetic.variables.locale [...] to the common possible combinations of your variable:

 assetic: variables: locale: [en,fr,de] 

... then use them inside the javaScripts tag after calling assetic:dump .

template

 {% javascripts 'bundles/my/components/{locale}.js' %} 
+3
source

Source: https://habr.com/ru/post/1491585/


All Articles