[英]Is it possible to concatenate a variable to an if statement in Twig?
I am trying to concatenate a variable to an array key to get access to certain values in Twig, but no success so far. 我试图将变量连接到数组键以访问Twig中的某些值,但到目前为止没有成功。
I have a large PHP array that has for example keys like this: 我有一个大型的PHP数组,例如,具有以下键:
$array = [
...
...
...
'test_1' => $test_1,
'test_2' => $test_2
];
I tried the following in my Twig template: 我在Twig模板中尝试了以下操作:
{% for i in 1..2 %}
{% if array.test_{{ i }} != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}
but that doesn't work. 但这不起作用。
Is there a way to access values like this in Twig? 在Twig中是否可以访问这样的值?
Try this: 尝试这个:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.