Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dictionaries in Mako templates

Tags:

python

mako

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?

like image 784
Adam Pointer Avatar asked Jul 27 '11 11:07

Adam Pointer


People also ask

How do I use Mako template in Python?

The simplest way to use Mako in your Python project is through the provided Template class. from mako. template import Template tmp = Template("hello ${name}") print(tmp. render(name="world!"))

What is a .Mako file?

Developer file used by Mako, a Python template engine; may contain both text and statements written in the Mako language; compiled into a Python module, which can be invoked in a Python program to output a filled template; most often used for auto-generating Web pages.

How do you comment in Mako?

Comments. Comments come in two varieties. The single line comment uses ## as the first non-space characters on a line: ## this is a comment. ...text ...


1 Answers

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

template.render(**vars)
like image 74
Jacob Avatar answered Sep 22 '22 16:09

Jacob