tempita package

A small templating language

This implements a small templating language. This language implements if/elif/else, for/continue/break, expressions, and blocks of Python code. The syntax is:

{{any expression (function calls etc)}}
{{any expression | filter}}
{{for x in y}}...{{endfor}}
{{if x}}x{{elif y}}y{{else}}z{{endif}}
{{py:x=1}}
{{py:
def foo(bar):
    return 'baz'
}}
{{default var = default_value}}
{{# comment}}

You use this with the Template class or the sub shortcut. The Template class takes the template string and the name of the template (for errors) and a default namespace. Then (like string.Template) you can call the tmpl.substitute(**kw) method to make a substitution (or tmpl.substitute(a_dict)).

sub(content, **kw) substitutes the template immediately. You can use __name='tmpl.html' to set the name of the template.

If there are syntax errors TemplateError will be raised.

exception tempita.TemplateError(message, position, name=None)[source]

Bases: exceptions.Exception

Exception raised while parsing a template

class tempita.Template(content, name=None, namespace=None, stacklevel=None, get_template=None, default_inherit=None, line_offset=0, delimiters=None)[source]

Bases: object

default_encoding = 'utf8'
default_inherit = None
default_namespace = {'end_braces': '}}', 'looper': <class 'tempita._looper.looper'>, 'start_braces': '{{'}
classmethod from_filename(filename, namespace=None, encoding=None, default_inherit=None, get_template=<function get_file_template>)[source]
substitute(*args, **kw)[source]
tempita.sub(content, delimiters=None, **kw)[source]
class tempita.HTMLTemplate(content, name=None, namespace=None, stacklevel=None, get_template=None, default_inherit=None, line_offset=0, delimiters=None)[source]

Bases: tempita.Template

default_namespace = {'attr': <function attr at 0x7fe1c4dc5c08>, 'end_braces': '}}', 'html': <class 'tempita.html'>, 'html_quote': <function html_quote at 0x7fe1c4dc56e0>, 'looper': <class 'tempita._looper.looper'>, 'start_braces': '{{', 'url': <function url at 0x7fe1c4dc5b90>}
tempita.sub_html(content, **kw)[source]
class tempita.html(value)[source]

Bases: object

class tempita.bunch(**kw)[source]

Bases: dict

Submodules

tempita.compat3 module

tempita.compat3.b

alias of __builtin__.str

tempita.compat3.basestring_

alias of __builtin__.basestring

tempita.compat3.bytes

alias of __builtin__.str

tempita.compat3.next(obj)[source]
tempita.compat3.is_unicode(obj)[source]