EmPy Templating System

4.2.1 · active · verified Wed Apr 15

EmPy is a powerful, robust, and mature templating system for Python that allows embedding Python code directly into template text. It processes source documents containing special markup (default: '@') to produce output. The library is currently at version 4.2.1 and has an active development status, with recent updates modernizing its features and Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use EmPy's `expand` function to process a template string with embedded Python expressions and statements. The template uses '@(expression)' for inline evaluation and '@{...}' for Python code blocks. A dictionary is passed as the `globals_` argument to provide the context for template variables.

from em import expand

template_string = '''Hello, @(name)!\nYour lucky number is @(2 * 3 * 7).\n@{for item in items:}\n  - @item\n@{}\n'''

context = {
    'name': 'World',
    'items': ['apple', 'banana', 'cherry']
}

rendered_output = expand(template_string, globals_=context)
print(rendered_output)

view raw JSON →