我在 Symfony2 上使用 Twig,参数中有 2 个数组:
我的 Controller :
return $this->render('MyBundle:Default:index.html.twig',
array('checked' => $boxChecked,
'choices' => $choices));
变量“checked”和“choices”是两个数组,我想显示 $checked[$choices[$i]] 的值以与 true 或 false 进行比较,以将检查或不应用到 twig tpl 的输入中。
这是我的代码,但不起作用:
{% for choice in choices %}
{% if checked.{{choice}} == true %}
<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" /></div>
{% else %}
<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" checked="checked" /> </div>
{% endif %}
{% endfor %}
错误是:“MyBundle:Default:index.html.twig”中的预期名称或编号第 22 行(500 内部服务器错误)
第 22 行是:{% if selected.{{choice}} == true %}
我不知道如何检查我的。(我的VAR选择进入我的foreach选择)到twig tpl?
最佳答案
您必须使用括号语法:
{% for choice in choices %}
{% if checked[choice] == true %}
<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" /></div>
{% else %}
<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" checked="checked" /> </div>
{% endif %}
{% endfor %}
关于php - 选择元素到 foreach Twig 模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17041208/