Integration with Flask

Using Jinja filters and tests

Flask-Genshi supports tests and filters from Jinja. Filters are exposed in the top-level namespace as-is, tests with is prepended to their name. Both are also accessible unchanged under the filters and tests namespaces.

<p class="${'even' if iseven(1) else 'odd'}">
  ${truncate('Hello World', 10)}

Same thing:

<p class="${'even' if tests.even(1) else 'odd'}">
  ${filters.truncate('Hello World', 10)}


<p class="odd">
  Hello ...

Changed in version 0.6: Top-level access.

Module templates

Flask can load templates specific to a flask.Module and Flask-Genshi also supports this. It works just like in Flask:


This will first look for, e.g. app/templates/modname/template.html, and if not found, e.g. app/mods/modname/templates/template.html. This lets you override module templates in your application.

New in version 0.4.

Context processors

Context processors work as expected.

def add_site_name():
    return dict(site_name=app.config['SITE_NAME'])

This lets you use site_name in templates without including it in every render call.


New in version 0.4.

Render from strings

Like flask.render_template_string(), Flask-Genshi lets you render strings as templates:

render_response(string='Hello $name', context={'name': 'World'}, method='text')

The same pattern applies to all functions.

New in version 0.4.

Signal support

Flask-Genshi supports a signal similar to flask.template_rendered called template_generated. It is sent the template object and the context that was used to generate the template stream, when one is successfully generated.

New in version 0.5.