YTE: YAML Template Engine with Python Expressions

1.9.4 · active · verified Thu Apr 16

YTE is a template engine for the YAML format that leverages YAML's inherent structure to embed and evaluate Python expressions dynamically. This allows for conditional logic, loops, and arbitrary Python code directly within YAML documents, rendering them into plain YAML. It aims to provide a more 'YAML-native' templating experience compared to general-purpose engines like Jinja2. The library is currently at version 1.9.4 and is actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `process_yaml` to render a YAML template containing embedded Python expressions. Expressions are prefixed with `?` and are evaluated in the provided context.

from yte import process_yaml
import os

# Example YAML content with YTE expressions
yaml_template = """
name: John Doe
age: ? 30 + 5  # Simple Python expression
is_adult: ? age > 18
items:
  ? for i in range(2):
    - id: ? i + 1
      value: Item ? i + 1
"""

# Context (variables) for the template. In a real scenario, this might come from
# a file, environment variables, or a Python dictionary.
context = {
    "username": os.environ.get('YTE_USERNAME', 'guest')
}

processed_yaml = process_yaml(yaml_template, context=context)
print(processed_yaml)

view raw JSON →