Global Functions

The following functions are available in the global scope by default:

range

Returns a list containing an arithmetic progression of integers. When step is given, it specifies the increment (or decrement):

{% for i in range(0, 3) %}
    {{ i }},
{% endfor %}

{# returns 0, 1, 2, 3 #}

{% for i in range(0, 6, 2) %}
    {{ i }},
{% endfor %}

{# returns 0, 2, 4, 6 #}

Tip

The range function works as the native PHP range function.

The .. operator is a syntactic sugar for the range function (with a step of 1):

{% for i in 0..10 %}
    {{ i }},
{% endfor %}

cycle

The cycle function can be used to cycle on an array of values:

{% for i in 0..10 %}
    {{ cycle(['odd', 'even'], i) }}
{% endfor %}

The array can contain any number of values:

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

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

constant

constant returns the constant value for a given string:

{{ some_date|date(constant('DATE_W3C')) }}