genshi.template.text.NewTextTemplate

Package genshi :: Package template :: Module text :: Class NewTextTemplate |

# Class NewTextTemplate

               object --+
                        |
    base.DirectiveFactory --+
                            |
                base.Template --+
                                |
                               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

Nested Classes
Inherited from `base.DirectiveFactory`: `__metaclass__`

Instance Methods
__init__(self, source, filepath=None, filename=None, loader=None, encoding=None, lookup=`'``strict``'`, allow_exec=False, delims=`(``'``{%``'``,` `'``%}``'``,` `'``{#``'``,` `'``#}``'``)`) Initialize a template from either a string, a file-like object, or an already parsed markup stream. |
Inherited from `base.Template`: `__getstate__`, `__repr__`, `__setstate__`, `generate` Inherited from `base.DirectiveFactory`: `get_directive`, `get_directive_index` Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Class Variables
| directives = ``[``(``'``def``'``,` <class 'genshi.template.directives.DefDi`...`` A list of `(name, cls)` tuples that define the set of directives provided by this factory.
| serializer = ``'``text``'`` hash(x)
Inherited from `base.Template`: `EXEC`, `EXPR`, `INCLUDE`, `SUB`

Properties
| delimiters
Inherited from `base.Template`: `stream` Inherited from `object`: `__class__`

Method Details

### __init__(self, source, filepath=None, filename=None, loader=None, encoding=None, lookup=`'``strict``'`, allow_exec=False, delims=`(``'``{%``'``,` `'``%}``'``,` `'``{#``'``,` `'``#}``'``)`) (Constructor) |
Initialize a template from either a string, a file-like object, or an already parsed markup stream. Parameters: `source` - a string, file-like object, or markup stream to read the template from `filepath` - the absolute path to the template file `filename` - the path to the template file relative to the search path `loader` - the TemplateLoader to use for loading included templates `encoding` - the encoding of the `source` `lookup` - the variable lookup mechanism; either "strict" (the default), "lenient", or a custom lookup class `allow_exec` - whether Python code blocks in templates should be allowed Overrides: object.__init__ (inherited documentation)

Class Variable Details

  ### directives A list of `(name, cls)` tuples that define the set of directives provided by this factory. Value:
      [('def', <class 'genshi.template.directives.DefDirective'>),
       ('when', <class 'genshi.template.directives.WhenDirective'>),
       ('otherwise',
        <class 'genshi.template.directives.OtherwiseDirective'>),
       ('for', <class 'genshi.template.directives.ForDirective'>),
       ('if', <class 'genshi.template.directives.IfDirective'>),
       ('choose', <class 'genshi.template.directives.ChooseDirective'>),
       ('with', <class 'genshi.template.directives.WithDirective'>)]

Property Details

  ### delimiters Get Method: _get_delims(self) Set Method: _set_delims(self, delims) Delete Method:
      '''    The delimiters for directives and comments. This should be a fo
      ur item tuple
          of the form ``(directive_start, directive_end, comment_start,
          comment_end)``, where each item is a string.
          '''