tempita – A small templating language

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.

Module Contents

exception tempita.TemplateError
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)
tempita.sub(content, **kw)
class tempita.bunch(**kw)

HTML Templates

class tempita.HTMLTemplate(content, name=None, namespace=None, stacklevel=None, get_template=None, default_inherit=None, line_offset=0)
class tempita.html(value)
tempita.html_quote(value, force=True)
tempita.url(v)
tempita.attr(**kw)

Table Of Contents

Previous topic

License

This Page

Quick search