YTE: YAML Template Engine with Python Expressions
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
-
NameError: name 'my_variable' is not defined
cause A Python expression within the YAML template (e.g., `? my_variable + 1`) is trying to use a variable that was not provided in the `context` dictionary when `process_yaml` was called.fixPass `my_variable` and its value in the `context` dictionary to the `process_yaml` function. For example: `process_yaml(template, context={'my_variable': 10})`. -
SyntaxError: invalid syntax
cause The Python expression following a `?` in the YAML template contains invalid Python syntax, or a Python-specific construct (like `for` or `if`) is malformed.fixCarefully review the Python expression within the YAML template. Ensure correct indentation for blocks, proper closing of parentheses/brackets, and adherence to Python syntax rules. Use a Python linter or IDE to validate the expression in isolation if needed. -
TypeError: unsupported operand type(s) for +: 'int' and 'str'
cause A Python expression is attempting an operation (e.g., addition) between incompatible data types (e.g., an integer and a string), which is common when template variables have unexpected types.fixExplicitly cast variables to the correct type within the YTE expression (e.g., `? int(item_count) + 1`) or ensure the `context` dictionary provides variables with the expected types.
Warnings
- gotcha YTE uses '?' as the prefix for Python expressions within YAML. This is a non-standard YAML syntax extension and might conflict with YAML linters or other parsers not aware of YTE.
- breaking There have been reported compatibility issues with Python 3.12 regarding the availability of for-loop variables for substitution within YTE templates. This may lead to unexpected behavior or failures.
- gotcha YTE expressions are raw Python code. Any variable referenced within an expression (e.g., `? age > 18`) must be explicitly passed in the `context` dictionary to `process_yaml` or be a built-in Python function/value. Undefined variables will raise a `NameError`.
Install
-
pip install yte
Imports
- process_yaml
import yte.process_yaml
from yte import process_yaml
Quickstart
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)