... | ... |
@@ -8,7 +8,7 @@ The ``reverse`` filter reverses a sequence, a mapping, or a string: |
8 | 8 |
|
9 | 9 |
.. code-block:: jinja |
10 | 10 |
|
11 |
- {% for use in users|reverse %} |
|
11 |
+ {% for user in users|reverse %} |
|
12 | 12 |
... |
13 | 13 |
{% endfor %} |
14 | 14 |
|
... | ... |
@@ -16,8 +16,32 @@ The ``reverse`` filter reverses a sequence, a mapping, or a string: |
16 | 16 |
|
17 | 17 |
{# outputs 4321 #} |
18 | 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"}|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"}|reverse(true) %} |
|
33 |
+ {{ key }}: {{ value }} |
|
34 |
+ {%- endfor %} |
|
35 |
+ |
|
36 |
+ {# output: 3: c 2: b 1: a #} |
|
37 |
+ |
|
19 | 38 |
.. note:: |
20 | 39 |
|
21 | 40 |
It also works with objects implementing the `Traversable`_ interface. |
22 | 41 |
|
42 |
+Arguments |
|
43 |
+--------- |
|
44 |
+ |
|
45 |
+ * ``preserve_keys``: Preserve keys when reversing a mapping or a sequence. |
|
46 |
+ |
|
23 | 47 |
.. _`Traversable`: http://php.net/Traversable |