{"id":8793,"library":"yte","title":"YTE: YAML Template Engine with Python Expressions","description":"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.","status":"active","version":"1.9.4","language":"en","source_language":"en","source_url":"https://github.com/yte-template-engine/yte","tags":["yaml","template-engine","python-expressions","configuration-management","automation"],"install":[{"cmd":"pip install yte","lang":"bash","label":"Install YTE"}],"dependencies":[{"reason":"YTE requires Python 3.8 or newer, but not Python 4.0 or newer.","package":"Python","optional":false}],"imports":[{"note":"The primary function to process YAML templates is directly available under the 'yte' package, not a submodule 'yte.process_yaml'.","wrong":"import yte.process_yaml","symbol":"process_yaml","correct":"from yte import process_yaml"}],"quickstart":{"code":"from yte import process_yaml\nimport os\n\n# Example YAML content with YTE expressions\nyaml_template = \"\"\"\nname: John Doe\nage: ? 30 + 5  # Simple Python expression\nis_adult: ? age > 18\nitems:\n  ? for i in range(2):\n    - id: ? i + 1\n      value: Item ? i + 1\n\"\"\"\n\n# Context (variables) for the template. In a real scenario, this might come from\n# a file, environment variables, or a Python dictionary.\ncontext = {\n    \"username\": os.environ.get('YTE_USERNAME', 'guest')\n}\n\nprocessed_yaml = process_yaml(yaml_template, context=context)\nprint(processed_yaml)","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"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.","severity":"breaking","affected_versions":"Potentially 1.9.x on Python 3.12"},{"fix":"Ensure all variables used in YTE expressions are present in the `context` dictionary passed to `process_yaml`. Thoroughly test templates for missing variable definitions.","message":"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`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Pass `my_variable` and its value in the `context` dictionary to the `process_yaml` function. For example: `process_yaml(template, context={'my_variable': 10})`.","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.","error":"NameError: name 'my_variable' is not defined"},{"fix":"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.","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.","error":"SyntaxError: invalid syntax"},{"fix":"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.","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.","error":"TypeError: unsupported operand type(s) for +: 'int' and 'str'"}]}