Cheetah3 Template Engine
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+.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including dropping support for Python versions older than 2.7 and requiring recompilation of all previously compiled Cheetah templates.
- deprecated The standalone `cheetah-compile` command-line program is deprecated.
- gotcha A security issue (arbitrary code execution) was reported. Caution is advised when processing templates from untrusted sources without proper sanitization or sandboxing.
- gotcha Under Python 3, `Cheetah.ImportHooks` now correctly raises `ModuleNotFoundError` instead of `ImportError` when a module cannot be found. Code that specifically catches `ImportError` for missing template modules might need updating.
Install
-
pip install ct3
Imports
- Template
from Cheetah.Template import Template
Quickstart
from Cheetah.Template import Template
import datetime
# Define a simple template string
template_code = """
#set $current_year = datetime.date.today().year
Hello, $name!
This template was rendered in $current_year using $engine.
"""
# Create a template instance and pass data
t = Template(template_code, searchList=[
{'name': 'Registry User', 'engine': 'Cheetah3'}
])
# Render the template
output = str(t)
print(output)