Edgewall Software

ApiDocs/genshi.template.text

Version 1 (modified by cmlenz, 7 years ago)

--

genshi.template.text

Plain text templating engine.

This module implements two template language syntaxes, at least for a certain transitional period. OldTextTemplate (aliased to just TextTemplate) defines a syntax that was inspired by Cheetah/Velocity. NewTextTemplate on the other hand is inspired by the syntax of the Django template language, which has more explicit delimiting of directives, and is more flexible with regards to white space and line breaks.

In a future release, OldTextTemplate will be phased out in favor of NewTextTemplate, as the names imply. Therefore the new syntax is strongly recommended for new projects, and existing projects may want to migrate to the new syntax to remain compatible with future Genshi releases.

NewTextTemplate

Implementation of a simple text-based template engine. This class will replace OldTextTemplate in a future release.

It uses a more explicit delimiting style for directives: instead of the old style which required putting directives on separate lines that were prefixed with a # sign, directives and commenbtsr are enclosed in delimiter pairs (by default {% ... %} and {# ... #}, respectively).

Variable substitution uses the same interpolation syntax as for markup languages: simple references are prefixed with a dollar sign, more complex expression enclosed in curly braces.

>>> tmpl = NewTextTemplate('''Dear $name,
...
... {# This is a comment #}
... We have the following items for you:
... {% for item in items %}
...  * ${'Item %d' % item}
... {% end %}
... ''')
>>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
Dear Joe,
<BLANKLINE>
<BLANKLINE>
We have the following items for you:
<BLANKLINE>
 * Item 1
<BLANKLINE>
 * Item 2
<BLANKLINE>
 * Item 3
<BLANKLINE>
<BLANKLINE>

By default, no spaces or line breaks are removed. If a line break should not be included in the output, prefix it with a backslash:

>>> tmpl = NewTextTemplate('''Dear $name,
...
... {# This is a comment #}\
... We have the following items for you:
... {% for item in items %}\
...  * $item
... {% end %}\
... ''')
>>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
Dear Joe,
<BLANKLINE>
We have the following items for you:
 * 1
 * 2
 * 3
<BLANKLINE>

Backslashes are also used to escape the start delimiter of directives and comments:

>>> tmpl = NewTextTemplate('''Dear $name,
...
... \{# This is a comment #}
... We have the following items for you:
... {% for item in items %}\
...  * $item
... {% end %}\
... ''')
>>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
Dear Joe,
<BLANKLINE>
{# This is a comment #}
We have the following items for you:
 * 1
 * 2
 * 3
<BLANKLINE>
since:version 0.5

OldTextTemplate

Legacy implementation of the old syntax text-based templates. This class is provided in a transition phase for backwards compatibility. New code should use the NewTextTemplate class and the improved syntax it provides.

>>> tmpl = OldTextTemplate('''Dear $name,
...
... We have the following items for you:
... #for item in items
...  * $item
... #end
...
... All the best,
... Foobar''')
>>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
Dear Joe,
<BLANKLINE>
We have the following items for you:
 * 1
 * 2
 * 3
<BLANKLINE>
All the best,
Foobar


See Genshi Text Template Language, ApiDocs, Documentation