2012-06-23 44 views
12

我想通過if語句中的鍵獲取元素的值。Twig:通過if語句中的鍵獲取數組的元素

例子:

作品:

{{ example[5] }} 

不起作用:

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

拋出異常:

意外標記的值 「標點」 「[」( 「語句塊結束」 的預期)

非常感謝您

親切的問候

+2

你想做什麼?要比較雙方,請使用'=='。 'is'用於某些特殊情況,如'null','defined'或'even'。 – Maerlyn

回答

15

而不是

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

試將'is'改爲'=='

{% if example2 == example[5] %} something ... {% endif %} 
+0

謝謝 - 工作! – Ueli

8

也許你應該使用attribute函數來獲取對象或數組值。 This link可能會幫助你

+0

我upvoted,因爲這解決了我的情況,必須做的另一個數組的值在數組中定位一個鍵。其他人可能正在尋找類似的解決方案來解決這個問題。 –

相關問題