2013-07-16 24 views
4

我正在使用Assetic來包含我的腳本,並且需要傳遞當前的語言環境。我會怎麼做?如何在Assetics'javascript'標記中使用Twig變量?

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

字符串連接不工作,並拋出的價值意外令牌「經營者」,「〜」

回答

3

可以使用assetic的變量中Ryan Weaver's answer here描述。這個功能有點壞,但在你使用locale作爲變量的情況下會起作用。

答案是,只有兩個變量在默認情況下(區域和env, 工作,它們的值在Symfony的preconfigued: https://github.com/symfony/AsseticBundle/blob/master/DefaultValueSupplier.php#L31

config_dev.yml

assetic: 
    use_controller: false 

config.yml

您還需要你的assetic.variables.locale [...]設爲您的變量的 總可能的組合:

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

...然後用它們的使用javaScripts內標記後調用assetic:dump

模板

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

謝謝!從來沒有想過,這將是複雜的。基本上它可以工作,但不幸的是,momentjs沒有「en」的語言環境(比如我的應用程序),只是針對「en-GB」和「en-US」。所以最後我無法使用這種解決方法。 – acme