Edit on GitHub
Jump to docs navigation

Twig Components / Methods / reverse

Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?

reverse(preserveKeys = false) is a Twig filter to reverse a sequence, a mapping, or a string:

{% for user in users|reverse %}
    ...
{% endfor %}

{{ '1234'|reverse }}

{# outputs 4321 #}

Tip: For sequences and mappings, numeric keys are not preserved. To reverse them as well, pass true as an argument to the reverse filter:

{% for key, value in {1: "a", 2: "b", 3: "c"}|reverse %}
{{ key }}: {{ value }}
{%- endfor %}

{# output: 0: c    1: b    2: a #}

{% for key, value in {1: "a", 2: "b", 3: "c"}|reverse(true) %}
{{ key }}: {{ value }}
{%- endfor %}

{# output: 3: c    2: b    1: a #}

Note: It also works with objects implementing the Traversable interface.

ArgumentsΒΆ

  • preserve_keys: Preserve keys when reversing a mapping or a sequence.

Source: Twig



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.