Skip to main content

Jinja2 templates for yaml. Make dynamic rules and configs.

Project description

YAML Jinja2 expressions

Renders Jinja2 expressions inside YAML file.

Install

pip install yaml-expressions

Examples

Load template from file

cfg/cfg.yml:

cfg:
  test: "{{test}}"
from yex import Yex

yex = Yex('./cfg/')

print(yex.render('cfg.yml', test='passed'))

Output: {'cfg': {'test': 'passed'}}

Load template from string

from yex import Yex

result = Yex('test: {{test}}')(test='passed')
# or
result = {'test':'passed'} > Yex('test: {{test}}')
# or
result = Yex('test: {{test}}').render(test='passed')

print(result)

Output: {'test': 'passed'}

Another examples

Describe SEO checklist, generate report

tests_tpl = """
tests:
    title: {{ 20 < (title | length) < 70 }}
    description: {{ 20 < (description | length) < 160 }}"""

report_tpl = """
report: >
    Test results:
      Title: {{ 'ok' if tests.title else 'fail' }}
        ({{ title[:10] }}...)
      Description: {{ 'ok' if tests.description else 'fail' }}
        ({{ description[:10] }}...)
"""

data = {
    'title': 't' * 130,
    'description': 'd' * 120,
}

results = Yex.render_text(tests_tpl, data)
report = Yex.render_text(report_tpl, **results, **data)

print(report.get('report'))

Output:

Test results:
  Title: fail
    (tttttttttt...)
  Description: ok
    (dddddddddd...)

Render HTML pages from prepared config

template = """
meta:
{% for k, v in meta.items() %}
  {{ k }}: >-
    {{ v }}
{% endfor %}
data:
{% for k, v in data.items() %}
  {{ k }}: >-
    {{ v }}
{% endfor %}
"""
data = [
    {
        'meta': {
            'title': 'Hello, world 1!',
        },
        'data': {
            'content': """<h1>Wellcome to our site!</h1>
        <p>Glad to see you!</p>
        """
        }
    },
    {
        'meta': {
            'title': 'Hello, world 2!',
        },
        'data': {
            'content': """<h1>Wellcome to our site!</h1>
        <p>Glad to see you!</p>
        """
        }
    },
]
content_page_template = """<title>{{meta.title}}</title>
<div>{{data.content}}</div>"""
content_page = Yex(content_page_template)
for cfg in Yex(template).generate(data):
    print(content_page.render_content(cfg))

Output:

<title>Hello, world 1!</title>
    <div><h1>Wellcome to our site!</h1>
    <p>Glad to see you!</p>
    </div>
<title>Hello, world 2!</title>
    <div><h1>Wellcome to our site!</h1>
    <p>Glad to see you!</p>
    </div>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yaml-expressions-0.2.1.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file yaml-expressions-0.2.1.tar.gz.

File metadata

  • Download URL: yaml-expressions-0.2.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for yaml-expressions-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c4be33c86bdfc10c2ba2fe4539aa937e3cbcac9bc0045aca01a1e03d113a0833
MD5 b898eaa5150cb0fdff54d6349fb9ec2b
BLAKE2b-256 edaa0df1a1bb10d54fffc582d0573a42bb14f4f18122f4e678dc066a096dfa59

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page