Features

  1. Looks clean, we don't want to replace python mess with strange template language mess
  2. Easy to learn - close as possible to Python
  3. Fast - also for CGI.
  4. Auto escaping of input, and an easy way to disable escaping for raw html. Needed for some parts like credits.
  5. General - text template that can be used for everything is more useful then special purpose xml based. For example, we can use templates to create email messages or css files from a set of colors

XML or text?

With XML templates otherwise you have valid documents while editing/designing. Also look at: HOWTO Avoid Being Called a Bozo When Producing XML for other pro's using xml-based templates - btw if using Genshi, you can choose easy between xml-based and text-based templates/output.

Current theme code style

For reference, code using current theme style:

def foo(self, title, stylesheet):
    """ Few lines of useless docstring...  """
    # some logic, temps...
    result = [
        '<html>',
        '<head>',
        '<title>%s</title>' % wikiutil.escape(title),
        ]
    if stylesheet:
        result.append('<link rel="stylesheet" href="%s" type="text/css" />' % stylesheet
    result.append('</head>')
    return ''.join(result)

Most code is less clear, you have to jump between methods to find the place each html fragment is created.

Note that you have to escape everything, except stuff that is raw html. Finding what should be escaped is quite hard, require a trip to wikiutil.send_title, where d is created.

Text based templates

Can be used for anything - html, mail, code generation.

PTL / Qpy

PTL - part of quixote framework. This is the most pythonic template system, because it is actually 99% Python.

Qpy is newer standalone package based on PTL. It has better unicode support and easier to integrate into projects that does not use Quixote.

This is a big improvement over the current style, but compared with webpy, the template contains a lot of Python junk.

def header [html] (title, stylesheet):
    "<html>"
    "<head>"
    "<title>%s</title>" % title
    if stylesheet:
        '<link rel="stylesheet" href="%s" type="text/css" />' % stylesheet
    "</head>"

The title will be escaped automatically. There is also an easy way to prevent escaping of raw html (htmltext(rawhtml)).

Web.py

web.py templator debian easy to learn, pythonic. It produce the cleanest looking templates

def with (title, stylesheet=None)
<html>
<head>
    <title>$title</title>
    $if stylesheet:
        <link rel="stylesheet" href="$stylesheet" type="text/css" />
</head>

Note the (optional) smart indentation of conditional parts, without need to end of block markers.

Also, everything is automatically escaped, and there is a simple way to prevent escaping of raw html ($:rawhtml).

Issues: tempaltes are cached in RAM, but not compiled to bytecode, so CGI may need to use pickled tempaltes.

Mako

Mako - newer system similar to cheetah. Faster then webpy.templator - its considered (one of) the fastest templating engines in Python. Like cheeta and others, the syntax is less pythonic, and require end of block markers. Templates are compiled to byte code, similar to moin text_html formatter files.

<html>
<head>
<title>${title}</title>
% if stylesheet:
    <link rel="stylesheet" href="${stylesheet}" type="text/css" />
%endif
</head>

Again input is automatically escaped. However, I could not find a way to disable escaping of single variables, unless moving to explicit esacaping. This feature is needed for some parts of moin themes, for example the credits list.

Cheetah

Cheetah - more complicated, mature. The syntax is similar to Mako, but more ugly - too much $ everywhere.

Issues: I looked into the code - mess.

Django

Django template - ugly syntax.

XML based templates

Good for designers, which can use the template as is to work on the design.

Genshi

Genshi - inspired by kid. kid original author thinks it is better then kid. A new feature is support for text based templates.

Related links

MoinMoin: TemplateSystems (last edited 2007-10-29 19:22:01 by localhost)