鉴于:
$column = array(
'value_1' => 'on',
'value_2' => 'on',
'value_3' => 'off'
);
$values= array('value_1', 'value_2', 'value_3');
非工作示例:
{% for value in values %}
<input type="checkbox" {% if column.{{value}} == 'on' %}checked{% endif %}>
{% endfor %}
问题是如何替换条件中的变量{{value}}?也许是column.{value}, column.[value],之类的东西column.'value'?但它不起作用。如何实施?
一切都变得容易多了。您可以使用方括号语法访问数组键:
这是一个带有示例的工作 TWIGFiddle 。
为什么要访问带点的数组元素
column.{{value}}?试试这个...
也试试这个