Genshi (templating language)
XML-based template engine in Python
- 1 min read
- 2 chapters
Related on Lably
Through Python (programming language) and HTML
Genshi can be used with several Python web frameworks, such as CherryPy, TurboGears, Pylons and web2py. Genshi was the default templating language for TurboGears from versions 1.1 to 2.3.8.
Genshi markup
Genshi makes use of namespaces to embed instructions into HTML. A typical instruction is given as an attribute, with a Python expression inside the quotes. For example, the following will render a paragraph that shows 4:
<span id="mwLA"></span><span class="nt" id="mwLQ"><html</span> <span class="na" id="mwLg">xmlns=</span><span class="s" id="mwLw">"http://www.w3.org/1999/xhtml"</span> <span class="na" id="mwMA">xmlns:py=</span><span class="s" id="mwMQ">"http://genshi.edgewall.org/"</span><span class="nt" id="mwMg">></span> <span class="nt" id="mwMw"><body></span> <span class="nt" id="mwNA"><p</span> <span class="na" id="mwNQ">py:content=</span><span class="s" id="mwNg">"</span><span class="mi" id="mwNw">2</span> <span class="o" id="mwOA">+</span> <span class="mi" id="mwOQ">2</span><span class="s" id="mwOg">"</span><span class="nt" id="mwOw">></span>This will be replaced with 4<span class="p" id="mwPA"></</span><span class="nt" id="mwPQ">p</span><span class="p" id="mwPg">></span> <span class="p" id="mwPw"></</span><span class="nt" id="mwQA">body</span><span class="p" id="mwQQ">></span> <span class="p" id="mwQg"></</span><span class="nt" id="mwQw">html</span><span class="p" id="mwRA">></span>
Because of the use of namespaces, Genshi can be used in WYSIWYG HTML editors.
Sign in to keep reading
One more chapter is free with a Lably account.
Differences between Kid and Genshi
- Genshi directly interprets templates (unlike Kid, which generates Python code)
- Genshi uses XInclude for template reuse
- Genshi adds attributes Kid does not have, like
py:choose - Genshi templates are easier to debug, because it tracks template source file names and line numbers, and errors from Genshi's interpreter produce more comprehensible stack traces than from Kid's generated code.

