gpt4 book ai didi

python - 如何读取 jinja 上下文变量

转载 作者:行者123 更新时间:2023-11-30 23:48:14 25 4
gpt4 key购买 nike

在我的主线中,我想在渲染模板后从当前模板上下文中读取变量。该变量已被“设置”bij 模板。我可以在上下文函数中访问该变量,但如何在我的主线中访问它。

result = template.render({'a' : "value-1" })
# in the template {% set b = "value-2" %}
b = ?

更新:我在 webapp2 源代码中找到了解决方案。该行是:

b = template.module.b

最佳答案

我发现,在 webapp2-extras 源的帮助下,可以在 python 主线中访问当前的 jinja 上下文。另请参阅:jinja 文档中的 jinja2.Template 类。

Python 主线:

result = template.render({'a' : "value-1" })
# in the template {% set b = "value-2" %}
b = template.module.b

感谢您的帮助。

关于python - 如何读取 jinja 上下文变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8035006/

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