# jinjyaml
[![GitHub tag](https://img.shields.io/github/tag/tanbro/jinjyaml.svg)](https://github.com/tanbro/jinjyaml)
[![Test Python Package](https://github.com/tanbro/jinjyaml/actions/workflows/python-package.yml/badge.svg)](https://github.com/tanbro/jinjyaml/actions/workflows/python-package.yml)
[![Documentation Status](https://readthedocs.org/projects/jinjyaml/badge/?version=latest)](https://jinjyaml.readthedocs.io/en/latest/?badge=latest)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=tanbro_jinjyaml&metric=alert_status)](https://sonarcloud.io/dashboard?id=tanbro_jinjyaml)
[![PyPI](https://img.shields.io/pypi/v/jinjyaml.svg)](https://pypi.org/project/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
```python
>>> 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`:
```yaml
"1.1": one
"1.2": two
```
- `child-2.yml`:
```yaml
"2.1":
"2.1.1": three
"2.1.2": four
```
- `main.yml`:
```yaml
children: !j2 |
{% include "child-1.yml" %}
{% include "child-2.yml" %}
```
execute python code:
```python
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:
```python
{'foo': {'1.1': 'one',
'1.2': 'two',
'2.1': {'2.1.1': 'three', '2.1.2': 'four'}}}
```
[jinja2]: https://jinja.palletsprojects.com/ "Jinja is a modern and designer-friendly templating language for Python"
[pyyaml]: https://pyyaml.org/ "PyYAML is a full-featured YAML framework for the Python programming language."
Raw data
{
"_id": null,
"home_page": "",
"name": "jinjyaml",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "jinja,jinja2,template,template-engine,yaml,yaml-tag",
"author": "",
"author_email": "liu xue yan <liu_xue_yan@foxmail.com>",
"download_url": "https://files.pythonhosted.org/packages/21/19/cddfc8444ce728feb32f02646360027c5639bb2d607ab6ccf66eb1d6dcda/jinjyaml-0.3.tar.gz",
"platform": null,
"description": "# jinjyaml\r\n\r\n[![GitHub tag](https://img.shields.io/github/tag/tanbro/jinjyaml.svg)](https://github.com/tanbro/jinjyaml)\r\n[![Test Python Package](https://github.com/tanbro/jinjyaml/actions/workflows/python-package.yml/badge.svg)](https://github.com/tanbro/jinjyaml/actions/workflows/python-package.yml)\r\n[![Documentation Status](https://readthedocs.org/projects/jinjyaml/badge/?version=latest)](https://jinjyaml.readthedocs.io/en/latest/?badge=latest)\r\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=tanbro_jinjyaml&metric=alert_status)](https://sonarcloud.io/dashboard?id=tanbro_jinjyaml)\r\n[![PyPI](https://img.shields.io/pypi/v/jinjyaml.svg)](https://pypi.org/project/jinjyaml/)\r\n\r\nApplication specific tag of [Jinja2][] template in [PyYAML][].\r\n\r\nIt may be useful if you only want to render special tag nodes in the document,\r\ninstead of whole YAML string as a template.\r\n\r\n## Usage\r\n\r\n### Example 1\r\n\r\n```python\r\n>>> import yaml\r\n>>> import jinjyaml as jy\r\n>>>\r\n>>> ctor = jy.Constructor()\r\n>>> yaml.add_constructor('!j2', ctor, yaml.FullLoader)\r\n>>>\r\n>>> s = '''\r\n... array:\r\n... !j2 |\r\n... {% for i in range(n) %}\r\n... - sub{{i}}: {{loop.index}}\r\n... {% endfor %}\r\n... '''\r\n>>>\r\n>>> obj = yaml.full_load(s)\r\n>>>\r\n>>> data = jy.extract(obj, context={'n': 3})\r\n>>> print(data)\r\n{'array': [{'sub0': 1}, {'sub1': 2}, {'sub2': 3}]}\r\n```\r\n\r\n### Example 2\r\n\r\nWe have such YAML files:\r\n\r\n- `child-1.yml`:\r\n\r\n ```yaml\r\n \"1.1\": one\r\n \"1.2\": two\r\n ```\r\n\r\n- `child-2.yml`:\r\n\r\n ```yaml\r\n \"2.1\":\r\n \"2.1.1\": three\r\n \"2.1.2\": four\r\n ```\r\n\r\n- `main.yml`:\r\n\r\n ```yaml\r\n children: !j2 |\r\n\r\n {% include \"child-1.yml\" %}\r\n {% include \"child-2.yml\" %}\r\n ```\r\n\r\nexecute python code:\r\n\r\n```python\r\nfrom pprint import pprint\r\n\r\nimport jinja2\r\nimport jinjyaml as jy\r\nimport yaml\r\n\r\nenv = jinja2.Environment(loader=jinja2.FileSystemLoader('.'))\r\n\r\nctor = jy.Constructor()\r\nyaml.add_constructor('!j2', ctor, yaml.FullLoader)\r\n\r\nwith open('main.yml') as fp:\r\n doc = yaml.full_load(fp)\r\n\r\nobj = jy.extract(doc, env)\r\npprint(obj)\r\n```\r\n\r\nWe'll get:\r\n\r\n```python\r\n{'foo': {'1.1': 'one',\r\n '1.2': 'two',\r\n '2.1': {'2.1.1': 'three', '2.1.2': 'four'}}}\r\n```\r\n\r\n[jinja2]: https://jinja.palletsprojects.com/ \"Jinja is a modern and designer-friendly templating language for Python\"\r\n[pyyaml]: https://pyyaml.org/ \"PyYAML is a full-featured YAML framework for the Python programming language.\"\r\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "Application specific YAML tag of Jinja2 template",
"version": "0.3",
"project_urls": {
"changelog": "https://github.com/tanbro/jinjyaml/blob/master/CHANGELOG.md",
"documentation": "https://jinjyaml.readthedocs.io/",
"homepage": "https://pypi.org/project/jinjyaml/",
"repository": "https://github.com/tanbro/jinjyaml"
},
"split_keywords": [
"jinja",
"jinja2",
"template",
"template-engine",
"yaml",
"yaml-tag"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "08fd101bca4a6ebff31c024656d836f5a9d9eda11a4c08c2e872c011e798ec65",
"md5": "cc51f7e02713d20e0f2e4acc57622be6",
"sha256": "ec81e0640509a5d081bf2a4ee8ec489fce9b4664f87955cc56a190e4720b6e64"
},
"downloads": -1,
"filename": "jinjyaml-0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cc51f7e02713d20e0f2e4acc57622be6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 7501,
"upload_time": "2023-04-25T02:44:22",
"upload_time_iso_8601": "2023-04-25T02:44:22.935825Z",
"url": "https://files.pythonhosted.org/packages/08/fd/101bca4a6ebff31c024656d836f5a9d9eda11a4c08c2e872c011e798ec65/jinjyaml-0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2119cddfc8444ce728feb32f02646360027c5639bb2d607ab6ccf66eb1d6dcda",
"md5": "390334f14f93b6026e9925fb18819da0",
"sha256": "540c245252dcfa9d4950aa3126e91e4079a7a1fbf71dd3ead75b03b74f9eaa30"
},
"downloads": -1,
"filename": "jinjyaml-0.3.tar.gz",
"has_sig": false,
"md5_digest": "390334f14f93b6026e9925fb18819da0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6447,
"upload_time": "2023-04-25T02:44:24",
"upload_time_iso_8601": "2023-04-25T02:44:24.863041Z",
"url": "https://files.pythonhosted.org/packages/21/19/cddfc8444ce728feb32f02646360027c5639bb2d607ab6ccf66eb1d6dcda/jinjyaml-0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-25 02:44:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tanbro",
"github_project": "jinjyaml",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [],
"lcname": "jinjyaml"
}