template: added dict_ext filter to extend a dictionary
This commit is contained in:
parent
936843bb67
commit
0b0bb0e2c9
1 changed files with 19 additions and 0 deletions
|
|
@ -207,6 +207,7 @@ class Renderer(object):
|
|||
self.env.filters['attr_escape'] = self.attr_escape
|
||||
self.env.filters['item_filter'] = self._item_filter
|
||||
self.env.filters['adv_format'] = self._adv_format
|
||||
self.env.filters['dict_ext'] = self._dict_ext
|
||||
# custom tests
|
||||
self.env.tests['in_the_past'] = self._in_the_past
|
||||
|
||||
|
|
@ -485,6 +486,24 @@ class Renderer(object):
|
|||
# jinja use string when no special char is used, so we have to convert to unicode
|
||||
return unicode(template).format(value=value, **kwargs)
|
||||
|
||||
def _dict_ext(self, source_dict, extra_dict, key=None):
|
||||
"""extend source_dict with extra dict and return the result
|
||||
|
||||
@param source_dict(dict): dictionary to extend
|
||||
@param extra_dict(dict, None): dictionary to use to extend first one
|
||||
None to return source_dict unmodified
|
||||
@param key(unicode, None): if specified extra_dict[key] will be used
|
||||
if it doesn't exists, a copy of unmodified source_dict is returned
|
||||
@return (dict): resulting dictionary
|
||||
"""
|
||||
if extra_dict is None:
|
||||
return source_dict
|
||||
if key is not None:
|
||||
extra_dict = extra_dict.get(key, {})
|
||||
ret = source_dict.copy()
|
||||
ret.update(extra_dict)
|
||||
return ret
|
||||
|
||||
## custom tests ##
|
||||
|
||||
def _in_the_past(self, timestamp):
|
||||
|
|
|
|||
Loading…
Reference in a new issue