Skip to content
Myr

Merge pull request #985 from bkiu/557-map-default

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

Adding a default parameter to builtin map filter

authored by David Lord · committed by GitHub · parents 04cc41e, 8178361 · Browse files · 2 files+5 added−6 removed
This commit

State

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

Diff stats

2 files+5 added−6 removed

  • other:+5 added−6 removed

2 files+5 added−6 removed

modified:jinja2/filters.py+4 added-5 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, default=None, 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.
3 unmodified lines
70 def attrgetter(item):71 for part in attribute:72 item = environment.getitem(item, part)7374 if isinstance(item, Undefined) and default:74 if default and isinstance(item, Undefined):75 item = default7677 if postprocess is not None:78 item = postprocess(item)
932 unmodified lines
1011 does not have the given attribute.10121013 .. sourcecode:: jinja10141015 Users on this page: {{ users|map(attribute="username", default="Anonymous")|join(", ") }}1015 {{ users|map(attribute="username", default="Anonymous")|join(", ") }}10161017 Alternatively you can let it invoke a filter by passing the name of the1018 filter and the arguments afterwards. A good example would be applying a1019 text conversion filter on a sequence:
134 unmodified lines
1154 default = None11551156 if len(args) == 2 and 'attribute' in kwargs:1157 attribute = kwargs.pop('attribute')1158 if 'default' in kwargs:1159 default = kwargs.pop('default')1158 default = kwargs.pop('default', None)1159 if kwargs:1160 raise FilterArgumentError('Unexpected keyword argument %r' %1161 next(iter(kwargs)))1162 func = make_attrgetter(context.environment, attribute, default=default)
100 unmodified lines
modified:CHANGES.rst+1 added-1 removed
16 unmodified lines
17- Added a new ``ChainableUndefined`` class to support getitem18 and getattr on an undefined object. (`#977`_)19- Allow ``{%+`` syntax (with NOP behavior) when20 ``lstrip_blocks == False`` (`#748`_)21- Added ``default`` param for ``map`` filter. (`#557`_)21- Added a ``default`` parameter for the ``map`` filter. (`#557`_)2223.. _#557: https://github.com/pallets/jinja/issues/55724.. _#765: https://github.com/pallets/jinja/issues/76525.. _#748: https://github.com/pallets/jinja/issues/748
613 unmodified lines