Php 在foreach细枝模板中选择元素

Php 在foreach细枝模板中选择元素,php,symfony,twig,Php,Symfony,Twig,我和Twig在Symfony2上,我在param中有2个数组: 我的控制器: return $this->render('MyBundle:Default:index.html.twig', array('checked' => $boxChecked, 'choices' => $choices)); Vars'checked'和'choices'

我和Twig在Symfony2上,我在param中有2个数组:

我的控制器:

return $this->render('MyBundle:Default:index.html.twig',
                             array('checked' => $boxChecked,
                                   'choices' => $choices));
Vars'checked'和'choices'是两个数组,我想显示$checked[$choices[$I]]的值,以便与true或false进行比较,以便将checked或not应用到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 %}
{%用于选项%}
{%如果选中。{{choice}}==true%}
{{选择|大写}}
{%else%}
{{选择|大写}}
{%endif%}
{%endfor%}
错误是:
第22行“MyBundle:Default:index.html.twig”中的预期名称或编号(500内部服务器错误)

第22行是:
{%if checked.{{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 %}
{%用于选项%}
{%如果选中[choice]==true%}
{{选择|大写}}
{%else%}
{{选择|大写}}
{%endif%}
{%endfor%}