Skip to main content

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

Metadata

Release files for structured-templates 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for structured-templates 0.1.1
File Size Uploaded
structured_templates-0.1.1.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for structured-templates 0.1.1
File Interpreter ABI Platform
structured_templates-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / structured_templates-0.1.1.tar.gz

Download URL structured_templates-0.1.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b94d6c6c0b8a193e869dfa2511cc3b8b73a88f7edd69ecaa9ff4fd5539b6c5dc
BLAKE2b-256 checksum
How to use checksums
049789afe2eecb86ecb2673ebec1d18cc80bd252559ad27dc8940ff5d8a9b45f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / structured_templates-0.1.1-py3-none-any.whl

Download URL structured_templates-0.1.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72b9e1b8ead52f33e91028838ccdf6ea69e1373da6d26b7b3d20e628472d3eaa
BLAKE2b-256 checksum
How to use checksums
1802d9b2946086264afae6fe757af1a64b8f1629197106b2c93399f5814174d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page