Skip to main content

Templating engine for structured data (no free-form text generation).

Project description

structured-templates

This package implements a simple templating engine for structured data, such as YAML or JSON. It is designed to be used in conjunction with any structured data format, such as JSON, YAML or TOML. It provides control elements and variable substition, all based on evaluating keys and strings.

Example

apiVersion: v1
kind: Secret
data:
  password: ${{ b64encode(secrets.password) }}
  if(secrets.username):
    username: ${{ b64encode(secrets.username) }}
  for(key, value in secrets.additional.items()):
    ${{ key }}: ${{ b64encode(value) }}

This example demonstrates how to use the templating engine to generate a Kubernetes Secret object. The if(...) and for(...) control elements are used to conditionally include keys and loop over a dictionary, respectively. The ${{ ... }} syntax is used to evaluate expressions and substitute the result into the template.

Usage

Install the package from PyPI:

pip install structured-templates

The TemplateEngine is the main class that is used to evaluate templates.

from structured_templates import TemplateEngine

engine = TemplateEngine()
assert engine.evaluate({"key": "${{ 1 + 1 }}"}) == {"key": 2}

Specification

Value substitution

Value substitution is done by evaluating an expression and substituting the result into the template. The expression can be any valid Python expression, and the result type is inserted as-is into the template. If the result is a string, the value substition can be prefixed and/or suffixed with text.

key: prefix${{ expression }}suffix
10integers: ${{ list(range(10)) }}

Attempting to substitute a non-string value into a string will raise an error.

Control elements

if(<expr>)

Conditionally include keys in the parent object or items in a list. The value of this control block can only be a scalar value if the parent object is a list.

items:
 - item1
 - if(condition): item2
 - if(condition):
   - item3
   - item4

May result in

items:
  - item1
  - item2
  - item3
  - item4

for(<names> in <expr>)

Iterate over the elements of an iterable. The value for this field is evaluated in the new scope. If the value is an object, the results of every loop are merged into a single object. If the value is a list, the results are concatenated. The value of this control block cannot be a scalar value.

items:
  for(idx in range(2)):
    ${{ str(idx) }}: Number ${idx}

Results in

items:
  '0': Number 0
  '1': Number 1    

macro(<expr>)

Define a macro that can be used in the template.

macro(inc(x)): ${{ x + 1 }}
key: ${{ inc(1) }}

Results in

key: 2

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

structured_templates-0.0.4.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

structured_templates-0.0.4-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file structured_templates-0.0.4.tar.gz.

File metadata

  • Download URL: structured_templates-0.0.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for structured_templates-0.0.4.tar.gz
Algorithm Hash digest
SHA256 49161b930d7f42ba81a184285709ace177850092b107238a177b30c9fb589a43
MD5 3a171f9aece0594193c8b22cd45a3d0e
BLAKE2b-256 d6bf5c1e3543110ff11ea2b10d44665c44371cc72e2f40850495a6ff28ccc76c

See more details on using hashes here.

File details

Details for the file structured_templates-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for structured_templates-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f935ca6c16a97b656aa46beaae1b05f07059bafac4b2f7b54393c4705a5df8f1
MD5 8af7ebe8403c85fac08ccbfd2b4047fc
BLAKE2b-256 f3252bf6d907f266cfd9fc4951ad92a11bcf0afe6c5a7b3dfc479668d96dae43

See more details on using hashes here.

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