我想在许多twig-templates上显示一些动态文本。 我不想将这个变量添加到每个Action中,而是希望将其写入一次并以某种方式将其添加到模板中,如
{{ displaytext('ann') }}
我怎样才能做到这一点?
答案 0 :(得分:1)
您可以通过创建Twig Extension类来实现此目的。
在http://symfony.com/doc/current/cookbook/templating/twig_extension.html
了解详情答案 1 :(得分:1)
您可以使用此变量创建文件并将其包含在其他文件中 - > Twig includes
您可以为所包含的页面设置变量,如文档中所示:
{# template.html will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html' with {'foo': 'bar'} %}
{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}