是否可以将变量连接到 Twig 中的 if 语句?
Is it possible to concatenate a variable to an if statement in Twig?
我正在尝试将变量连接到数组键以访问 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 中访问这样的值?
试试这个:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}
我正在尝试将变量连接到数组键以访问 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 中访问这样的值?
试试这个:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}