Just grab it from PyPI with easy_install or pip, for example:

$ easy_install Flask-Genshi

If you’re starting a new project you don’t need to explicitly install Flask as Flask-Genshi depends on it already.

How to Use

You need to construct a Genshi with your Flask instance.

from flaskext.genshi import Genshi

app = Flask(__name__)
genshi = Genshi(app)

The best way to render templates is to use render_response(). This ensures that the proper mimetype is sent if you render XHTML, XML or text, and sets the right doctype for you.

Use it like so:

from flaskext.genshi import render_response

def index():
    title = 'Genshi + Flask, a match made in heaven!'
    return render_response('index.html', dict(title=title))

Using Methods

Methods control things such as the doctype and how end tags are rendered, and with render_response() also the mimetype. Unless overridden the method used is decided by the template’s filename extension.

By default HTML renders as strict HTML 4.01. This is how you change it to HTML5:

genshi.extensions['html'] = 'html5'

I recommend against this but of course you can also change it to XHTML:

genshi.extensions['html'] = 'xhtml'

You can also override the default with a parameter to the templating functions:

render_response('video.html', method='html5')

You can add and edit any methods and extensions you like, here are the defaults:

Method Extension Template Language Output
'html' .html XML based HTML 4.01 Strict
'html5'   XML based HTML 5
'xhtml'   XML based XHTML 1.0 Strict
'xml' .xml XML based application/xml
'svg' .svg XML based SVG 1.1
'text' .txt Text based text/plain
'js' .js Text based application/javascript
'css' .css Text based text/css

Filtering template streams

You can apply filters to templates that you render globally per rendering method:

from genshi.filters import Transformer

def prepend_title(template):
    return template | Transformer('head/title').prepend('MySite - ')

Alternatively, since version 0.5, you can apply a filter for a template as you render it, more repetitive but with more control:

render_response('index.html', filter=prepend_title)

See the Genshi documentation for more filters you can use.

New in version 0.3.

Changed in version 0.5: Filters can now optionally take a second argument for the context.

Changed in version 0.5: You can now apply filters on a per-render basis.

Table Of Contents

Related Topics