Twig 仅对细枝中的变量求值一次

Twig 仅对细枝中的变量求值一次,twig,locale,evaluate,Twig,Locale,Evaluate,在细枝布局中,我使用 有没有方便的方法让twig只对这个变量求值一次,然后在其他地方进一步使用它,而不是每次都求值 或者我需要设置twig变量并进一步使用它?使用set指令,例如: {% set locale = app.request.locale %} {# when you want to print it everywhere else: #} {{ locale }} {# you can also use it in other directives #} {% if locale

在细枝布局中,我使用

有没有方便的方法让twig只对这个变量求值一次,然后在其他地方进一步使用它,而不是每次都求值


或者我需要设置twig变量并进一步使用它?

使用
set
指令,例如:

{% set locale = app.request.locale %}
{# when you want to print it everywhere else: #}
{{ locale }}
{# you can also use it in other directives #}
{% if locale == 'eng' %}
...dosomething
{% endif %}

在主模板中指定值,并在其他位置使用变量
{% set locale = app.request.locale %}
{# when you want to print it everywhere else: #}
{{ locale }}
{# you can also use it in other directives #}
{% if locale == 'eng' %}
...dosomething
{% endif %}