简体   繁体   English

在Mako模板中使用词典

[英]Using dictionaries in Mako templates

Instead of passing variables to a template like so: 而不是像这样将变量传递给模板:

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

How can I pass a dictionary to the template and have it render in the same manner 如何将字典传递给模板并以相同的方式呈现

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

so in the template I can display the variables as normal: 所以在模板中我可以正常显示变量:

${var1} ${var2}

I don't want any extra code in the template so I was thinking of using the Context object somehow, but I have hit a brick wall. 我不希望模板中有任何额外的代码,所以我想以某种方式使用Context对象,但我已经碰到了一堵砖墙。 Any ideas? 有任何想法吗?

我不知道mako,但是要使用dict作为关键字参数 (或kwargs),你必须在前面加上两个*

template.render(**vars)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM