Chameleon

4.6.0 · active · verified Thu Apr 16

Chameleon is a fast HTML/XML template compiler for Python. It implements the Zope Page Templates language, compiling templates into optimized Python byte-code for high performance. Currently at version 4.6.0, it is actively maintained with a regular release cadence, supporting Python 3.9+ and PyPy 3.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a `PageTemplate` from a string, defining a variable using `tal:define`, interpolating variables with `${...}`, and passing a context object for dynamic data. It then renders the template and prints the resulting HTML.

from chameleon import PageTemplate

template_string = """
<div tal:define="name 'World'">
  <h1>Hello, ${name}!</h1>
  <p>Today is ${context.today.strftime('%Y-%m-%d')}.</p>
</div>
"""

template = PageTemplate(template_string)

# Render the template with a 'context' dictionary
import datetime
output = template(context={'today': datetime.date.today()})
print(output)

view raw JSON →