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.
pip install Flask-MakoTemplates
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:
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 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')
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
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)
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)
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.
Renders a template from the template folder with the given context.
Parameters: |
|
---|
Renders a template from the given template source string with the given context.
Parameters: |
|
---|
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: |
|
---|