Zope Template Application Language (TAL)

6.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a TAL template, create a Python object to serve as the template context, and render the template using `TALTemplate`.

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)

view raw JSON →