Brubeck currently supports Jinja2, Tornado, Mako or Pystache templates.
Template support is contained in brubeck.templates
as rendering handlers. Each handler will attach a render_template
function to your handler and overwrite the default render_error
to produce templated errors messages.
Using a template system is then as easy as calling render_template
with the template filename and some context, just like you're used to.
Using Jinja2 template looks like this.
from brubeck.templating import Jinja2Rendering
class DemoHandler(WebMessageHandler, Jinja2Rendering):
def get(self):
context = {
'name': 'J2D2',
}
return self.render_template('success.html', **context)
The corresponding HTML looks like this:
<html>
<head>
<title>Jinja2 Render</title>
</head>
<body>
<p>Take five, {{ name }}!</p>
</body>
</html>
In addition to using a rendering handler, you need to provide the path to your templates.
That looks like this:
from brubeck.templating import load_jinja2_env
config = {
template_loader=load_jinja2_env('./templates/jinja2')
...
}
Using a function here keeps the config lightweight and flexible.
template_loader
needs to be some function that returns an environment.
Is your favorite template system not in this list? Please take a look at the other implementations. It's probably easy to add support.