我遇到了一个问题,我在Symfony2 twigjs和assetic中使用它来动态渲染我的一些模板。
我已经阅读了文档、来源和测试。
在TransFilterCompilerTest.php/testCompileDynamicTranslations
中,
我的树枝模板中的{{ 'foo' | trans }}
将在twigjs模板中被替换
'this.env_.filter("trans",'...
在我的推特上有一个
但在我编译的js中,我只有sb.append(twig.filter.escape(this.env_, "posted", "html", null, true));
你知道为什么吗?
谢谢!
经过进一步的调查,我发现虽然翻译编译过滤器是几个月前在JMSTwigJsBundle中添加的,但所需的功能是最近才添加到Assetic中的。使用库的已发布版本将不起作用。目前,必须使用git中的master。
正在使用deps文件。。。
[assetic]
git=http://github.com/kriswallsmith/assetic.git
[AsseticBundle]
git=http://github.com/symfony/AsseticBundle.git
target=/bundles/Symfony/Bundle/AsseticBundle
必须将网站支持的语言指定为参数。我把它添加到我的config.yml文件中。
parameters:
assetic.variables:
locale: ['en', 'fr']
最后,文件集必须指示它根据区域设置而变化。
{% javascripts vars=["locale"]
'@AcmeBundle/Resources/views/Default/some_template.html.twig'
filter="twig_js"
%}
这里有一个完整的样本模板:
{% twig_js name="some_template" %}
<b>{{'test.say.hello' | trans({"%name%": name|default("World")})}}</b>
twig.js引导文件也必须在模板定义之前加载。调用模板如预期:
Twig.render(some_template, {name: 'CoBaLt2760'})