2016-09-14 35 views
0

我有一個包含這樣一個數目的命名陣列的對象的樹枝變量:樹枝:檢查中的對象包含的所有數組是空

{{ dump(trashbin) }} 

輸出:

array:2 [ 
    "Campaign" => [] 
    "ClientBudget" => [] 
] 

我什麼目前正在檢查包含的陣列是否長度爲0:

{% if trashbin.Campaign|length == 0 and trashbin.ClientBudget|length == 0 %} 
Nothing to undelete. Trashbin empty 
{% endif %} 

將來,任何數量的命名數組我無法預見的名字可能會添加到該列表中。

如果所有包含的數組都是空的,我該如何簡化和一般化檢查?我想在這種情況下向用戶顯示一條特殊消息。

回答

2

如果你不想讓你的控制器/模型內部的邏輯,你將不得不使用此一flag

{% set has_trash = false %} 
{% for trash in trashbin if not trash is empty %} 
    {% set has_trash = true %} 
{% endfor %} 

{% if not has_trash %} 
    Nothing to delete 
{% endif %} 

twigfiddle

+0

那麼,你有你的答案,因爲這是純粹的'樹枝' – DarkBee

+0

誤讀您的評論。這正是我所希望的。謝謝! – connexo

相關問題