Iteration

Iteration tags run blocks of code repeatedly.

for

Repeatedly executes a block of code. For a full list of attributes available within a for loop, see forloop (object).

Input

<ul>
{% for member in data.AllMembersByName %}
<li>{{member.FullName}}</li>
{% endfor %}
</ul>

Output

<ul>
<li>Aarit Akshai</li>
<li>Ayaan Ellis</li>
<li>Eliz Lomber</li>
<li>Kieram Graham</li>
<li>Renata Mantrum</li>
<li>Tanuki Mushinoku</li>
<li>Tara Preston</li>
</ul>

for (parameters)

limit

Limits the loop to the specified number of iterations.

Input

{% assign array = (1..6) %}
<!-- array = [1,2,3,4,5,6] -->
{% for item in array limit:2 %}
{{ item }}
{% endfor %}

Output

1 2

offset

Begins the loop at the specified index.

Input

{% assign array = (1..6) %}
<!-- array = [1,2,3,4,5,6] -->
{% for item in array offset:2 %}
{{ item }}
{% endfor %}

Output

3 4 5 6
range
 

Defines a range of numbers to loop through. The range can be defined by both literal and variable numbers.

Input

{% for i in (3..5) %}
{{ i }}
{% endfor %}
{% assign num = 4 %}
{% for i in (1..num) %}
{{ i }}
{% endfor %}

Output

3 4 5

1 2 3 4
reversed
 

Reverses the order of the loop. Note that this flag’s spelling is different from the filter reverse.

Input

{% assign array = (1..6) %}

<!-- array = [1,2,3,4,5,6] -->
{% for item in array reversed %}
{{ item }}
{% endfor %}

Output

6 5 4 3 2 1

cycle

Loops through a group of strings and prints them in the order that they were passed as arguments. Each time cycle is called, the next string argument is printed.

cycle must be used within a for loop block.

Input

{% cycle "one", "two", "three" %}
{% cycle "one", "two", "three" %}
{% cycle "one", "two", "three" %}
{% cycle "one", "two", "three" %}

Output

one
two
three
one

Uses for cycle include:

  • applying odd/even classes to rows in a table

  • applying a unique class to the last product thumbnail in a row

cycle (parameters)

cycle accepts a “cycle group” parameter in cases where you need multiple cycle blocks in one template. If no name is supplied for the cycle group, then it is assumed that multiple calls with the same parameters are one group.

Input

{% cycle "first": "one", "two", "three" %}
{% cycle "second": "one", "two", "three" %}
{% cycle "second": "one", "two", "three" %}
{% cycle "first": "one", "two", "three" %}

Output

one
one
two
two

tablerow

Generates an HTML table. Must be wrapped in opening <table> and closing </table> HTML tags.

Input

<table>
{% tablerow member in data.AllMembersByName cols: 4 %}
{{ member.FullName }}
{% endtablerow %}
</table>

Output

<table>
<tbody>
<tr class="row1">
<td class="col1">
Aarit Akshai
</td><td class="col2">
Ayaan Ellis
</td><td class="col3">
Eliz Lomber
</td><td class="col4">
Kieram Graham
</td></tr>
<tr class="row2"><td class="col1">
Renata Mantrum
</td><td class="col2">
Tanuki Mushinoku
</td><td class="col3">
Tara Preston
</td></tr>
</tbody>
</table>

tablerow (parameters)

cols

Defines how many columns the tables should have.

Input

<table>
{% tablerow member in data.AllMembersByName cols: 2 %}
{{ member.FullName }}
{% endtablerow %}
</table>

Output

<table>
<tbody><tr class="row1">
<td class="col1">
Aarit Akshai
</td><td class="col2">
Ayaan Ellis
</td></tr>
<tr class="row2"><td class="col1">
Eliz Lomber
</td><td class="col2">
Kieram Graham
</td></tr>
<tr class="row3"><td class="col1">
Renata Mantrum
</td><td class="col2">
Tanuki Mushinoku
</td></tr>
<tr class="row4"><td class="col1">
Tara Preston
</td></tr>
</tbody></table>
 range

Defines a range of numbers to loop through. The range can be defined by both literal and variable numbers.

{% assign num = 4 %}
<table>
{% tablerow i in (1..num) cols: 2 %}
{{ i }}
{% endtablerow %}
</table>
<!--literal number example-->
<table>
{% tablerow i in (3..5) cols: 2 %}
{{ i }}
{% endtablerow %}
</table>

forloop.first

Returns true if it's the first iteration of the for loop. Returns false if it is not the first iteration.

Input

{% for product in collections.frontpage.products %}
{% if forloop.first == true %}
First time through!
{% else %}
Not the first time.
{% endif %}
{% endfor %}

Output

First time through!
Not the first time.
Not the first time.
Not the first time.
Not the first time.

forloop.index

Returns the current index of the for loop, starting at 1.

Input

{% for member in data.AllMembersByName %}
{{ forloop.index }}
{% endfor %}

Output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

forloop.index0

Returns the current index of the for loop, starting at 0.

Input

{% for member in data.AllMembersByName %}
{{ forloop.index0 }}
{% endfor %}

Output

0 1 2 3 4 5 6 7

forloop.last

Returns true if it's the last iteration of the for loop. Returns false if it is not the last iteration.

Input

{% for member in data.AllMembersByName %}
{% if forloop.last == true %}
This is the last iteration!
{% else %}
Keep going...
{% endif %}
{% endfor %}

Output

Keep going...
Keep going...
Keep going...
Keep going...
Keep going...
This is the last iteration!

forloop.length

Returns the number of iterations of the loop.

Input

{% for member in data.AllMembersByName %}
{% if forloop.first %}
<p>This collection has {{ forloop.length }} members:</p>
{% endif %}
<p>{{ member.FullName }}</p>
{% endfor %}

Output

This collection has 7 members:
Aarit Akshai
Ayaan Ellis
Eliz Lomber
Kieram Graham
Renata Mantrum
Tanuki Mushinoku
Tara Preston

forloop.rindex

Returns forloop.index in reverse order.

Input

{% for member in data.AllMembersByName %}
{{ forloop.rindex }}
{% endfor %}

Output

7 6 5 4 3 2 1

forloop.rindex0

Returns forloop.index0 in reverse order.

Input

{% for member in data.AllMembersByName %}
{{ forloop.rindex0 }}
{% endfor %}

Output

6 5 4 3 2 1 0
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Please sign in to leave a comment.