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_file('cfg.yml', test='passed'))

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

Load template from string

from yex import Yex

print(yex.render_text('test: {{test}}', test='passed'))

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...)

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.1.2.tar.gz (2.4 kB view hashes)

Uploaded Source

Supported by

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