Iteration


break

Break iteration of the current loop

Input

    {% for i in (1..5) %}
        {% if i == 4 %}
            {% break %}
        {% endif %}
        {{ i }}
    {% endfor %}

Output

 1
 2
 3
 4

continue

Skips the current iteration of the current loop

Input

    {% for i in (1..5) %}
        {% if i == 4 %}
            {% continue %}
        {% endif %}
        {{ i }}
    {% endfor %}

Output

 1
 2
 3
 5

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.

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 "one", "two", "three" %}
 {% cycle "one", "two", "three" %}
 {% cycle "first": "one", "two", "three" %}
 {% cycle "one", "two", "three" %}

Output

 one
 two
 one
 three

for

Repeatedly executes a block of code.

Parameters

Name Description
limit Limits the loop to the specified number of iterations.
offset Begins the loop at the specified index.

Input

 {% for product in category.products %}
   {{ product.title }}
 {% endfor %}

Output

 hat shirt pants

for tag can also have an else condition

Input

 {% for product in category.products %}
   {{ product.title }}
 {% else %}
   This category doesn't have any products
 {% endfor %}

Output

 This category doesn't have any products

You can also use break (stop iteration) or continue (skip rest of the current iteration) tags inside for block.

tablerow

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

Parameters

Name Description
cols Defines how many columns the tables should have.
limit Exits the tablerow loop after a specific index.
offset Starts the tablerow loop after a specific index.

Input

 <table>
 {% tablerow product in category.products %}
   {{ product.title }}
 {% endtablerow %}
 </table>

Output

 <table>
   <tr class="row1">
     <td class="col1">
       Product name
     </td>
   </tr>
   <tr class="row2">
     <td class="col2">
       Second product name
     </td>
   </tr>
 </table>