forloop
Information about the current for loop.
The forloop object is only available within the for tag.
Properties
Whether the current iteration is the first in the loop.
true: The current iteration is the first in the loop.false: The current iteration is not the first in the loop.
The 1-based index of the current iteration.
The 0-based index of the current iteration.
Whether the current iteration is the last in the loop.
true: The current iteration is the last in the loop.false: The current iteration is not the last in the loop.
The total number of iterations in the loop.
The parent forloop object. If the current loop is not nested within another for loop, nil is returned.
The 1-based index of the current iteration, in reverse order.
The 0-based index of the current iteration, in reverse order.
Examples
Single-level loop
The following example uses a single-level loop to iterate over an array and retrieves the index of the current iteration via forloop.index.
{{#var arr = "one,two,three" | split(",") /}}
{{#for item in arr}}
- {{ forloop.index }} {{ item }}
{{/for}}
Nested loops
The following example performs multi-level iteration within nested loops and retrieves the index of the outer (parent) iteration across levels via forloop.parentloop.index.
{{#var arr1 = "one,two,three" | split(",") /}}
{{#var arr2 = "four,five,six" | split(",") /}}
{{#for item1 in arr1}}
- parent: {{ forloop.index }} {{ item1 }}
{{#for item2 in arr2}}
- nested: {{ forloop.index }} {{ item2 }} parent: {{forloop.parentloop.index}}
{{/for}}
{{/for}}