symfony - 如何在 Assetics 'javascript' 标签中使用 Twig 变量?

标签 symfony twig assetic

我正在使用 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/

相关文章:

symfony - 使用 Assetic 通过参数化 url 清除 Assets 缓存

symfony - 如何在 Symfony 中使用 assetic 查看 Assets 并自动安装和转储 Assets

php - 在 redis 中存储 Symfony2 session

javascript - 外部 JS 文件中的 Twig 变量

twig - 有条件地在 Twig 2 中定义一个 block

node.js - 在 Swig 模板中使用 include 时连接变量和文件

javascript - 如何在 Twig 中保留双引号?

Symfony 2 如何从另一个包导入 LESS 文件

symfony - 在 Symfony 中自动阻止/禁止暴力扫描器

git - 结构脚本/bash : how to detect git file changed