Adding a default parameter to builtin map filter
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Brendan · committed by David Lord · parent e214048 · Browse files · 3 files·+32 added−2 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+32 added−2 removed
- tests:+16 added−0 removed
- other:+16 added−2 removed
Provenance
- Commit
8b09b03- Tree
0faa9e5- Author
Brendan <brendancq@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
e214048
3 files changed+32 added−2 removed
modified:jinja2/filters.py+14 added-2 removed
57 unmodified lines58 to lowercase and returns other types as-is."""59 return value.lower() if isinstance(value, string_types) else value60 61 62-def make_attrgetter(environment, attribute, postprocess=None):62+def make_attrgetter(environment, attribute, postprocess=None, default=None):63 """Returns a callable that looks up the given attribute from a64 passed object with the rules of the environment. Dots are allowed65 to access attributes of attributes. Integer parts in paths are66 looked up as integers.8 unmodified lines75 def attrgetter(item):76 for part in attribute:77 item = environment.getitem(item, part)78 79+ if default and isinstance(item, Undefined):80+ item = default81+ break82+83 if postprocess is not None:84 item = postprocess(item)85 86 return item877 unmodified lines964 .. sourcecode:: jinja965 966 Users on this page: {{ users|map(attribute='username')|join(', ') }}967 968+ If the list of objects may not contain the given attribute, a default969+ value may be provided.970+971+ .. sourcecode:: jinja972+973+ {{ users|map(attribute='username', default='Anonymous')|join(', ') }}974+975 Alternatively you can let it invoke a filter by passing the name of the976 filter and the arguments afterwards. A good example would be applying a977 text conversion filter on a sequence:978 129 unmodified lines1108 seq = args[1]1109 1110 if len(args) == 2 and 'attribute' in kwargs:1111 attribute = kwargs.pop('attribute')1112+ default = kwargs.pop('default', None)1113 if kwargs:1114 raise FilterArgumentError('Unexpected keyword argument %r' %1115 next(iter(kwargs)))1104- func = make_attrgetter(context.environment, attribute)1116+ func = make_attrgetter(context.environment, attribute, default=default)1117 else:1118 try:1119 name = args[2]1120 args = args[3:]96 unmodified linesmodified:tests/test_filters.py+16 added
559 unmodified lines560 ]561 tmpl = env.from_string('{{ users|map(attribute="name")|join("|") }}')562 assert tmpl.render(users=users) == 'john|jane|mike'563 564+ def test_attribute_map_default(self, env):565+ class User(object):566+ def __init__(self, name):567+ self.name = name568+ class NotUser(object):569+ def __init__(self, not_name):570+ self.not_name = not_name571+ env = Environment()572+ users = [573+ User('john'),574+ User('jane'),575+ NotUser('plant'),576+ ]577+ tmpl = env.from_string('{{ users|map(attribute="name", default="anonymous")|join("|") }}')578+ assert tmpl.render(users=users) == 'john|jane|anonymous'579+580 def test_empty_map(self, env):581 env = Environment()582 tmpl = env.from_string('{{ none|map("upper")|list }}')583 assert tmpl.render() == '[]'100 unmodified linesmodified:CHANGES.rst+2 added
12 unmodified lines13 :class:`~environment.Environment` enables it, in order to avoid a14 slow initial import. (`#765`_)15 - Python 2.6 and 3.3 are not supported anymore.16 - The `map` filter in async mode now automatically awaits17+- Added `default` parameter for the `map` filter. (`#985`_)18 19 .. _#765: https://github.com/pallets/jinja/issues/76520+.. _#985: https://github.com/pallets/jinja/pull/98521 22 23 Version 2.10.124 --------------600 unmodified lines