1``reverse`` 2=========== 3 4.. versionadded:: 1.6 5 Support for strings has been added in Twig 1.6. 6 7The ``reverse`` filter reverses a sequence, a mapping, or a string: 8 9.. code-block:: jinja 10 11 {% for user in users|reverse %} 12 ... 13 {% endfor %} 14 15 {{ '1234'|reverse }} 16 17 {# outputs 4321 #} 18 19.. tip:: 20 21 For sequences and mappings, numeric keys are not preserved. To reverse 22 them as well, pass ``true`` as an argument to the ``reverse`` filter: 23 24 .. code-block:: jinja 25 26 {% for key, value in {1: "a", 2: "b", 3: "c"}|reverse %} 27 {{ key }}: {{ value }} 28 {%- endfor %} 29 30 {# output: 0: c 1: b 2: a #} 31 32 {% for key, value in {1: "a", 2: "b", 3: "c"}|reverse(true) %} 33 {{ key }}: {{ value }} 34 {%- endfor %} 35 36 {# output: 3: c 2: b 1: a #} 37 38.. note:: 39 40 It also works with objects implementing the `Traversable`_ interface. 41 42Arguments 43--------- 44 45* ``preserve_keys``: Preserve keys when reversing a mapping or a sequence. 46 47.. _`Traversable`: https://secure.php.net/Traversable 48