Zope Page Templates

6.1 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

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.

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)

view raw JSON →