Skip to content
Myr

Merge pull request #956 from revelt/patch-1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Tweak docs grouper chapter

authored by David Lord · committed by GitHub · parents 8e3c0e7, d8f1eb1 · Browse files · 1 file+23 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+23 added−26 removed

  • other:+23 added−26 removed

1 file+23 added−26 removed

modified:jinja2/filters.py+23 added-26 removed
866 unmodified lines
867_GroupTuple = namedtuple('_GroupTuple', ['grouper', 'list'])868_GroupTuple.__repr__ = tuple.__repr__869_GroupTuple.__str__ = tuple.__str__870871872@environmentfilter873def do_groupby(environment, value, attribute):873 """Group a sequence of objects by a common attribute.874 """Group a sequence of objects by an attribute using Python's875 :func:`itertools.groupby`. The attribute can use dot notation for876 nested access, like ``"address.city"``. Unlike Python's ``groupby``,877 the values are sorted first so only one group is returned for each878 unique value.879875 If you for example have a list of dicts or objects that represent persons876 with `gender`, `first_name` and `last_name` attributes and you want to877 group all users by genders you can do something like the following878 snippet:880 For example, a list of ``User`` objects with a ``city`` attribute881 can be rendered in groups. In this example, ``grouper`` refers to882 the ``city`` value of the group.883884 .. sourcecode:: html+jinja885882 <ul>883 {% for group in persons|groupby('gender') %}884 <li>{{ group.grouper }}<ul>885 {% for person in group.list %}886 <li>{{ person.first_name }} {{ person.last_name }}</li>887 {% endfor %}</ul></li>888 {% endfor %}889 </ul>886 <ul>{% for city, items in users|groupby("city") %}887 <li>{{ city }}888 <ul>{% for user in items %}889 <li>{{ user.name }}890 {% endfor %}</ul>891 </li>892 {% endfor %}</ul>893891 Additionally it's possible to use tuple unpacking for the grouper and892 list:894 ``groupby`` yields namedtuples of ``(grouper, list)``, which895 can be used instead of the tuple unpacking above. ``grouper`` is the896 value of the attribute, and ``list`` is the items with that value.897898 .. sourcecode:: html+jinja899896 <ul>897 {% for grouper, list in persons|groupby('gender') %}898 ...899 {% endfor %}900 </ul>900 <ul>{% for group in users|groupby("city") %}901 <li>{{ group.grouper }}: {{ group.list|join(", ") }}902 {% endfor %}</ul>903902 As you can see the item we're grouping by is stored in the `grouper`903 attribute and the `list` contains all the objects that have this grouper904 in common.905904 .. versionchanged:: 2.6907 It's now possible to use dotted notation to group by the child908 attribute of another attribute.905 The attribute supports dot notation for nested access.906 """907 expr = make_attrgetter(environment, attribute)908 return [_GroupTuple(key, list(values)) for key, values909 in groupby(sorted(value, key=expr), expr)]
350 unmodified lines