How can I modify/merge Jinja2 dictionaries?

前端 未结 3 1585
慢半拍i
慢半拍i 2020-12-10 02:53

I have a Jinja2 dictionary and I want a single expression that modifies it - either by changing its content, or merging with another dictionary.

>>>         


        
3条回答
  •  伪装坚强ぢ
    2020-12-10 03:10

    I added a filter to merge dictionaries, namely:

    >>> def add_to_dict(x,y): return dict(x, **y)
    >>> e.filters['add_to_dict'] = add_to_dict
    >>> e.from_string("{{ x|add_to_dict({4:5}) }}").render({'x':{1:2,2:3}})
    u'{1: 2, 2: 3, 4: 5}'
    

提交回复
热议问题