Flask-MakoTemplates

The Flask-MakoTemplates extension provides Mako Templates support for Flask.

Based on code from flask-mako, flask-genshi and flask itself, it allows for templates written in Mako to be used just like Jinja2 templates: it will look for templates in the same directories, provides the same API and integrates with Flask-Babel for internationalization.

Installation

pip install Flask-MakoTemplates

Usage

Configuration

You can use the following parameters in your config to configure Mako’s TemplateLookup object, which simply map to that class’s constructor parameters of the same names:

  • MAKO_INPUT_ENCODING
  • MAKO_OUTPUT_ENCODING
  • MAKO_MODULE_DIRECTORY
  • MAKO_COLLECTION_SIZE
  • MAKO_IMPORTS
  • MAKO_FILESYSTEM_CHECKS

Registration

The two registration methods are supported:

from flask import Flask
from flaskext.makotemplates import MakoTemplates

app = Flask(__name__)
mako = MakoTemplates(app)

Or, you can use the init_app() method.

Rendering

Rendering is done exactly the same way as with Jinja2 templates. The Mako templates’ context is enriched by the same mechanism as the Jinja2 templates, meaning that the same flask variables are available throughout your Mako templates, including g, session, etc.

from flaskext.makotemplates import render_template

def hello_mako():
    return render_template('hello.html', name='mako')

Babel integration

Flask-MakoTemplates will detect Flask-Babel if it is used by the application and will automatically add the appropriate imports when creating the TemplateLookup object

API

class makotemplates.MakoTemplates(app=None)

Main class for bridging mako and flask. We try to stay as close as possible to how Jinja2 is used in flask, while at the same time surfacing the useful stuff from Mako.

Here’s how to initialize the extension.

app = Flask(__name__)
mako = MakoTemplates(app)
init_app(app)

Initialize a Flask application for use with this extension. Useful for the factory pattern but not needed if you passed your application to the MakoTemplates constructor.

mako = MakoTemplates()

app = Flask(__name__)
mako.init_app(app)
template_lookup

Returns a TemplateLookup instance that looks for templates from the same places as Flask, ie. subfolders named ‘templates’ in both the app folder and its blueprints’ folders.

If flask-babel is installed it will add support for it in the templates by adding the appropriate imports clause.

makotemplates.render_template(template_name, **context)

Renders a template from the template folder with the given context.

Parameters:
  • template_name – the name of the template to be rendered
  • context – the variables that should be available in the context of the template.
makotemplates.render_template_string(source, **context)

Renders a template from the given template source string with the given context.

Parameters:
  • source – the sourcecode of the template to be rendered
  • context – the variables that should be available in the context of the template.
makotemplates.render_template_def(template_name, def_name, **context)

Renders a specific def from a given template from the template folder with the given context. Useful for implementing this AJAX pattern:

http://techspot.zzzeek.org/2008/09/01/ajax-the-mako-way

Parameters:
  • template_name – the name of the template file containing the def to be rendered
  • def_name – the name of the def to be rendered
  • context – the variables that should be available in the context of the template.
Fork me on GitHub