genshi.template.base
Basic templating functionality.
TemplateError
Base exception class for errors related to template processing.
TemplateSyntaxError
Exception raised when an expression in a template causes a Python syntax error, or the template is not well-formed.
BadDirectiveError
Exception raised when an unknown directive is encountered when parsing a template.
An unknown directive is any attribute using the namespace for directives, with a local name that doesn't match any registered directive.
TemplateRuntimeError
Exception raised when an the evaluation of a Python expression in a template causes an error.
Context
Container for template input data.
A context provides a stack of scopes (represented by dictionaries).
Template directives such as loops can push a new scope on the stack with data that should only be available inside the loop. When the loop terminates, that scope can get popped off the stack again.
>>> ctxt = Context(one='foo', other=1) >>> ctxt.get('one') 'foo' >>> ctxt.get('other') 1 >>> ctxt.push(dict(one='frost')) >>> ctxt.get('one') 'frost' >>> ctxt.get('other') 1 >>> ctxt.pop() {'one': 'frost'} >>> ctxt.get('one') 'foo'
get(self, key, default=None)
Get a variable's value, starting at the current scope and going upward.
param key: the name of the variable param default: the default value to return when the variable is not found keys(self)
Return the name of all variables in the context.
return: a list of variable names items(self)
Return a list of (name, value) tuples for all variables in the context.
return: a list of variables push(self, data)
Push a new scope on the stack.
param data: the data dictionary to push on the context stack. pop(self)
Pop the top-most scope from the stack.
TemplateMeta
Meta class for templates.
Template
Abstract template base class.
This class implements most of the template processing model, but does not specify the syntax of templates.
generate(self, **args, *kwargs)
Apply the template to the given context data.
Any keyword arguments are made available to the template as context data.
Only one positional argument is accepted: if it is provided, it must be an instance of the Context class, and keyword arguments are ignored. This calling style is used for internal processing.
return: a markup event stream representing the result of applying the template to the context data.
See ApiDocs, Documentation