ruamel.yaml.jinja2

0.2.7 · maintenance · verified Sun Apr 12

ruamel.yaml.jinja2 is a plugin for ruamel.yaml that enables pre- and post-processing of Jinja2 templates within YAML files. This allows YAML files containing Jinja2 constructs to be loaded, modified as YAML, and then dumped back while preserving the Jinja2 templating. The current version is 0.2.7, released in September 2021, indicating a slow release cadence or maintenance mode.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates loading a YAML string that contains Jinja2 directives. It then modifies a value within the loaded data structure and dumps it back to `stdout`, preserving the Jinja2 template elements. It is crucial to use `YAML(typ='jinja2')` to enable the pre- and post-processing and use the *same* `YAML` instance for both loading and dumping.

import sys
from ruamel.yaml import YAML

yaml_str = """
- {{ name }}: "{% include 'ethnicity.jinja2' with context %}"
  age: 43
hobbies: {% include 'hobbies.jinja2' with context %}
"""

yaml = YAML(typ='jinja2')
yaml.preserve_quotes = True

data = yaml.load(yaml_str)
data[0]['age'] = 18

yaml.dump(data, sys.stdout)

view raw JSON →