{"id":17001208,"url":"https://github.com/tanbro/jinjyaml","last_synced_at":"2026-03-04T06:03:24.258Z","repository":{"id":47209349,"uuid":"258397270","full_name":"tanbro/jinjyaml","owner":"tanbro","description":"Application specific YAML tag of Jinja2 template","archived":false,"fork":false,"pushed_at":"2025-01-21T04:39:09.000Z","size":155,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T13:31:57.301Z","etag":null,"topics":["jinja","jinja2","python","template","template-engine","yaml","yaml-tag"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tanbro.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.md","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-24T03:40:28.000Z","updated_at":"2025-01-21T04:39:12.000Z","dependencies_parsed_at":"2023-12-26T08:32:28.999Z","dependency_job_id":"56077e7d-b5a5-4d00-907d-cce1e62f6c79","html_url":"https://github.com/tanbro/jinjyaml","commit_stats":{"total_commits":55,"total_committers":3,"mean_commits":"18.333333333333332","dds":"0.32727272727272727","last_synced_commit":"3e88a2268ecdd1dd7a779bb83c848fe17dbe97cd"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanbro%2Fjinjyaml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanbro%2Fjinjyaml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanbro%2Fjinjyaml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanbro%2Fjinjyaml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tanbro","download_url":"https://codeload.github.com/tanbro/jinjyaml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244986190,"owners_count":20542970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jinja","jinja2","python","template","template-engine","yaml","yaml-tag"],"created_at":"2024-10-14T04:24:08.419Z","updated_at":"2026-03-04T06:03:19.200Z","avatar_url":"https://github.com/tanbro.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jinjyaml\n\n[![GitHub tag](https://img.shields.io/github/tag/tanbro/jinjyaml.svg)](https://github.com/tanbro/jinjyaml)\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)\n[![PyPI](https://img.shields.io/pypi/v/jinjyaml.svg)](https://pypi.org/project/jinjyaml/)\n[![Documentation Status](https://readthedocs.org/projects/jinjyaml/badge/?version=latest)](https://jinjyaml.readthedocs.io/en/latest/?badge=latest)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=tanbro_jinjyaml\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=tanbro_jinjyaml)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=tanbro_jinjyaml\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=tanbro_jinjyaml)\n\nAn application-specific tag for [Jinja2][] templates within [PyYAML][].\n\nThis can be useful if you want to render only specially tagged nodes in the document, rather than treating the entire YAML string as a template.\n\n## Usage\n\n### Basic Example 1\n\n1. Add `Jinja2` template constructor for tag `\"!j2\"`\n\n   ```python\n   import yaml\n   import jinjyaml as jy\n\n   ctor = jy.Constructor()\n   yaml.add_constructor(\"!j2\", ctor, yaml.SafeLoader)\n   ```\n\n1. create `YAML` file `1.yml`, with such contents:\n\n   ```yaml\n   array: !j2 |\n     {% for i in range(n) %}\n     - sub{{i}}: {{loop.index}}\n     {% endfor %}\n   ```\n\n1. load and render the `YAML` file\n\n   ```python\n   with open(\"1.yml\") as fp:\n       data = yaml.load(fp, Loader=yaml.SafeLoader)\n       # or for the short:\n       # data = yaml.safe_load(fp)\n\n   jy.extract(data, context={\"n\": 3}, inplace=True)\n\n   print(data)\n   ```\n\nWe'll get:\n\n```json\n{\"array\": [{\"sub0\": 1}, {\"sub1\": 2}, {\"sub2\": 3}]}\n```\n\n## Advanced Usage\n\n### Include files\n\n#### Jinja2's include filter function\n\nWe have such YAML files:\n\n- `sub-1.yml`:\n\n  ```yaml\n\n  \"1.1\": one\n  \"1.2\": two\n  ```\n\n- `sub-2.yml`:\n\n  ```yaml\n\n  \"2.1\":\n    \"2.1.1\": three\n    \"2.1.2\": four\n  ```\n\n- `main.yml`:\n\n  ```yaml\n  foo: !j2 |\n\n    {% filter indent %}\n    {% include \"sub-1.yml\" %}\n    {% endfilter %}\n\n    {% filter indent %}\n    {% include \"sub-2.yml\" %}\n    {% endfilter %}\n  ```\n\nexecute python code:\n\n```python\nfrom pprint import pprint\n\nimport jinja2\nimport jinjyaml as jy\nimport yaml\n\nenv = jinja2.Environment(loader=jinja2.FileSystemLoader(\".\"))\n\nctor = jy.Constructor()\nyaml.add_constructor(\"!j2\", ctor, yaml.SafeLoader)\n\nwith open(\"main.yml\") as fp:\n    doc = yaml.safe_load(fp)\n\nobj = jy.extract(doc, env)\npprint(obj)\n```\n\nWe'll get:\n\n```json\n{\"foo\": {\"1.1\": \"one\",\n         \"1.2\": \"two\",\n         \"2.1\": {\"2.1.1\": \"three\", \"2.1.2\": \"four\"}}}\n```\n\n#### pyyaml-include\n\n\u003e ℹ️ **Note:** \\\n\u003e 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) features do not handle indentation well in languages sensitive to it, such as Python or YAML. Therefore, using these features in complex cases is not recommended.\n\u003e\n\u003e For such scenarios, consider using [pyyaml-include][]. This package provides a [PyYAML][] extension that allows you to include other YAML files while preserving proper indentation. Using this extension can help maintain the integrity of your YAML files more effectively.\n\n1. install [pyyaml-include][]:\n\n    ```bash\n    pip install pyyaml-include\n    ```\n\n1. add both [pyyaml-include][] and `jinjyaml`'s constructor:\n\n    ```python\n    import yaml\n    import jinjyaml as jy\n    import pyyaml_include\n\n    yaml.add_constructor(\"!j2\", jy.Constructor)\n    yaml.add_constructor(\"!inc\", pyyaml_include.Constructor(base_dir=\"path_to_you_dir\"))\n    ```\n\n1. Assume that we have YAML files same to previous example, the `main.yml` can be modified as below:\n\n    ```yaml\n    foo: !j2 |\n      {% for i in range(n) %}\n      - !inc sub-{{loop.index}}.yml\n      {% endfor %}\n    ```\n\n1. include and load other YAML files:\n\n   Assume that we have YAML files same to previous example:\n\n    ```python\n    with open(\"main.yml\") as fp:\n        doc = yaml.safe_load(fp)\n\n    obj = jy.extract(doc, env)\n    pprint(obj)\n    ```\n\nThen we'll get:\n\n```json\n{\n  \"foo\": [\n    {\"1.1\": \"one\", \"1.2\": \"two\" },\n    {\"2.1\": {\"2.1.1\": \"three\", \"2.1.2\": \"four\"}}\n  ]\n}\n```\n\nIn this situation, there is no need to use `jinja2.Environment` and `jinja2.FileSystemLoader` to render the template, nor is it necessary to apply the `indent` filter within the template. This is because [pyyaml-include][] has already parsed the included files into objects.\n\n\u003e ❇️ **Conclusions:** \\\n\u003e You can use [jinja2][]'s `include` and `indent` to literally include other YAML files as raw text, or use [pyyaml-include][] to include other YAML files as already-parsed objects.\n\n[jinja2]: https://jinja.palletsprojects.com/ \"Jinja is a fast, expressive, extensible templating engine.\"\n[pyyaml]: https://pyyaml.org/ \"PyYAML is a full-featured YAML framework for the Python programming language.\"\n[pyyaml-include]: https://github.com/tanbro/pyyaml-include \"An extending constructor of PyYAML: include other YAML files into current YAML document.\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanbro%2Fjinjyaml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftanbro%2Fjinjyaml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanbro%2Fjinjyaml/lists"}