Adam Pointer
Adam Pointer

Reputation: 1492

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. Any ideas?

Upvotes: 8

Views: 3960

Answers (1)

Jacob
Jacob

Reputation: 43219

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

template.render(**vars)

Upvotes: 13

Related Questions