cycle

The cycle function cycles through an array of values. It is useful for alternating values within a loop.

It must be used within a for loop.

{% for item in items %}
    <p class="{{ cycle(['odd', 'even'], loop.index0) }}">{{ item.itemName }}</p>
{% endfor %}

Arguments

The cycle function has the following signature.

cycle(values, position)

Both arguments are required.

Argument Description
values

Required. The array of values to cycle through.

{% for item in items %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}

The array of values can contain any number of values. It is not limited to two values.

{% set fruits = ['apple', 'orange', 'citrus'] %}

{% for i in 0..10 %}
    {{ cycle(fruits, i) }}
{% endfor %}

position

Required. The cycle position. It's used to determine which item from the values array to use.

The value should be numeric and should be zero-based (meaning the first value starts at 0).

{% for item in items %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}

The array of values can contain any number of values. It is not limited to two values.

{% for i in 0..10 %}
    {{ cycle(values, i) }}
{% endfor %}

< Back to the list of functions