Table Of Contents

Genshi Templating


Genshi is a stream-based XML templating system, heavily inspired by Kid. Major improvements are better tracebacks and faster execution. There are a few differences in what features are implemented, but most (TurboGears) projects can be converted from Kid to Genshi fairly painlessly, as the syntax is largely the same.

Project site:
Buffet plug-in package:
None (part of the Genshi package)
What Works:
Master template replacement, Widgets in templates, Normal templating
What Doesn’t:
Widget Templates (but works with ToscaWidgets)


Installation through easy_install and the Cheeseshop works as usual:

easy_install Genshi

This also adds the appropriate plug-in hooks into the system so that Genshi is available as a Buffet template plug-in.

Quickstart Template

A quickstart template for TruboGears projects using Genshi templates is available as a separate TurboGears extension through the Cheeseshop as well:

easy_install gsquickstart

This also includes a master.html template, which provides all the functionality of the master.kid and sitetemplate.kid files in a project with Kid templates.

Genshi vs. Kid

The most important difference is the way how master templates work, since there is no py:extends or py:layout directive in Genshi. Instead it supports a sub.set of the XInclude standard and included templates with proper py:match directives can be used as base templates.

Another difference is that Genshi templates should have a .html file extension instead of .kid. For example, if you specify


a file named foobar.html will be searched in the Python module named example.templates.

Also, there will be no compiled versions with a .pyc extension for Genshi templates.

Because the architecture is very similar to Kid, Genshi can handle widgets seamlessly and will most likely replace Kid as the official TurboGears templating library in a future TurboGears release.

The project website has a full set of documentation and an SVN checkout provides a working example TG application that uses Genshi throughout.


This TurboGears Genshi templating example takes in a list of students and a passing_score. Each student has a name, a list of scores (assumed to be the same length for all students), and an average. The template renders these students into a table, tagging failing scores with the CSS class failing and handling the case when there are no students.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<!--! This would be saved as projectname/templates/students.html -->

<html xmlns=""

  <!--! Slightly different from Kid, all includes are done with XIncludes -->
  <xi:include href="master.html" />

    <title>Template Example</title>

    <div py:choose="len(students)" py:strip="True">
      <table class="students" py:when="0">
          <th>No Students Found</th>
      <table class="students" py:otherwise="">
          <th>Student Name</th>
          <th colspan="${len(students[0].scores)}">Scores</th>
        <tr py:for="student in students">
          <th py:content="">Student Name</th>
          <span py:for="score in student.scores" py:choose="" py:strip="True">
            <td class="failing" py:when="score < passing_score">${score}</td>
            <td py:otherwise="">${score}</td>

          <!--! alternative way of handling the test,
          yay for the and/or hack -->
          <td class="average ${student.average < passing_score and 'failing' or ''}">${student.average}</td>

Generating other formats than HTML

By default Genshi will generate HTML output. Please note that Genshi does not treat “XHTML” as HTML. The effect is that template code like

<html xmlns=""
<br />

will be changed in the output. The resulting page which is delivered to the browser looks like this:


This behavior causes problems if you want to validate your XHTML pages. Fortunately, you can specify output format explicitely. One possibility is to do it for a single function as it is shown on the @expose() page:

@expose(template="project.templates.welcome", format='xhtml')
def welcome():

But using the method above, the global default would stay html. To change the global default use TurboGears’ configuration mechanism. The option genshi.outputformat will control what the default output format is. Valid values are:

  • html (default)
  • xhtml
  • xml and
  • text

You may want to change the option genshi.default_doctype too. The valid values are specified in the Genshi documentation.

genshi.default_doctype = 'xhtml-transitional'
genshi.outputformat = 'xhtml'