Skip to main content

forloop

Information about a parent for loop.

Properties

Returns true if the current iteration is the first. Returns false if not.

The 1-based index of the current iteration.

Anchor to
index0

The 0-based index of the current iteration.

Returns true if the current iteration is the last. Returns false if not.

Anchor to
length

The total number of iterations in the loop.

Anchor to
parentloop

The parent forloop object.

If the current for loop isn't nested inside another for loop, then nil is returned.

Example
Use the parentloop property
{% for i in (1..3) -%}
{% for j in (1..3) -%}
{{ forloop.parentloop.index }} - {{ forloop.index }}
{%- endfor %}
{%- endfor %}

Output

1 - 1
1 - 2
1 - 3

2 - 1
2 - 2
2 - 3

3 - 1
3 - 2
3 - 3
Anchor to
rindex

The 1-based index of the current iteration, in reverse order.

Anchor to
rindex0

The 0-based index of the current iteration, in reverse order.

{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
{% for page in pages -%}
{%- if forloop.length > 0 -%}
{{ page.title }}{% unless forloop.last %}, {% endunless -%}
{%- endif -%}
{% endfor %}

Output

About us, Contact, Potion dosages
Was this section helpful?