blob: a1f0e7c097d5ed4afe937a181cc95754406f5514 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``range``
2=========
3
4Returns a list containing an arithmetic progression of integers:
5
6.. code-block:: twig
7
8 {% for i in range(0, 3) %}
9 {{ i }},
10 {% endfor %}
11
12 {# outputs 0, 1, 2, 3, #}
13
14When step is given (as the third parameter), it specifies the increment (or
15decrement for negative values):
16
17.. code-block:: twig
18
19 {% for i in range(0, 6, 2) %}
20 {{ i }},
21 {% endfor %}
22
23 {# outputs 0, 2, 4, 6, #}
24
25.. note::
26
27 Note that if the start is greater than the end, ``range`` assumes a step of
28 ``-1``:
29
30 .. code-block:: twig
31
32 {% for i in range(3, 0) %}
33 {{ i }},
34 {% endfor %}
35
36 {# outputs 3, 2, 1, 0, #}
37
38The Twig built-in ``..`` operator is just syntactic sugar for the ``range``
39function (with a step of ``1``, or ``-1`` if the start is greater than the end):
40
41.. code-block:: twig
42
43 {% for i in 0..3 %}
44 {{ i }},
45 {% endfor %}
46
47.. tip::
48
49 The ``range`` function works as the native PHP `range`_ function.
50
51Arguments
52---------
53
54* ``low``: The first value of the sequence.
55* ``high``: The highest possible value of the sequence.
56* ``step``: The increment between elements of the sequence.
57
58.. _`range`: https://secure.php.net/range