Zope Page Templates (ZPT) is a powerful templating system for Python, renowned for its ability to allow designers to work directly with templates using standard XML/HTML tools. Templates are valid XML/HTML documents, making them easy to edit in visual editors. It is currently at version 6.1, actively maintained by the Zope Foundation, with updates typically coinciding with other Zope-related library releases.
pip install zope-pagetemplateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a Zope Page Template, create a Python object to serve as the template's context, and render the template, substituting dynamic data using TAL (Template Attribute Language) expressions.
Ensure your project is running on Python 3.10 or a more recent compatible version. For older Python environments, use zope.pagetemplate < 6.0.
Update your import statements to use `from zope.pagetemplate.tal import ...` or `from zope.pagetemplate.tales import ...`.
Always validate your templates for XML/HTML correctness. Ensure all tags are properly closed and nested. Tools like `lxml.html` can sometimes help clean up 'tag soup' HTML before passing it to ZPT.
Familiarize yourself with the ZPT/TAL specification regarding context lookup. Ensure the context object passed to the template rendering call (`template(context=...)`) provides all expected attributes or items that TAL expressions are attempting to access.
Change your import statement to `from zope.pagetemplate.tal import TALInterpreter`.
Review the template string at the indicated line and column for unclosed tags, incorrect nesting, invalid character entities, or other XML syntax violations. ZPT requires strict XML compliance.
Ensure that the object passed as `context` to `template()` has an attribute named `some_variable`. If it's a dictionary, access with `context/key` will also work, but Python objects are more common for attributes.
Use `template = PageTemplate(text=template_string)` instead of `source`. If loading from a file, use `PageTemplate(filename='path/to/template.pt')`.