我的控制器在名为'petition'的变量中向Twig发送以下关联数组;
Array
(
[0] => stdClass Object
(
[id] => 1
[doctype] => "somedoc"
[nrdoc] => "99"
[datadoc] => "2015-01-01"
)
[1] => stdClass Object
(
[id] => 2
[doctype] => "otherdoc"
[nrdoc] => "100"
[datadoc] => "2015-01-01"
)
)
然后,在我的Twig模板(视图)中,我正在这样做:
{% for id in petition %}
{% if id.doctype == 'somedoc' %}
{{id.nrdoc}} / {{id.datadoc}}
{% else %}
UNDEFINED!
{% endif %}
{% endfor %}
问题在于我无法弄清楚如何输出“UNDEFINED!”的逻辑。只有一次,如果doctype!=“somedoc”,当数组中有其他key->值元素时。我正在这样做,它会输出“未定义!”每次脚本循环......
提前感谢您的帮助
加布里埃尔
答案 0 :(得分:2)
One variant is to define an extra variable for this:
{% set undefined = false %}
{% for id in petition %}
{% if id.doctype == 'somedoc' %}
{{ id.nrdoc }} / {{ id.datadoc }}
{% else %}
{% set undefined = false %}
{% endif %}
{% endfor %}
{% if undefined == true %}
UNDEFINED!
{% endif %}
You can read more about setting Twig variables here.