Templates

Templates are useful for rendering html for use in Mail or Webapp2 Handlers. Ferris provides a thin wrapper around jinja2.

Rendering templates

Templates can be easily rendered by using render():

from ferris3 import template

context = {"greeting": "Hello!"}
result = template.render("app/email/welcome.html", context)

Notice that the template path is relative to the root of the application.

The environment

The jinja2 environment is available via environment():

temp_template = template.environment().from_string("<h1>{{greeting}}</h1>")
result = temp_template.render({"greenting": "hello"})

You can also add globals, filters, extensions, etc.:

template.environment().globals['user'] = endpoints.get_current_user()
template.environment().filters['format_date'] = lambda x: x.strftime("%d-%m-%Y")
template.environment().add_extension("jinja2.ext.autoescape")

Typically you’d do this in some auto-loaded file such as appengine_config.py, main.py, or app/settings.py.