-2

How can I use loop.index in this code.

{% for veri in data %}

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

{% endfor %}

enter image description here .....

5

2 Answers 2

1

As pointed out by Goto u need to use loop.parent.loop as seen 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

Sign up to request clarification or add additional context in comments.

6 Comments

I do not have a data. I have 2 data and 2 loops. it have not been worked.
This is just an example code. This works with 2 arrays as well, see updated answer in which I work with data and data2. This is StackOverflow, don't expect us to do all your work. Anticipate on the provided answer and try to use them in your own code. Don't expect answers you just can copy/paste and be done with it
Of course I tried and changed your code. just I said . But it have not been worked...
Works perfectly with 2 arrays in my 2nd example
Update your question to reflect your adjusted code then
|
0

Do you want something like this?

{% 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.