Zope Page Templates

The package provides a complete implementation of the Zope Page Templates (ZPT) language.

Zope Page Templates (ZPT) is a system which can generate HTML and XML. ZPT is formed by the Template Attribute Language (TAL), the Expression Syntax (TALES), Intertionalization (I18N) and the Macro Expansion Template Attribute Language (METAL).

Extensions

The ZPT implementation is largely compatible with the reference implementation. Below is an overview of notable differences:

Default expression

The default expression is python:. Path expressions are not supported in the base package. The package introduces the import: expression which imports global names.

Tuple unpacking

The tal:define and tal:repeat clauses supports tuple unpacking:

tal:define="(a, b, c) [1, 2, 3]"

The star character is not supported.

Dot-notation for dictionary lookups

If attribute lookup fails (i.e. the dot operator), dictionary lookup is tried. The engine replaces attribute lookups with a call to a function that has the following body:

try:
    return context.key
except AttributeError:
    try:
        return context[key]
    except KeyError:
        raise AttributeError(key)

Interpolation is supported

The Genshi expression interpolation syntax is supported outside tags and inside static attributes:

<span class="hello-${'world'}">
   Hello, ${'world'}!
</span>

Literal insertion

If objects for insertion provide an __html__ method, it will be called and the result inserted literally, without escaping.

With Zope 2

The five.pt package brings the Chameleon template engine to Zope 2 (see the project page for five.pt). It’s a drop-in replacement, providing bridges to the most common API.

If your application uses the CMF (e.g. Plone), use the cmf.pt package in addition (see the project page for cmf.pt).

Add the package dependency and include the following ZCML-snippet:

<include package="five.pt" />

Or:

<include package="cmf.pt" />

With Zope Toolkit

If you’re looking to use Chameleon with the Zope Toolkit, the z3c.pt package is a drop-in replacement of the reference implementation zope.pagetemplate.

API reference

This section contains an autogenerated API reference.

The PageTemplate* constructors create templates from XML files.

class chameleon.zpt.template.PageTemplate(body, parser=None, **kwargs)

Base template class.

Note: template language implementations will usually make a subclass available for convenience.

A language parser must be provided as parser. The template body parameter must be a string.

Most language parsers support two modes of operation: XML and text mode. This can be configured using the format parameter (provide either ‘text’ or ‘xml’).

The default string type is unicode. Pass a value for encoding if you need to allow strings that are not unicode or plain ASCII. Note that the output will always be unicode.

To enable default attribute prefix rendering, use the omit_default_prefix parameter (required for certain legacy applications).

To provide a custom translation function, use the translate parameter.

The debug flag can be set to True to enable detailed error reporting. This is recommended for development, but should be disabled in production.

class chameleon.zpt.template.PageTemplateFile(filename, parser=None, **kwargs)

Constructs a template object using the template language defined by parser. Must be passed an absolute (or current-working-directory-relative) filename as filename. If auto_reload is true, each time the template is rendered, it will be recompiled if it has been changed since the last rendering.

The debug flag can be set to True to enable detailed error reporting. This is recommended for development, but should be disabled in production.

class chameleon.zpt.template.PageTextTemplate(body, parser=None, **kwargs)

Base template class.

Note: template language implementations will usually make a subclass available for convenience.

A language parser must be provided as parser. The template body parameter must be a string.

Most language parsers support two modes of operation: XML and text mode. This can be configured using the format parameter (provide either ‘text’ or ‘xml’).

The default string type is unicode. Pass a value for encoding if you need to allow strings that are not unicode or plain ASCII. Note that the output will always be unicode.

To enable default attribute prefix rendering, use the omit_default_prefix parameter (required for certain legacy applications).

To provide a custom translation function, use the translate parameter.

The debug flag can be set to True to enable detailed error reporting. This is recommended for development, but should be disabled in production.

class chameleon.zpt.template.PageTextTemplateFile(filename, parser=None, **kwargs)

Base template class.

Note: template language implementations will usually make a subclass available for convenience.

A language parser must be provided as parser. The template body parameter must be a string.

Most language parsers support two modes of operation: XML and text mode. This can be configured using the format parameter (provide either ‘text’ or ‘xml’).

The default string type is unicode. Pass a value for encoding if you need to allow strings that are not unicode or plain ASCII. Note that the output will always be unicode.

To enable default attribute prefix rendering, use the omit_default_prefix parameter (required for certain legacy applications).

To provide a custom translation function, use the translate parameter.

The debug flag can be set to True to enable detailed error reporting. This is recommended for development, but should be disabled in production.

A template loader class is provided (for use with Pylons and other platforms).

class chameleon.zpt.loader.TemplateLoader(search_path=None, auto_reload=False, parser=None, translate=None)

Table Of Contents

Previous topic

Chameleon

Next topic

Template Attribute Language (TAL)

This Page