开发者

Using dictionaries in Mako templates

开发者 https://www.devze.com 2023-03-23 11:09 出处:网络
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

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 c开发者_C百科ode in the template so I was thinking of using the Context object somehow, but I have hit a brick wall. Any ideas?


I don't know mako, but to use a dict as keyword arguments (or kwargs), you have to prepend two *:

template.render(**vars)
0

精彩评论

暂无评论...
验证码 换一张
取 消