|
| 1 | +# Template Functions |
| 2 | + |
| 3 | +Initium extends the MiniJinja template engine with utility filters for hashing and encoding. These filters are available in all templates — both `render` templates and `seed` spec files. |
| 4 | + |
| 5 | +## Available Filters |
| 6 | + |
| 7 | +### `sha256` |
| 8 | + |
| 9 | +Compute the SHA-256 hash of a string. |
| 10 | + |
| 11 | +```jinja |
| 12 | +{{ "hello" | sha256 }} |
| 13 | +{# → 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 #} |
| 14 | +``` |
| 15 | + |
| 16 | +**Parameters:** |
| 17 | + |
| 18 | +| Parameter | Type | Default | Description | |
| 19 | +| --------- | ------ | ------- | ---------------------------------------- | |
| 20 | +| `mode` | string | `"hex"` | Output format: `"hex"` or `"bytes"` | |
| 21 | + |
| 22 | +**Modes:** |
| 23 | + |
| 24 | +- `"hex"` (default) — returns a lowercase hex string (64 characters). |
| 25 | +- `"bytes"` — returns an array of 32 byte values (integers 0–255). |
| 26 | + |
| 27 | +```jinja |
| 28 | +{{ "hello" | sha256("hex") }} |
| 29 | +{# → 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 #} |
| 30 | +
|
| 31 | +{{ "hello" | sha256("bytes") }} |
| 32 | +{# → [44, 242, 77, ...] (32 integers) #} |
| 33 | +``` |
| 34 | + |
| 35 | +### `base64_encode` |
| 36 | + |
| 37 | +Encode a string to Base64 (standard alphabet with padding). |
| 38 | + |
| 39 | +```jinja |
| 40 | +{{ "hello world" | base64_encode }} |
| 41 | +{# → aGVsbG8gd29ybGQ= #} |
| 42 | +``` |
| 43 | + |
| 44 | +### `base64_decode` |
| 45 | + |
| 46 | +Decode a Base64 string back to its original value. Returns an error if the input is not valid Base64 or the decoded bytes are not valid UTF-8. |
| 47 | + |
| 48 | +```jinja |
| 49 | +{{ "aGVsbG8gd29ybGQ=" | base64_decode }} |
| 50 | +{# → hello world #} |
| 51 | +``` |
| 52 | + |
| 53 | +## Chaining Filters |
| 54 | + |
| 55 | +Filters can be chained to compose operations: |
| 56 | + |
| 57 | +```jinja |
| 58 | +{# SHA-256 hash then Base64-encode the hex digest #} |
| 59 | +{{ "secret" | sha256 | base64_encode }} |
| 60 | +
|
| 61 | +{# Base64 encode then decode (roundtrip) #} |
| 62 | +{{ "data" | base64_encode | base64_decode }} |
| 63 | +
|
| 64 | +{# Hash an environment variable value #} |
| 65 | +{{ env.API_KEY | sha256 }} |
| 66 | +``` |
| 67 | + |
| 68 | +## Use Cases |
| 69 | + |
| 70 | +### Content Fingerprinting |
| 71 | + |
| 72 | +Generate a checksum for a config value to detect changes: |
| 73 | + |
| 74 | +```jinja |
| 75 | +checksum: {{ env.CONFIG_DATA | sha256 }} |
| 76 | +``` |
| 77 | + |
| 78 | +### Encoding Secrets |
| 79 | + |
| 80 | +Base64-encode a value for Kubernetes secret manifests: |
| 81 | + |
| 82 | +```jinja |
| 83 | +data: |
| 84 | + password: {{ env.DB_PASSWORD | base64_encode }} |
| 85 | +``` |
| 86 | + |
| 87 | +### Verifying Integrity |
| 88 | + |
| 89 | +Decode and verify Base64-encoded content: |
| 90 | + |
| 91 | +```jinja |
| 92 | +decoded_cert: {{ env.B64_CERT | base64_decode }} |
| 93 | +``` |
| 94 | + |
| 95 | +## Error Handling |
| 96 | + |
| 97 | +| Error | Cause | |
| 98 | +| -------------------------------- | --------------------------------------------------------- | |
| 99 | +| `sha256: unsupported mode '…'` | Mode parameter is not `"hex"` or `"bytes"` | |
| 100 | +| `base64_decode: invalid input` | Input string is not valid Base64 | |
| 101 | +| `base64_decode: not valid UTF-8` | Decoded bytes are not a valid UTF-8 string | |
0 commit comments