小编kee*_*zer的帖子

将twig循环索引转换为数组

我想用{{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

那么如何将循环索引实现到我的数组中呢?

arrays indexing loops for-loop twig

10
推荐指数
2
解决办法
4万
查看次数

选择元素到foreach Twig模板

我在使用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 &quot;MyBundle:Default:index.html.twig&quot; at line 22 (500 Internal Server Error)

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

php symfony twig

2
推荐指数
1
解决办法
6407
查看次数

标签 统计

twig ×2

arrays ×1

for-loop ×1

indexing ×1

loops ×1

php ×1

symfony ×1