首页 文章

twig测试模板中的全局值

提问于
浏览
1

在我的symfony项目中,我在parameters.yml中有一个配置变量

role : front1

在我的config.yml中,我的配置变量被公开了

twig:
   globals:     
      role : %role%

在我的template.html.twig中,当我尝试用if测试它时,我有一个错误 .

{% if {{role}} == 'front2' %}
    {# front2 #}
    (2)
{% else %}
    {# front1 #}
    (1)
{% endif %}

错误:
哈希键必须是带引号的字符串,数字,名称或括在括号中的表达式(第132行中的_1654297_,值为"{" in :: base.html.twig的意外标记"punctuation"

我不明白错误在哪里 . 你能帮助我吗 ?

提前致谢 .

1 回答

  • 2

    正如Oligan所说的那样:

    {% if {{role}} == 'front2' %}
    

    是错的 . 你需要使用:

    {% if role == 'front2' %}
    

    仅使用{}外部条件 .

相关问题