我正在使用 Assetic 包含我的脚本,并且需要传递当前区域设置。我该怎么做?
{% javascripts
'@MyBundle/Resources/public/components/moment/moment.js'
'@MyBundle/Resources/public/components/moment/lang/' ~ app.request.locale ~ '.js'
%}
字符串连接不起作用并抛出意外的值为“~”的标记“运算符”
最佳答案
您可以使用 assetic 的变量,如 Ryan Weaver's answer here 中所述。 。该功能有点损坏,但在您使用区域设置作为变量时可以在您的情况下使用。
The answer is that only two variables work by default (locale and env, and their values are preconfigued in Symfony: https://github.com/symfony/AsseticBundle/blob/master/DefaultValueSupplier.php#L31.
config_dev.yml
assetic:
use_controller: false
config.yml
You'll also need to set your assetic.variables.locale [...] to the total possible combinations of your variable:
assetic:
variables:
locale: [en,fr,de]
...然后在调用 assetic:dump
后在 javaScripts 标记内使用它们。
模板
{% javascripts
'bundles/my/components/{locale}.js'
%}
关于symfony - 如何在 Assetics 'javascript' 标签中使用 Twig 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17670858/