Application specific YAML tag of Jinja2 template
Project description
jinjyaml
Application specific tag of Jinja2 template in PyYAML.
It may be useful if you only want to render special tag nodes in the document, instead of whole YAML string as a template.
Usage
Example 1
>>> import yaml
>>> import jinjyaml as jy
>>>
>>> ctor = jy.Constructor()
>>> yaml.add_constructor('!j2', ctor, yaml.FullLoader)
>>>
>>> s = '''
... array:
... !j2 |
... {% for i in range(n) %}
... - sub{{i}}: {{loop.index}}
... {% endfor %}
... '''
>>>
>>> obj = yaml.full_load(s)
>>>
>>> data = jy.extract(obj, context={'n': 3})
>>> print(data)
{'array': [{'sub0': 1}, {'sub1': 2}, {'sub2': 3}]}
Example 2
We have such YAML files:
-
child-1.yml
:"1.1": one "1.2": two
-
child-2.yml
:"2.1": "2.1.1": three "2.1.2": four
-
main.yml
:children: !j2 | {% include "child-1.yml" %} {% include "child-2.yml" %}
execute python code:
from pprint import pprint
import jinjyaml as jy
import yaml
env = jinja2.Environment(loader=jinja2.FileSystemLoader('.'))
ctor = jy.Constructor()
yaml.add_constructor('!j2', ctor, yaml.FullLoader)
with open('main.yml') as fp:
doc = yaml.full_load(fp)
obj = jy.extract(doc, env)
pprint(obj)
We'll get:
{'foo': {'1.1': 'one',
'1.2': 'two',
'2.1': {'2.1.1': 'three', '2.1.2': 'four'}}}
CHANGELOG
0.2.1
Version | 0.2.1 |
---|---|
Date | 2021-11-05 |
- New Features:
- update to PyYAML 6.0
0.2
-
Date: 2021-09-08
-
new api
-
update to Jinja2 3.0
0.2a1
Many APIs changed in v0.2
v0.1
An early version, maybe not stable enough.
AUTHORS
-
Liu Xue Yan (liu_xue_yan@foxmail.com)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
jinjyaml-0.2.1.tar.gz
(7.8 kB
view hashes)