如何修改/合并Jinja2字典? [英] How can I modify/merge Jinja2 dictionaries?

查看:480
本文介绍了如何修改/合并Jinja2字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jinja2字典,我想要一个修改它的表达式 - 通过更改其内容或与另一个字典合并。

 >>> import jinja2 
>>> e = jinja2.Environment()

修改dict:Fails。

 >>> e.from_string({{x [4] = 5}})。render({'x':{1:2,2:3}})
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
文件jinja2 / environment.py,第743行,from_string
返回cls.from_code(self,self.compile(source),全局变量,无)
文件jinja2 / environment py,第469行,编译
self.handle_exception(exc_info,source_hint = source)
文件< unknown>,第1行,模板
jinja2.exceptions.TemplateSyntaxError:expected令牌
'结束打印声明',获得'='

两阶段更新:打印多余的无。

 >>> e.from_string({{x.update({4:5}}}} {{x}})render({'x':{1:2,2:3}})
u '无{1:2,2:3,4:5}'
>>> e.from_string({{dict(x.items()+ {3:4} .items())}})render({'x':{1:2,2:3}})
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
文件jinja2 / environment.py,第868行,呈现
return self.environment.handle_exception(exc_info,True)
文件< template>,第1行,顶部-level模板代码
TypeError:<(>()正好是0个参数(1)

使用 dict(x,** y):失败。

 >>> e.from_string({{dict((3,4),** x)}})render({'x':{1:2,2:3}})
追溯(最近call last):
文件< stdin>,第1行,< module>
文件jinja2 / environment.py,第868行,呈现
return self.environment.handle_exception(exc_info,True)
文件< template>,第1行,顶部-level模板代码
TypeError:call()关键字必须是字符串

那么一个通过更改属性或与另一个字典合并来修改Jinja2中的字典 x



此问题类似于: 如何将两个Python字典合并为一个单一的表达式? - 就像Jinja2和Python类似。

解决方案

听起来像 Jinja2do语句扩展名可能有帮助。启用此扩展将允许您重写:



{{x.update({4:5}}}} {{x}}



as



{%do x .update({4:5})%} {{x}}



示例:

 >>> import jinja2 
>>> e = jinja2.Environment(extensions = [jinja2.ext.do,])
>>> e.from_string({%do x.update({4:5})%} {{x}})render({'x':{1:2,2:3}})
你'{1:2,2:3,4:5}'
>>>


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

>>> import jinja2
>>> e = jinja2.Environment()

Modify a dict: Fails.

>>> e.from_string("{{ x[4]=5 }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "jinja2/environment.py", line 743, in from_string
    return cls.from_code(self, self.compile(source), globals, None)
  File "jinja2/environment.py", line 469, in compile
    self.handle_exception(exc_info, source_hint=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token
                                            'end of print statement', got '='

Two-stage update: Prints superfluous "None".

>>> e.from_string("{{ x.update({4:5}) }} {{ x }}").render({'x':{1:2,2:3}})
u'None {1: 2, 2: 3, 4: 5}'
>>> e.from_string("{{ dict(x.items()+ {3:4}.items()) }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "jinja2/environment.py", line 868, in render
    return self.environment.handle_exception(exc_info, True)
  File "<template>", line 1, in top-level template code
TypeError: <lambda>() takes exactly 0 arguments (1 given)

Use dict(x,**y): Fails.

>>> e.from_string("{{ dict((3,4), **x) }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "jinja2/environment.py", line 868, in render
    return self.environment.handle_exception(exc_info, True)
  File "<template>", line 1, in top-level template code
TypeError: call() keywords must be strings

So how does one modify the dictionary x in Jinja2 by changing an attribute or merging with another dictionary?

This question is similar to: How can I merge two Python dictionaries as a single expression? -- insofar as Jinja2 and Python are analogous.

解决方案

Sounds like the Jinja2 "do" statement extension may help. Enabling this extension would allow you to rewrite:

"{{ x.update({4:5}) }} {{ x }}"

as

"{% do x.update({4:5}) %} {{ x }}".

Example:

>>> import jinja2
>>> e = jinja2.Environment(extensions=["jinja2.ext.do",])
>>> e.from_string("{% do x.update({4:5}) %} {{ x }}").render({'x':{1:2,2:3}})
u' {1: 2, 2: 3, 4: 5}'
>>> 

这篇关于如何修改/合并Jinja2字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆