{"id":7072,"library":"chameleon","title":"Chameleon","description":"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.","status":"active","version":"4.6.0","language":"en","source_language":"en","source_url":"https://github.com/malthe/chameleon","tags":["templating","html","xml","zope","pagetemplates","compiler","web","performance"],"install":[{"cmd":"pip install chameleon","lang":"bash","label":"Latest stable version"}],"dependencies":[],"imports":[{"note":"For creating templates directly from a string.","symbol":"PageTemplate","correct":"from chameleon import PageTemplate"},{"note":"For loading templates from a file path.","symbol":"PageTemplateFile","correct":"from chameleon import PageTemplateFile"},{"note":"For loading multiple templates from a directory, relative to a base path.","symbol":"PageTemplateLoader","correct":"from chameleon import PageTemplateLoader"}],"quickstart":{"code":"from chameleon import PageTemplate\n\ntemplate_string = \"\"\"\n<div tal:define=\"name 'World'\">\n  <h1>Hello, ${name}!</h1>\n  <p>Today is ${context.today.strftime('%Y-%m-%d')}.</p>\n</div>\n\"\"\"\n\ntemplate = PageTemplate(template_string)\n\n# Render the template with a 'context' dictionary\nimport datetime\noutput = template(context={'today': datetime.date.today()})\nprint(output)","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your project runs on Python 3.9 or newer. Upgrade your Python environment if necessary.","message":"Chameleon 4.x dropped support for Python versions earlier than 3.9.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Replace `\\$` with `$$` in your templates if you need a literal dollar sign.","message":"Backslash-escaping of dollar-based string interpolation is no longer supported. To render a literal dollar sign, use `$$`.","severity":"breaking","affected_versions":">=4.0.0 (exact version unclear, but around 3.x to 4.x transition)"},{"fix":"Be aware that expressions within `tal:` attributes (e.g., `tal:content=\"expression\"`) will be evaluated as Python code. If you previously relied on Zope's path expressions without explicit `python:` prefixes, you might need to adjust your syntax.","message":"Chameleon uses Python as the default expression language, unlike traditional Zope Page Templates which might default to path expressions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid using variable names with two or more leading underscores (e.g., `__my_var__`). Do not attempt to redefine Python built-in types or keywords within your template's scope.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate your template configuration to use the `boolean_attributes` option instead of `literal_false`. Consult the official documentation for `boolean_attributes` usage.","message":"The `literal_false` flag has been removed. Use `boolean_attributes` for similar behavior regarding boolean attributes.","severity":"deprecated","affected_versions":">=4.0.0 (removed around 4.0.0)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install chameleon` to install the library. Ensure you are in the correct virtual environment if you are using one.","cause":"The 'chameleon' package is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'chameleon'"},{"fix":"Verify 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.","cause":"This usually indicates an incompatibility between the requested package version and your Python version, or a corrupted pip cache.","error":"ERROR: Could not find a version that satisfies the requirement chameleon (from versions: none)"},{"fix":"Examine 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.","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.","error":"chameleon.exc.ExpressionError: ('...', ...)"},{"fix":"Carefully 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.","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.","error":"IndentationError: expected an indented block"}]}