Skip to Content

Iteration Tags

These tags are used to loop over collections and arrays in Liquid templates. They allow you to repeat content for each item in a collection, making it easy to display lists and tables. Use these tags to iterate over products, categories, or any array of data.

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

NameDescription
limitLimits the loop to the specified number of iterations.
offsetBegins 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

NameDescription
colsDefines how many columns the tables should have.
limitExits the tablerow loop after a specific index.
offsetStarts the tablerow loop after a specific index.

Input

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

Output

<table> <tr className="row1"> <td className="col1"> Product name </td> </tr> <tr className="row2"> <td className="col2"> Second product name </td> </tr> </table>
Last updated on