Cheetah3 is a free and open-source template engine and code-generation tool for Python. It is a fork of the original CheetahTemplate library, designed to compile templates into optimized, readable Python code. It blends Python's power and flexibility with a simple template language. The library is actively maintained and supports Python 2.7 and Python 3.4+.
pip install ct3Verified import paths — ran on the pinned version, not inferred.
Demonstrates defining a template string, injecting data via a search list, and rendering the output. It also shows using Python imports and `#set` directives within the template.
Ensure your Python environment meets the minimum requirement (2.7 or >=3.4) and recompile all templates after upgrading to Cheetah3 version 3.0.0 or later.
Use `cheetah compile` instead, which provides the same functionality.
Always treat templates from untrusted sources as potentially malicious. Implement sandboxing or strict input validation if dynamic template loading is necessary.
Update exception handling to catch `ModuleNotFoundError` for module-not-found scenarios, especially when dealing with dynamic template imports.
Ensure the variable is passed into the template's `searchList` or `namespaces` during instantiation, or check for its existence using `$varExists('variableName')` before using it in the template.Review the template file for incorrect Python syntax in code blocks or improper use of Cheetah directives. For example, direct Python `break` statements are not allowed in `<% ... %>` blocks that are meant for expressions or simple statements.
Ensure a C compiler (like `gcc` on Linux or appropriate build tools on Windows) is installed and available in your system's PATH. Then, reinstall `ct3` using `pip install --no-binary :none: ct3` to force a source build and attempt to compile the C extensions.
Compile your `.tmpl` files into `.py` files using `cheetah compile yourTemplate.tmpl` and ensure the directory containing these compiled `.py` files is in `sys.path`. Alternatively, configure Cheetah's `ImportHooks` to allow direct import from `.tmpl` files.