forloop
Information about a parent for
loop.
Properties
The parent
forloop
object.If the current
for
loop isn't nested inside anotherfor
loop, thennil
is returned.ExampleUse theparentloop
property{% for i in (1..3) -%}{% for j in (1..3) -%}{{ forloop.parentloop.index }} - {{ forloop.index }}{%- endfor %}{%- endfor %}{% for i in (1..3) -%} {% for j in (1..3) -%} {{ forloop.parentloop.index }} - {{ forloop.index }} {%- endfor %} {%- endfor %}
Output
1 - 11 - 21 - 32 - 12 - 22 - 33 - 13 - 23 - 3
{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
Anchor to Use the `forloop` object
Use the forloop
object
forloop
object{% for page in pages -%}
{%- if forloop.length > 0 -%}
{{ page.title }}{% unless forloop.last %}, {% endunless -%}
{%- endif -%}
{% endfor %}
{% 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?