我想用{{loop.index}}我的注释数组的键将我的字符串值显示在我的数组'nameComments'中,但{{ nameComments[{{ loop.index }}] }}显示错误
{% for com in comments %}
<p>Comment {{ nameComments[{{ loop.index }}] }} : "{{ com['comment'] }}"</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
{% for com in comments %}
<p>Comment {{ nameComments[1] }} : "{{ com['comment'] }}"</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
并{{ loop.index }}告诉我价值:1
那么如何将循环索引实现到我的数组中呢?
我在使用Twig的Symfony2上,我在param中有2个数组:
我的控制器:
return $this->render('MyBundle:Default:index.html.twig',
array('checked' => $boxChecked,
'choices' => $choices));
Run Code Online (Sandbox Code Playgroud)
Vars'已检查'和'选择'是两个数组,我想显示$ checked [$ choices [$ i]]的值来与true ofr进行比较以将已检查或未应用于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 %}
Run Code Online (Sandbox Code Playgroud)
错误是: Expected name or number in "MyBundle:Default:index.html.twig" at line 22 (500 Internal Server Error)
第22行是: {% if checked.{{choice}} …