Skip to content
Myr

Merge pull request #1356 from BALaka-18/map-filter-default

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

Map filter can use False as default

authored by David Lord · committed by GitHub · parents ab9cd8c, 73a94e0 · Browse files · 3 files+11 added−1 removed
This commit

State

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

Diff stats

3 files+11 added−1 removed

  • tests:+8 added−0 removed
  • other:+3 added−1 removed

3 files+11 added−1 removed

modified:src/jinja2/filters.py+1 added-1 removed
65 unmodified lines
66 def attrgetter(item):67 for part in attribute:68 item = environment.getitem(item, part)6970 if default and isinstance(item, Undefined):70 if default is not None and isinstance(item, Undefined):71 item = default7273 if postprocess is not None:74 item = postprocess(item)
1323 unmodified lines
modified:tests/test_filters.py+8 added
664 unmodified lines
665 env = Environment()666 tmpl = env.from_string(667 '{{ users|map(attribute="lastname", default="smith")|join(", ") }}'668 )669 test_list = env.from_string(670 '{{ users|map(attribute="lastname", default=["smith","x"])|join(", ") }}'671 )672 test_str = env.from_string(673 '{{ users|map(attribute="lastname", default="")|join(", ") }}'674 )675 users = [676 Fullname("john", "lennon"),677 Fullname("jane", "edwards"),678 Fullname("jon", None),679 Firstname("mike"),680 ]681 assert tmpl.render(users=users) == "lennon, edwards, None, smith"682 assert test_list.render(users=users) == "lennon, edwards, None, ['smith', 'x']"683 assert test_str.render(users=users) == "lennon, edwards, None, "684685 def test_simple_select(self, env):686 env = Environment()687 tmpl = env.from_string('{{ [1, 2, 3, 4, 5]|select("odd")|join("|") }}')
89 unmodified lines
modified:CHANGES.rst+2 added
28 unmodified lines
29 URL instead of ignoring trailing characters. The parsing in general30 has been updated to be more efficient and match more cases. URLs31 without a scheme are linked as ``https://`` instead of ``http://``.32 :issue:`522, 827, 1172`, :pr:`1195`33- Filters that get attributes, such as ``map`` and ``groupby``, can34 use a false or empty value as a default. :issue:`1331`353637Version 2.11.338--------------
780 unmodified lines