2

我在带有 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 进行比较,以将已检查或不应用于 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 %}

错误是:Expected name or number in &quot;MyBundle:Default:index.html.twig&quot; at line 22 (500 Internal Server Error)

第 22 行是:{% if checked.{{choice}} == true %}

我不知道我是如何检查的。(我的 VAR CHOICE 进入我的 foreach CHOICES)进入 twig tpl ?

4

1 回答 1

5

您必须改用括号语法:

    {% 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 %}
于 2013-06-11T10:42:14.213 回答