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?
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!"))
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.
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 ...
I don't know mako, but to use a dict as keyword arguments (or kwargs), you have to prepend two *
:
template.render(**vars)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With