From Kid to Genshi – Changing template language

Genshi (formerly Markup) is a library that provides an integrated set of components for parsing, generating, and processing HTML, XML or other textual content for output generation on the web.

Replacing Kid with Genshi XML Template Language makes a lot of sense:

  • Better performance
  • Easier to debug
  • Uses standards like XPath and XInclude
  • TurboGears 1.1 will by default use Genshi as its template engine.

XInclude alone is a reason to use Genshi. It is an inclusion mechanism to facilitate modularity, and a recommendation by W3C.

Migrating to Genshi is very simple. Most things will work without change.

Below is an example of how i use Genshi to iterate over actions and include a template to display action details. I use the same template in other places to avoid duplication.

dev.cfg:

tg.defaultview = "genshi"

project.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="master.html"/>
<head>
...
</head>
<body>
...
    <xi:include href="action.html" py:for="action in project.actions"/>
...
</body>
</html>

action.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/">

<body py:strip="">
  <b>${action.title}</b>
  ...
</body>
About these ads

5 Responses to From Kid to Genshi – Changing template language

  1. Ian Ozsvald says:

    Here’s a quick hat-tip to you, I mention your blog (this entry is shown) in my latest ShowMeDo series. In this 2-part set I introduce useful on-line resources for new Python programmers:

    http://showmedo.com/videos/video?name=pythonOzsvaldIntroToPyResources2&fromSeriesID=27

    Cheers, and good luck with the blog,
    Ian.

  2. Matthew says:

    Thanks, that’s useful. Would you be able to share your master.html as well? Thanks.

  3. Florian says:

    If you’re interested in genshi and you also do a lot of dhtml and you don’t want to throw around html fragments between the server and the client AND you’d also don’t like assembling markup by hand in javascript then Genshi2JS could interest you :)

    http://dev.codeflow.org/trac/genshi2js

  4. C Ring says:

    Thank you for posting this! this has gone a long way toward helping me resolve my problem!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: