Chameleon
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
-
ModuleNotFoundError: No module named 'chameleon'
cause The 'chameleon' package is not installed in the current Python environment.fixRun `pip install chameleon` to install the library. Ensure you are in the correct virtual environment if you are using one. -
ERROR: Could not find a version that satisfies the requirement chameleon (from versions: none)
cause This usually indicates an incompatibility between the requested package version and your Python version, or a corrupted pip cache.fixVerify your Python version meets the `chameleon` requirements (>=3.9 for current versions) using `python --version`. Upgrade `pip` (`pip install --upgrade pip`), clear the pip cache, or try specifying a compatible version of `chameleon` if you cannot upgrade Python. -
chameleon.exc.ExpressionError: ('...', ...)cause An error occurred during the evaluation of a Python expression within the template. The traceback will usually point to the specific expression and context.fixExamine the full traceback provided by `chameleon.exc.ExpressionError`. It often includes the template's filename, line, and column where the error originated, along with the problematic expression and variable scope. Debug the Python expression within your template. -
IndentationError: expected an indented block
cause Although Chameleon compiles to Python, complex `tal:` attribute logic, `metal:` macros, or inline Python blocks might lead to malformed generated Python code if the template syntax is incorrect or ambiguous.fixCarefully review the `tal:`, `metal:`, or inline Python syntax in your template around the reported line. Ensure proper nesting and valid logical constructs as interpreted by the Page Templates language.
Warnings
- breaking Chameleon 4.x dropped support for Python versions earlier than 3.9.
- breaking Backslash-escaping of dollar-based string interpolation is no longer supported. To render a literal dollar sign, use `$$`.
- gotcha Chameleon uses Python as the default expression language, unlike traditional Zope Page Templates which might default to path expressions.
- gotcha Template variable names starting with two or more leading underscores are disallowed as they are reserved for internal compiler use. Also, certain Python built-ins (e.g., `int`, `float`, `str`, `None`, `True`, `False`) cannot be redefined within templates.
- deprecated The `literal_false` flag has been removed. Use `boolean_attributes` for similar behavior regarding boolean attributes.
Install
-
pip install chameleon
Imports
- PageTemplate
from chameleon import PageTemplate
- PageTemplateFile
from chameleon import PageTemplateFile
- PageTemplateLoader
from chameleon import PageTemplateLoader
Quickstart
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)