I am trying to populate a YAML file with values from a dictionary in python.
from jinja2 import Environment, FileSystemLoader
import glob
def map_template(context,template_path,template_name,destination):
environment = Environment(loader = FileSystemLoader(template_path), trim_blocks=True, lstrip_blocks=True)
results_filename = destination
results_template = environment.get_template(template_name)
with open(results_filename, mode="w", encoding="utf-8") as results:
results.write(results_template.render(context))
print(f"... wrote {results_filename}")
The value in the context looks like
{'node' : {'value' : [{'key1':val1,'key2':"str2",'key3':val3}] } }
The wanted output YAML looks like
node:
- key1:val1
key2:"str2"
key3: val3
My current solution in my template
node:
- {{node.value[0].key1}}
{{node.value[0].key2}}
{{node.value[0].key3}}
But is there a way to dynamically explode the list using jinja2 template without having to hardcode the number of elements and their names?