Zope Template Application Language (TAL)
zope.tal provides an implementation of the Zope Template Application Language (TAL), a powerful and secure server-side templating system primarily used within the Zope framework but usable standalone. It enforces strict separation of presentation logic from application logic. The current stable version is 6.0, with releases typically tied to Python version support updates and Zope ecosystem advancements.
Common errors
-
ModuleNotFoundError: No module named 'zope.app.tal'
cause Attempting to import from an old package path (`zope.app.tal`) which was deprecated and removed in favor of `zope.tal` directly.fixUpdate your import statements to use `from zope.tal.template import TALTemplate` (or other relevant symbols from `zope.tal`). -
xml.etree.ElementTree.ParseError: junk after document element:
cause The TAL template contains malformed XML/HTML, such as unclosed tags, incorrect nesting, or invalid characters.fixReview your template string for well-formedness. Ensure all tags are properly closed, attributes are quoted, and HTML entities are correctly escaped. -
AttributeError: 'MyContext' object has no attribute 'missing_attribute'
cause A TAL path expression (e.g., `context/missing_attribute`) references an attribute that does not exist on the provided context object.fixEnsure that the context object passed to the `TALTemplate` instance has all the attributes or dictionary keys that the template attempts to access. Debug by inspecting the `context` object at runtime.
Warnings
- breaking Python 3.8 and older are no longer supported by `zope.tal` version 6.x.
- breaking The `zope.interface` dependency has been updated, requiring `zope.interface >= 6.0`.
- gotcha TAL templates are strict about XML/HTML well-formedness. Mismatched tags, unquoted attributes, or special characters not escaped can lead to parsing errors.
- gotcha Context path expressions (e.g., `context/title`) are case-sensitive and require attributes/items to exist on the context object or its sub-objects. Non-existent paths will lead to `AttributeError` or `KeyError` during rendering.
Install
-
pip install zope.tal
Imports
- TALTemplate
from zope.app.tal.template import TALTemplate
from zope.tal.template import TALTemplate
- HTMLTALParser
from zope.tal.htmltalparser import HTMLTALParser
Quickstart
from zope.tal.template import TALTemplate
# Define a simple TAL template
template_string = '''
<html tal:define="user_name context/name">
<head>
<title tal:content="context/page_title">Default Title</title>
</head>
<body>
<h1 tal:content="string:Hello, ${user_name}!" />
<p tal:condition="context/is_admin">Welcome, Administrator!</p>
<ul tal:repeat="item context/items">
<li tal:content="item" />
</ul>
</body>
</html>
'''
# Create a context object with data for the template
class MyContext:
def __init__(self, name, title, admin_status, item_list):
self.name = name
self.page_title = title
self.is_admin = admin_status
self.items = item_list
# Instantiate the template
template = TALTemplate(template_string)
# Create a context instance
context_data = MyContext(
name='Alice',
title='My TAL Page',
admin_status=True,
item_list=['Item 1', 'Item 2', 'Item 3']
)
# Render the template with the context
output = template(context_data)
print(output)