Cheetah3 Template Engine

3.4.0.post5 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

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.

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)

view raw JSON →