gpt4 book ai didi

python - 在 Mako 模板中使用字典

转载 作者:太空狗 更新时间:2023-10-29 21:35:28 24 4
gpt4 key购买 nike

而不是像这样将变量传递给模板:

template.render(var1='hello', var2='world')

如何将字典传递给模板并让它以相同的方式呈现

vars = {'var1': 'hello', 'var2': 'world'}

所以在模板中我可以正常显示变量:

${var1} ${var2}

我不想在模板中添加任何额外的代码,所以我想以某种方式使用 Context 对象,但我遇到了困难。有什么想法吗?

最佳答案

我不知道 mako,但是使用字典作为 keyword arguments (或 kwargs),你必须在前面加上两个 *:

template.render(**vars)

关于python - 在 Mako 模板中使用字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843428/

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