The Temp class is instantiated as follows:
Temp(template, **kwargs)
Here, "template" is the complete text of a template (as returned, for instance, by file.read()). Following "template", there is a set of keyword arguments, where the left-hand-side indicates a variable name, and the right-hand-side a value. Keyword arguments can either be passed explicitly, like so:
temp = Temp(template, one=1, two=2) print temp
... or one could choose to construct an argument dictionary first:
mydict = { "one": 1, "two": 2 } temp = Temp(template, **mydict) print temp
In the rest of this manual, we will frequently refer to these keyword arguments as the "namespace" of a template. The reason for this terminology will become clear in the following sections.
The Temp object is rendered whenever its __repr__() method is called -
i.e. whenever the template needs to be converted to a string. In the example
above, this happens invisibly in the line print temp
, but it can
be done more explicitly through repr(temp)
or just plain
`temp`
, which will both return the rendered string. The template
is re-rendered every time this happens.
A Temp object can also be rendered by calling it like a function:
temp = Temp(template, one=1, two=2) print temp(one=3, two=4)
The keyword arguments passed to the call "over-ride" the keyword arguments passed during the object's instantiation.
Cubictemp also contains a small helper class named "File". It wraps the Temp class to ease a common usage scenario: loading a template from a file. The File class is instantiated as follows:
File(path, **kwargs)
The "path" argument is the full path to a Cubictemp template. The File class also makes reading exceptions a bit more convenient by pre-pending the template file path to error messages.
<--previous | contents | next--> | (11/16/04) |