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.
pip install yteVerified import paths — ran on the pinned version, not inferred.
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.
Be aware of the '?' prefix for expressions and ensure your tooling is compatible or configure it to ignore these YTE-specific constructs. YTE templates are still valid YAML, where '?' expressions are treated as strings by standard parsers.
If encountering issues on Python 3.12, check the YTE GitHub issues for a resolution or consider using Python 3.11 or earlier until a fix is released. Review template logic, especially variable scoping within loops.
Ensure all variables used in YTE expressions are present in the `context` dictionary passed to `process_yaml`. Thoroughly test templates for missing variable definitions.
Pass `my_variable` and its value in the `context` dictionary to the `process_yaml` function. For example: `process_yaml(template, context={'my_variable': 10})`.Carefully 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.
Explicitly 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.