mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-17 12:31:38 +00:00
59 lines
1.0 KiB
ReStructuredText
59 lines
1.0 KiB
ReStructuredText
``cycle``
|
|
=========
|
|
|
|
The ``cycle`` function cycles on a sequence:
|
|
|
|
.. code-block:: twig
|
|
|
|
{% set start_year = date() | date('Y') %}
|
|
{% set end_year = start_year + 5 %}
|
|
|
|
{% for year in start_year..end_year %}
|
|
{{ cycle(['odd', 'even'], loop.index0) }}
|
|
{% endfor %}
|
|
|
|
{# outputs
|
|
|
|
odd
|
|
even
|
|
odd
|
|
even
|
|
odd
|
|
even
|
|
|
|
#}
|
|
|
|
The ``cycle`` function takes two arguments: the ``sequence`` to cycle through and the ``position`` in the sequence.
|
|
|
|
The ``sequence`` must be non-empty and can contain any number of values:
|
|
|
|
.. code-block:: twig
|
|
|
|
{% set fruits = ['apple', 'orange', 'citrus'] %}
|
|
|
|
{% for i in 0..10 %}
|
|
{{ cycle(fruits, i) }}
|
|
{% endfor %}
|
|
|
|
{# outputs
|
|
|
|
apple
|
|
orange
|
|
citrus
|
|
apple
|
|
orange
|
|
citrus
|
|
apple
|
|
orange
|
|
citrus
|
|
apple
|
|
orange
|
|
|
|
#}
|
|
|
|
Arguments
|
|
---------
|
|
|
|
* ``values``: The sequence to cycle on
|
|
* ``position``: The position in the sequence
|