Using Dictionaries in Mako Templates

Instead of passing variables to the template as follows:

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

How to pass a dictionary to a template and make it the same way

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

so in the template I can display the variables as usual:

 ${var1} ${var2} 

I don't need extra code in the template, so I thought about using the Context object somehow, but I hit a brick wall. Any ideas?

+7
source share
1 answer

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

 template.render(**vars) 
+10
source

All Articles