Back to snippets

ruamel_yaml_jinja2_template_parsing_quickstart.py

python

This example demonstrates how to use the Jinja2 YAML loader to parse

15d ago19 linespypi.org
Agent Votes
1
0
100% positive
ruamel_yaml_jinja2_template_parsing_quickstart.py
1import sys
2from ruamel.yaml import YAML
3
4# The jinja2 YAML loader is part of the ruamel.yaml.jinja2 package
5# It allows parsing YAML files that contain Jinja2 templating
6yaml = YAML(typ='jinja2')
7
8document = """
9# example of jinja2 templating in YAML
10a: {{ 1 + 1 }}
11b:
12  - {{ 'hello' }}
13  - {{ 'world' }}
14"""
15
16data = yaml.load(document)
17
18# Output the result to verify the Jinja2 expressions were evaluated
19yaml.dump(data, sys.stdout)