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 jinja2
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'}}}
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinjyaml-0.3.tar.gz.
File metadata
- Download URL: jinjyaml-0.3.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
540c245252dcfa9d4950aa3126e91e4079a7a1fbf71dd3ead75b03b74f9eaa30
|
|
| MD5 |
390334f14f93b6026e9925fb18819da0
|
|
| BLAKE2b-256 |
2119cddfc8444ce728feb32f02646360027c5639bb2d607ab6ccf66eb1d6dcda
|
File details
Details for the file jinjyaml-0.3-py3-none-any.whl.
File metadata
- Download URL: jinjyaml-0.3-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec81e0640509a5d081bf2a4ee8ec489fce9b4664f87955cc56a190e4720b6e64
|
|
| MD5 |
cc51f7e02713d20e0f2e4acc57622be6
|
|
| BLAKE2b-256 |
08fd101bca4a6ebff31c024656d836f5a9d9eda11a4c08c2e872c011e798ec65
|