Previous Chapter | Next Chapter | Up | Next Section | Contents

Alternate Python String Format Syntax

This section describes the extended Python string format syntax. The extended Python string format syntax is used by the Python classes DocumentTemplate.String and DocumentTemplate.File. This format is based on the insertion-by-name format strings of Python with additional format characters, '[' and ']' to indicate block boundaries. In addition, attributes may be used within formats to control how insertion is done. For example:

%(date fmt=DayOfWeek upper)s

causes the contents of variable 'date' to be inserted using custom format 'DayOfWeek' and with all lower case letters converted to upper case.

Document template strings use an extended form of python string formatting. To insert a named value, simply include text of the form:


where 'name' is the name of the value and ' x ' is a format specification, such as '12.2d' . To introduce a block such as an 'if' or an 'in' or a block continuation, such as an 'else', use '[' as the format specification. To terminate a block, use ']' as the format specification, as in:

%(if input_name)[
Hello %(var input_name size=16 etc="...")s.
%(elif nick_name)[
Hi %(var nick_name capitalize)s.
Have we been introduced?

The form:


is a short-hand for :

%(var name)x

In most cases, the tag name, 'var' can be omitted. It must be included when:

- The variable to be inserted is named 'var ' and

- when using the expr attribute:

%(var expr="foo+1")s

Previous Chapter | Next Chapter | Up | Next Section | Contents