Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig: get element of an array by key within an if statement

I'd like to get the value of an element by key within an if statement.

Example:

works:

{{ example[5] }}

doesn't work:

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

Exception thrown:

Unexpected token "punctuation" of value "[" ("end of statement block" expected)

Thank you very much

Kind regards

like image 385
Ueli Avatar asked Jun 23 '12 12:06

Ueli


2 Answers

Instead of

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

try changing 'is' to '=='

{% if example2 == example[5] %} something ... {% endif %}
like image 79
MDrollette Avatar answered Sep 28 '22 07:09

MDrollette


Maybe you should use the attribute function to get the object or array value. This link may help you

like image 31
Leonardo Antúnez Avatar answered Sep 28 '22 07:09

Leonardo Antúnez