Tempita

0.6.0 · maintenance · verified Thu Apr 16

Tempita is a very small, simple text templating language designed for text substitution, not as a full-fledged web templating engine. It's suitable for generating Python files, configuration files, or other text-based content when `string.Template` is insufficient, but a heavier solution like Jinja is overkill. The current version is 0.6.0, which is Python 3 only. The project is explicitly in maintenance mode, not actively developed for new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both the `Template` class for creating reusable template objects and the `sub` function for immediate string substitution. It also includes an example using Tempita's control flow (`for`, `if`) and inline Python blocks (`py:`).

from tempita import Template, sub

# Using the Template class
tmpl = Template("Hello {{name}}! The answer is {{2 * 3}}.")
output_class = tmpl.substitute(name='World')
print(f"Class Output: {output_class}")

# Using the sub shortcut for immediate substitution
output_shortcut = sub("Welcome, {{user}}!", user='Alice')
print(f"Shortcut Output: {output_shortcut}")

# Example with control flow (if/for) and Python blocks
complex_template = Template("""
<ul>
{{for item in items}}
  <li>{{item.upper()}}{{if loop.last}} (last){{endif}}</li>
{{endfor}}
</ul>
{{py: x = 10}}
The value of x is {{x}}.
""")
output_complex = complex_template.substitute(items=['apple', 'banana', 'cherry'])
print("\nComplex Output:")
print(output_complex)

view raw JSON →