如何在树枝上使用2循环索引

时间:2018-02-12 11:42:45

标签: twig

如何在此代码中使用loop.index。

{% for veri in data %}

<li>no: {{loop.index}}</li> 
  {% for inveri in datain %}
<li>no: {{loop.index}}</li> 
  {% endfor %}

{% endfor %}

enter image description here .....

2 个答案:

答案 0 :(得分:1)

正如Goto所指出,你需要使用loop.parent.loop here

{% set data = [1,2,3,4,5,] %}

{% for d in data %}
{{ loop.index0 * (data|length + 1) + 1 }}
    {% for d in data %}
{{ loop.parent.loop.index0 * (data|length + 1) + 1 + loop.index }}
    {% endfor %}

{% endfor %}

twigfiddle

{% set data = [1,2,3,4,5,] %}
{% set data2 = [1,2,3,4,5,6,7,8,9] %}

{% for d in data %}
{{ loop.index0 * (data2|length + 1) + 1 }}
    {% for d in data2 %}
{{ loop.parent.loop.index0 * (data2|length + 1) + 1 + loop.index }}
    {% endfor %}

{% endfor %}

twigfiddle with 2 data-sets

答案 1 :(得分:0)

你想要这样的东西吗?

{% set data  = [1, 2, 3, 4, 5] %}
{% set data2 = [1, 2, 3] %}

{% set i = 1 %}
{% for d in data %}
    {{ i }}
    {% set i = i + 1 %}

    {% for d2 in data2 %}
        {{ i }}
        {% set i = i + 1 %}
    {% endfor %}
{% endfor %}

See TwigFiddle