树枝模板中的动态功能

时间:2016-04-07 07:16:43

标签: symfony

我想在许多twig-templates上显示一些动态文本。 我不想将这个变量添加到每个Action中,而是希望将其写入一次并以某种方式将其添加到模板中,如

{{ displaytext('ann') }}

我怎样才能做到这一点?

2 个答案:

答案 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 %}