首页 文章

Twig:在if语句中按键获取数组的元素

提问于
浏览
13

我想在if语句中按键获取元素的值 .

Example:

作品:

{{ example[5] }}

不起作用:

{% if example2 is example[5] %} something ... {% endif %}

Exception thrown:

意外的标记“标点符号”值“[”(“语句块结束”预期)

非常感谢你

亲切的问候

2 回答

  • 16

    代替

    {% if example2 is example[5] %} something ... {% endif %}
    

    尝试将'is'更改为'=='

    {% if example2 == example[5] %} something ... {% endif %}
    
  • 8

    也许您应该使用 attribute 函数来获取对象或数组值 . This link可以帮到你

相关问题