# jinjyaml
[![GitHub tag](https://img.shields.io/github/tag/tanbro/jinjyaml.svg)](https://github.com/tanbro/jinjyaml)
[![Python Package](https://github.com/tanbro/jinjyaml/actions/workflows/python-package.yml/badge.svg)](https://github.com/tanbro/jinjyaml/actions/workflows/python-package.yml)
[![PyPI](https://img.shields.io/pypi/v/jinjyaml.svg)](https://pypi.org/project/jinjyaml/)
[![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)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=tanbro_jinjyaml&metric=coverage)](https://sonarcloud.io/summary/new_code?id=tanbro_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
1. Add `Jinja2` template constructor for tag `"!j2"`
```python
import yaml
import jinjyaml as jy
ctor = jy.Constructor()
yaml.add_constructor("!j2", ctor, yaml.SafeLoader)
```
1. create `YAML` file `1.yml`, with such contents:
```yaml
array: !j2 |
{% for i in range(n) %}
- sub{{i}}: {{loop.index}}
{% endfor %}
```
1. load and render the `YAML` file
```python
with open("1.yml") as fp:
data = yaml.load(fp, Loader=yaml.SafeLoader)
# or for the short:
# data = yaml.safe_load(fp)
jy.extract(data, context={"n": 3}, inplace=True)
print(data)
```
We'll get:
```json
{"array": [{"sub0": 1}, {"sub1": 2}, {"sub2": 3}]}
```
### Example 2
We have such YAML files:
- `sub-1.yml`:
```yaml
"1.1": one
"1.2": two
```
- `sub-2.yml`:
```yaml
"2.1":
"2.1.1": three
"2.1.2": four
```
- `main.yml`:
```yaml
foo: !j2 |
{% filter indent %}
{% include "sub-1.yml" %}
{% endfilter %}
{% filter indent %}
{% include "sub-2.yml" %}
{% endfilter %}
```
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.SafeLoader)
with open("main.yml") as fp:
doc = yaml.safe_load(fp)
obj = jy.extract(doc, env)
pprint(obj)
```
We'll get:
```json
{"foo": {"1.1": "one",
"1.2": "two",
"2.1": {"2.1.1": "three", "2.1.2": "four"}}}
```
> **NOTE:**
>
> Since [Jinja2][]'s [`include`](https://jinja.palletsprojects.com/en/3.0.x/templates/#include) and [`indent`](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.indent) do not work very nice with indention languages like Python or YAML, it's not advised to use the feature in a complex case.
[jinja2]: https://jinja.palletsprojects.com/ "Jinja is a fast, expressive, extensible templating engine."
[pyyaml]: https://pyyaml.org/ "PyYAML is a full-featured YAML framework for the Python programming language."
Raw data
{
"_id": null,
"home_page": null,
"name": "jinjyaml",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "jinja, jinja2, template, template-engine, yaml, yml, yaml-tag, PyYAML",
"author": null,
"author_email": "liu xue yan <liu_xue_yan@foxmail.com>",
"download_url": "https://files.pythonhosted.org/packages/2f/27/9a486de3702acb4c349604c14d8baa0c5604ccda32a0b57d92a28978869b/jinjyaml-0.4.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[![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[![PyPI](https://img.shields.io/pypi/v/jinjyaml.svg)](https://pypi.org/project/jinjyaml/)\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[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=tanbro_jinjyaml&metric=coverage)](https://sonarcloud.io/summary/new_code?id=tanbro_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\n1. Add `Jinja2` template constructor for tag `\"!j2\"`\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.SafeLoader)\r\n ```\r\n\r\n1. create `YAML` file `1.yml`, with such contents:\r\n\r\n ```yaml\r\n array: !j2 |\r\n {% for i in range(n) %}\r\n - sub{{i}}: {{loop.index}}\r\n {% endfor %}\r\n ```\r\n\r\n1. load and render the `YAML` file\r\n\r\n ```python\r\n with open(\"1.yml\") as fp:\r\n data = yaml.load(fp, Loader=yaml.SafeLoader)\r\n # or for the short:\r\n # data = yaml.safe_load(fp)\r\n\r\n jy.extract(data, context={\"n\": 3}, inplace=True)\r\n\r\n print(data)\r\n ```\r\n\r\nWe'll get:\r\n\r\n```json\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- `sub-1.yml`:\r\n\r\n ```yaml\r\n\r\n \"1.1\": one\r\n \"1.2\": two\r\n ```\r\n\r\n- `sub-2.yml`:\r\n\r\n ```yaml\r\n\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 foo: !j2 |\r\n\r\n {% filter indent %}\r\n {% include \"sub-1.yml\" %}\r\n {% endfilter %}\r\n\r\n {% filter indent %}\r\n {% include \"sub-2.yml\" %}\r\n {% endfilter %}\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.SafeLoader)\r\n\r\nwith open(\"main.yml\") as fp:\r\n doc = yaml.safe_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```json\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> **NOTE:**\r\n>\r\n> Since [Jinja2][]'s [`include`](https://jinja.palletsprojects.com/en/3.0.x/templates/#include) and [`indent`](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.indent) do not work very nice with indention languages like Python or YAML, it's not advised to use the feature in a complex case.\r\n\r\n[jinja2]: https://jinja.palletsprojects.com/ \"Jinja is a fast, expressive, extensible templating engine.\"\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.4",
"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",
" yml",
" yaml-tag",
" pyyaml"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a96d0142d79dbc715bb9f425eaacbf9167927f6597e40dcd0d16b4aef933a864",
"md5": "948c66cad7a892abcecdb96c6922395c",
"sha256": "816a5fea4ecb913e26dbd879374df4d701a8b55cec9aeb2037d20bbb07c9ed45"
},
"downloads": -1,
"filename": "jinjyaml-0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "948c66cad7a892abcecdb96c6922395c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8523,
"upload_time": "2024-06-01T10:20:43",
"upload_time_iso_8601": "2024-06-01T10:20:43.640686Z",
"url": "https://files.pythonhosted.org/packages/a9/6d/0142d79dbc715bb9f425eaacbf9167927f6597e40dcd0d16b4aef933a864/jinjyaml-0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2f279a486de3702acb4c349604c14d8baa0c5604ccda32a0b57d92a28978869b",
"md5": "d30872edd9d85ab03186419bf93addd8",
"sha256": "2d72eefe36b8005a6a2b11d9a268bbcff18daf67120590db11062c13f40668be"
},
"downloads": -1,
"filename": "jinjyaml-0.4.tar.gz",
"has_sig": false,
"md5_digest": "d30872edd9d85ab03186419bf93addd8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 7657,
"upload_time": "2024-06-01T10:20:45",
"upload_time_iso_8601": "2024-06-01T10:20:45.183662Z",
"url": "https://files.pythonhosted.org/packages/2f/27/9a486de3702acb4c349604c14d8baa0c5604ccda32a0b57d92a28978869b/jinjyaml-0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-01 10:20:45",
"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"
}