我正在尝试访问数组,但无法访问它。在我的config.yml中,下面是我的数组:
abc : [xyz]在另一个文件中,我编写了以下代码来访问abc数组。
{% if abc[0] is defined) %}
then do something
{% endif %}但不知何故,它不起作用。请帮帮我,我在这方面是新手。
发布于 2016-03-04 21:33:01
这取决于变量是否总是被声明:
如果变量总是声明的,并且数组可以为空
{% if abc is not empty %}
{# then do something #}
{% endif %}Twig中的<variable> is not empty等同于PHP中的!empty($variable)。当提供数组时,is not empty将检查数组中是否有值和/或值。
empty test in Twig documentation。
如果变量不是始终声明的
检查abc变量是否已声明且不为空:
{% if (abc is declared) and (abc is not empty) %}
{# then do something #}
{% endif %}Twig中的<variable> is declared等同于PHP中的isset($variable)。
发布于 2016-03-04 16:45:10
基于注释,我建议使用foreach循环,并根据索引值定义I。如下所示:
{% for abcs in abc %}
{% if (loop.index == 0) %}
then do something
{% endif %}
{% endfor %}BR的
https://stackoverflow.com/questions/35789907
复制相似问题