Skip to content
Myr

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

3 files+32 added−2 removed

modified:jinja2/filters.py+14 added-2 removed
57 unmodified lines
58 to lowercase and returns other types as-is."""59 return value.lower() if isinstance(value, string_types) else value606162def make_attrgetter(environment, attribute, postprocess=None):62def 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 lines
75 def attrgetter(item):76 for part in attribute:77 item = environment.getitem(item, part)7879 if default and isinstance(item, Undefined):80 item = default81 break8283 if postprocess is not None:84 item = postprocess(item)8586 return item
877 unmodified lines
964 .. sourcecode:: jinja965966 Users on this page: {{ users|map(attribute='username')|join(', ') }}967968 If the list of objects may not contain the given attribute, a default969 value may be provided.970971 .. sourcecode:: jinja972973 {{ users|map(attribute='username', default='Anonymous')|join(', ') }}974975 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 lines
1108 seq = args[1]11091110 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 lines
modified:tests/test_filters.py+16 added
559 unmodified lines
560 ]561 tmpl = env.from_string('{{ users|map(attribute="name")|join("|") }}')562 assert tmpl.render(users=users) == 'john|jane|mike'563564 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'579580 def test_empty_map(self, env):581 env = Environment()582 tmpl = env.from_string('{{ none|map("upper")|list }}')583 assert tmpl.render() == '[]'
100 unmodified lines
modified:CHANGES.rst+2 added
12 unmodified lines
13 :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`_)1819.. _#765: https://github.com/pallets/jinja/issues/76520.. _#985: https://github.com/pallets/jinja/pull/985212223Version 2.10.124--------------
600 unmodified lines