From: jeff Date: Sat, 13 Jun 2020 23:30:27 +0000 (-1000) Subject: update wording on dictionary default ordering X-Git-Tag: 3.0.0rc1~13^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fa20045b0d2bf06f11a2bab005d9bd986525eda;p=thirdparty%2Fjinja.git update wording on dictionary default ordering --- diff --git a/docs/templates.rst b/docs/templates.rst index 6226468d..1826bbcb 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -688,9 +688,17 @@ iterate over containers like `dict`:: {% endfor %} -Note, however, that **Python dicts are not ordered**; so you might want to -either pass a sorted ``list`` of ``tuple`` s -- or a -``collections.OrderedDict`` -- to the template, or use the `dictsort` filter. +Python dicts may not be in the order you want to display them in. If +order matters, use the ``|dictsort`` filter. + +.. code-block:: jinja + +
+ {% for key, value in my_dict | dictsort %} +
{{ key|e }}
+
{{ value|e }}
+ {% endfor %} +
Inside of a for-loop block, you can access some special variables: diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index 5ff0bdde..af9a4524 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -307,9 +307,8 @@ def do_dictsort( by: 'te.Literal["key", "value"]' = "key", reverse: bool = False, ) -> "t.List[t.Tuple[K, V]]": - """Sort a dict and yield (key, value) pairs. Because python dicts are - unsorted you may want to use this function to order them by either - key or value: + """Sort a dict and yield (key, value) pairs. Python dicts may not + be in the order you want to display them in, so sort them first. .. sourcecode:: jinja