Zope Page Templates
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.
Common errors
-
ImportError: cannot import name 'TALInterpreter' from 'zope.tal'
cause The `zope.tal` and `zope.tales` packages were merged into `zope.pagetemplate` in version 5.0.fixChange your import statement to `from zope.pagetemplate.tal import TALInterpreter`. -
zope.pagetemplate.markup.xmlfile.ZPTParseError: <string> Line X, Column Y: ...
cause The template provided is not well-formed XML/HTML, or contains syntax errors that ZPT's parser cannot handle.fixReview 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. -
AttributeError: 'ContextObject' object has no attribute 'some_variable'
cause The TAL expression `context/some_variable` attempts to access an attribute that does not exist on the context object provided during rendering.fixEnsure 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. -
TypeError: PageTemplate() got an unexpected keyword argument 'source'
cause Older versions or specific usages might have expected a `source` keyword argument for template content, but the current `PageTemplate` constructor uses `text`, `filename`, or `url`.fixUse `template = PageTemplate(text=template_string)` instead of `source`. If loading from a file, use `PageTemplate(filename='path/to/template.pt')`.
Warnings
- breaking Zope Page Templates version 6.x (including 6.1) requires Python 3.10 or newer. Older Python versions are no longer supported.
- breaking The previously separate `zope.tal` and `zope.tales` packages have been integrated into `zope.pagetemplate` since version 5.0. Direct imports from the standalone `zope.tal` or `zope.tales` packages will fail.
- gotcha Zope Page Templates are strict about XML well-formedness. Minor HTML parsing errors (e.g., unclosed tags, incorrect nesting, invalid character entities) will prevent templates from rendering and raise `zope.pagetemplate.markup.xmlfile.ZPTParseError`.
- gotcha Understanding the ZPT context mechanism (`context/attribute`, `repeat`, `define`, `default`) is crucial. Misinterpreting how context objects and their attributes are exposed often leads to `KeyError` or `AttributeError` during template evaluation.
Install
-
pip install zope-pagetemplate
Imports
- PageTemplate
from zope.pagetemplate.pagetemplate import PageTemplate
- TALInterpreter
from zope.tal import TALInterpreter
from zope.pagetemplate.tal import TALInterpreter
Quickstart
from zope.pagetemplate.pagetemplate import PageTemplate
template_str = """
<html xmlns:tal="http://xml.zope.org/namespaces/tal">
<head>
<title tal:content="context/title">Default Title</title>
</head>
<body>
<h1 tal:content="context/header"></h1>
<p tal:content="context/message">Default Message</p>
<ul tal:repeat="item context/items">
<li tal:content="item"></li>
</ul>
</body>
</html>
"""
# Simulate a context object for the template
class Context:
def __init__(self, title, header, message, items):
self.title = title
self.header = header
self.message = message
self.items = items
context_data = Context(
title="My Dynamic Page",
header="Welcome to ZPT!",
message="This content is dynamically rendered.",
items=["item 1", "item 2", "item 3"]
)
# Create a PageTemplate instance with the template string
template = PageTemplate(text=template_str)
# Render the template with the provided context
output = template(context=context_data)
print(output)