gpt4 book ai didi

python - Jinja2 扩展多个关键字参数

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:18 28 4
gpt4 key购买 nike

我找不到如何将多个命名参数传递给 jinja2 扩展。我想要这样的东西:

{% some_extension foo='foo' bar='bar' %}
data
{% end_someextension %}

最佳答案

这就是我克服这个问题的方法:

{% exttag 'main', scope='global', auto='root' %}{% endexttag %}

解析方法:

def parse(self, parser):
lineno = parser.stream.__next__().lineno
context = nodes.ContextReference()
key = parser.parse_expression()
parser.stream.skip_if('comma')
if parser.stream.skip_if('name:scope'):
parser.stream.skip(1)
scope = parser.parse_expression()
else:
scope = nodes.Const('page')
parser.stream.skip_if('comma')
if parser.stream.skip_if('name:auto'):
parser.stream.skip(1)
auto = parser.parse_expression()
else:
auto = nodes.Const(None)
args = [key,scope,auto,context]
body = parser.parse_statements(['name:endexttag'], drop_needle=True)
return nodes.CallBlock(self.call_method('_render_block', args),
[], [], body).set_lineno(lineno)

可能有一种更优雅的方法来实现这一点,但这对我来说很有效,而且不会过于复杂。如果您希望模板编写者能够以任何顺序指定关键字,则必须使用循环。

但这绝对允许您在扩展中使用关键字参数。

关于python - Jinja2 扩展多个关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16635145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com