range

The range function returns an array containing an arithmetic progression of integers.

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

{# The above will output 0, 1, 2, 3, #}

By default it will increment by 1. If you pass a step value a the third parameter then it specifies the increment or decrement.

{% for i in range(1, 10, 2) %}
    {{ i }},
{% endfor %}

{# The above will output 1, 3, 5, 7, 9, #}

A negative step value will decrease the numbers. The first value has to be larger than the second value in order for this to work. If they are not then the step value will be made positive and the sequence will be incremented by the positive step value.

{% for i in range(50, 5, -15) %}
    {{ i }},
{% endfor %}

{# The above will output 50, 35, 20, 5, #}

If no step value is set and the start value is larger than the end value, then a step of -1 is used.

Arguments

The range function has the following signature.

range(start, end, step)

The step argument is optional.

Argument Description
start

The number to start the range at. If it is larger than the end value then the step will be negative and decrease the values. If it is less than the end value then the step will be positive and increase the values.

end

The value to end the range at. If the last value based on the step argument doesn't equal the end value, then the last value will be the second to last value.

If it is larger than the start value then the step will be positive and increase the values. If it is less than the start value then the step will be negative and decrease the values.

step

The value to increase or decrease the range by. It is optional and defaults to 1.

If start is larger than end then the step will be negative and decrease the range. If start is less than end then the step will be positive and increase the range.

< Back to the list of functions