[英]Verify if is in array in twig template
我有一個問題,是否可以在樹枝模板中創建in_array? 例如,我有一個名為aColors的數組,其中包含一種產品的顏色:
Array[
0 => "1"
1 => "2"
]
我在Libraries文件夾中有一個課:
class Colors{
public static $aColors = array(
'1' => 'White',
'2' => 'Black',
'3' => 'Yellow',
'4' => 'Red',
'5' => 'Green',
'6' => 'Blue',
);
}
現在,我想驗證是否使用product =顏色的array的值和使用常規顏色的array的鍵,並在.twig中進行了嘗試:
{% for key,val in aColors %}
{% if val in Colors::aColors %}
{% endfor %}
但是不行。 存在解決方案? 請幫我。 誰能幫我? 提前謝謝!!!
Twig不能在其中運行任何PHP代碼。 這是Twig開發人員的正式立場:Twig是模板引擎,唯一要做的就是模板化,它對類,靜態類屬性和其他僅PHP的東西一無所知。 您有以下選項:
您只是在比較按鍵,因此請使用
{% for key,val in aColors %}
{% if val in Colors::aColors|keys %}
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.