我正在尝试将一个变量连接到数组键,以访问Twig中的某些值,但到目前为止没有成功。
我有一个大型的PHP数组,它有如下所示的键:
$array = [
...
...
...
'test_1' => $test_1,
'test_2' => $test_2
];我在Twig模板中尝试了以下内容:
{% for i in 1..2 %}
{% if array.test_{{ i }} != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}但这不管用。
有办法在Twig中获得这样的值吗?
发布于 2015-09-28 14:48:54
试试这个:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}https://stackoverflow.com/questions/32825622
复制相似问题