{"id":10369,"library":"zope-pagetemplate","title":"Zope Page Templates","description":"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.","status":"active","version":"6.1","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/zope.pagetemplate","tags":["templating","web","zope","html","xml","tal"],"install":[{"cmd":"pip install zope-pagetemplate","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides core interface definitions used by ZPT.","package":"zope.interface","optional":false},{"reason":"Provides object location services, often relevant in Zope-style applications.","package":"zope.location","optional":false},{"reason":"Used for publishing and HTTP request handling, particularly when integrating with Zope applications.","package":"zope.publisher","optional":false}],"imports":[{"symbol":"PageTemplate","correct":"from zope.pagetemplate.pagetemplate import PageTemplate"},{"note":"Since v5.0, zope.tal and zope.tales are integrated into zope.pagetemplate.","wrong":"from zope.tal import TALInterpreter","symbol":"TALInterpreter","correct":"from zope.pagetemplate.tal import TALInterpreter"}],"quickstart":{"code":"from zope.pagetemplate.pagetemplate import PageTemplate\n\ntemplate_str = \"\"\"\n<html xmlns:tal=\"http://xml.zope.org/namespaces/tal\">\n  <head>\n    <title tal:content=\"context/title\">Default Title</title>\n  </head>\n  <body>\n    <h1 tal:content=\"context/header\"></h1>\n    <p tal:content=\"context/message\">Default Message</p>\n    <ul tal:repeat=\"item context/items\">\n      <li tal:content=\"item\"></li>\n    </ul>\n  </body>\n</html>\n\"\"\"\n\n# Simulate a context object for the template\nclass Context:\n    def __init__(self, title, header, message, items):\n        self.title = title\n        self.header = header\n        self.message = message\n        self.items = items\n\ncontext_data = Context(\n    title=\"My Dynamic Page\",\n    header=\"Welcome to ZPT!\",\n    message=\"This content is dynamically rendered.\",\n    items=[\"item 1\", \"item 2\", \"item 3\"]\n)\n\n# Create a PageTemplate instance with the template string\ntemplate = PageTemplate(text=template_str)\n\n# Render the template with the provided context\noutput = template(context=context_data)\n\nprint(output)","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your project is running on Python 3.10 or a more recent compatible version. For older Python environments, use zope.pagetemplate < 6.0.","message":"Zope Page Templates version 6.x (including 6.1) requires Python 3.10 or newer. Older Python versions are no longer supported.","severity":"breaking","affected_versions":"6.0+"},{"fix":"Update your import statements to use `from zope.pagetemplate.tal import ...` or `from zope.pagetemplate.tales import ...`.","message":"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.","severity":"breaking","affected_versions":"5.0+"},{"fix":"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.","message":"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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Change your import statement to `from zope.pagetemplate.tal import TALInterpreter`.","cause":"The `zope.tal` and `zope.tales` packages were merged into `zope.pagetemplate` in version 5.0.","error":"ImportError: cannot import name 'TALInterpreter' from 'zope.tal'"},{"fix":"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.","cause":"The template provided is not well-formed XML/HTML, or contains syntax errors that ZPT's parser cannot handle.","error":"zope.pagetemplate.markup.xmlfile.ZPTParseError: <string> Line X, Column Y: ..."},{"fix":"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.","cause":"The TAL expression `context/some_variable` attempts to access an attribute that does not exist on the context object provided during rendering.","error":"AttributeError: 'ContextObject' object has no attribute 'some_variable'"},{"fix":"Use `template = PageTemplate(text=template_string)` instead of `source`. If loading from a file, use `PageTemplate(filename='path/to/template.pt')`.","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`.","error":"TypeError: PageTemplate() got an unexpected keyword argument 'source'"}]}