This module provides Mako template support for webapp2.

To use it, you must add the mako package to your application directory (for App Engine) or install it in your virtual environment (for other servers).

You can download mako from PyPi:

Learn more about Mako:

webapp2_extras.mako.default_config = {'template_path': 'templates'}

Default configuration values for this module. Keys are:

Directory for templates. Default is templates.
class webapp2_extras.mako.Mako(app, config=None)[source]

Wrapper for configurable and cached Mako environment.

To used it, set it as a cached property in a base RequestHandler:

import webapp2

from webapp2_extras import mako

class BaseHandler(webapp2.RequestHandler):

    def mako(self):
        # Returns a Mako renderer cached in the app registry.
        return mako.get_mako(

    def render_response(self, _template, **context):
        # Renders a template and writes the result to the response.
        rv = self.mako.render_template(_template, **context)

Then extended handlers can render templates directly:

class MyHandler(BaseHandler):
    def get(self):
        context = {'message': 'Hello, world!'}
        self.render_response('my_template.html', **context)
render_template(_filename, **context)[source]

Renders a template and returns a response object.

  • _filename – The template filename, related to the templates directory.
  • context – Keyword arguments used as variables in the rendered template. These will override values set in the request context.

A rendered template.

webapp2_extras.mako.get_mako(factory=<class 'webapp2_extras.mako.Mako'>, key='webapp2_extras.mako.Mako', app=None)[source]

Returns an instance of Mako from the app registry.

It’ll try to get it from the current app registry, and if it is not registered it’ll be instantiated and registered. A second call to this function will return the same instance.

  • factory – The callable used to build and register the instance if it is not yet registered. The default is the class Mako itself.
  • key – The key used to store the instance in the registry. A default is used if it is not set.
  • app – A webapp2.WSGIApplication instance used to store the instance. The active app is used if it is not set.
webapp2_extras.mako.set_mako(mako, key='webapp2_extras.mako.Mako', app=None)[source]

Sets an instance of Mako in the app registry.

  • store – An instance of Mako.
  • key – The key used to retrieve the instance from the registry. A default is used if it is not set.
  • request – A webapp2.WSGIApplication instance used to retrieve the instance. The active app is used if it is not set.

Previous topic


Next topic

Extra routes

This Page