tmpltool 1.5.0

A fast and simple command-line template rendering tool using MiniJinja templates with environment variables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# Template Syntax

tmpltool uses the [MiniJinja](https://github.com/mitsuhiko/minijinja) template engine, which is compatible with Python's Jinja2. For complete documentation, visit: https://docs.rs/minijinja/

## Table of Contents

- [Syntax Overview]#syntax-overview
- [Variables]#variables
- [Expressions & Operators]#expressions--operators
- [Template Tags Reference]#template-tags-reference — if, for, set, with, filter, raw, autoescape, do
- [Whitespace Control]#whitespace-control
- [Is Syntax (Validation & Checks)]#is-syntax-validation--checks
- [Comments]#comments
- [Filters]#filters
- [Template Includes]#template-includes

---

## Syntax Overview

Templates contain three types of delimiters:

| Delimiter | Purpose | Example |
|-----------|---------|---------|
| `{{ ... }}` | Expressions (output) | `{{ variable }}` |
| `{% ... %}` | Statements (logic) | `{% if condition %}` |
| `{# ... #}` | Comments (ignored) | `{# note #}` |

## Variables

```jinja
{{ variable_name }}
```

**Note:** Environment variables are NOT automatically available. Use the `get_env()` function to access them:

```jinja
{{ get_env(name="MY_VAR", default="fallback") }}
```

---

## Expressions & Operators

### Literals

```jinja
{{ "string" }}           {# String #}
{{ 42 }}                 {# Integer #}
{{ 3.14 }}               {# Float #}
{{ true }} {{ false }}   {# Booleans #}
{{ none }}               {# Null/None #}
{{ [1, 2, 3] }}          {# Array/List #}
{{ {"key": "value"} }}   {# Object/Map #}
```

### Arithmetic Operators

```jinja
{{ 10 + 5 }}    {# 15 - Addition #}
{{ 10 - 5 }}    {# 5 - Subtraction #}
{{ 10 * 5 }}    {# 50 - Multiplication #}
{{ 10 / 3 }}    {# 3.33... - Division #}
{{ 10 // 3 }}   {# 3 - Integer division #}
{{ 10 % 3 }}    {# 1 - Modulo #}
{{ 2 ** 8 }}    {# 256 - Power #}
```

### Comparison Operators

```jinja
{{ a == b }}    {# Equal #}
{{ a != b }}    {# Not equal #}
{{ a < b }}     {# Less than #}
{{ a <= b }}    {# Less than or equal #}
{{ a > b }}     {# Greater than #}
{{ a >= b }}    {# Greater than or equal #}
```

### Logical Operators

```jinja
{{ a and b }}   {# Logical AND #}
{{ a or b }}    {# Logical OR #}
{{ not a }}     {# Logical NOT #}
```

### Other Operators

```jinja
{# String concatenation #}
{{ "Hello, " ~ name ~ "!" }}

{# Membership test #}
{{ "foo" in ["foo", "bar"] }}     {# true #}
{{ "x" not in "abc" }}            {# true #}

{# Ternary/conditional expression #}
{{ "yes" if condition else "no" }}

{# Default value (if undefined or none) #}
{{ value | default("fallback") }}

{# Attribute access #}
{{ object.property }}
{{ object["property"] }}

{# Array indexing #}
{{ array[0] }}
{{ array[-1] }}    {# Last element #}

{# Slicing #}
{{ array[1:3] }}   {# Elements 1 and 2 #}
{{ array[:2] }}    {# First 2 elements #}
{{ array[2:] }}    {# From index 2 to end #}
```

---

## Template Tags Reference

### {% if %} — Conditionals

Conditional statement execution with multiple branches.

```jinja
{% if condition %}
  content when true
{% elif other_condition %}
  alternative content
{% else %}
  fallback content
{% endif %}
```

**Important:** `get_env()` cannot be used directly in `{% if %}` conditions. Use `{% set %}` to assign to a variable first:

```jinja
{% set debug = get_env(name="DEBUG", default="false") %}
{% if debug == "true" %}
  Debug mode enabled
{% endif %}
```

**Truthiness:** Empty strings, `0`, `false`, `none`, and empty collections are falsy. Everything else is truthy.

### {% for %} — Loops

Loop over sequences (arrays, objects, ranges).

```jinja
{% for item in items %}
  {{ item }}
{% endfor %}
```

**Loop Variables:**

| Variable | Description |
|----------|-------------|
| `loop.index` | Current iteration (1-indexed) |
| `loop.index0` | Current iteration (0-indexed) |
| `loop.first` | True if first iteration |
| `loop.last` | True if last iteration |
| `loop.length` | Total number of items |
| `loop.revindex` | Iterations remaining (1-indexed) |
| `loop.revindex0` | Iterations remaining (0-indexed) |
| `loop.depth` | Nesting level (starts at 1) |
| `loop.depth0` | Nesting level (starts at 0) |

**Examples:**

```jinja
{# Basic loop with index #}
{% for user in users %}
  {{ loop.index }}. {{ user.name }}
{% endfor %}

{# Alternating row styles #}
{% for row in rows %}
  <tr class="{{ loop.cycle('odd', 'even') }}">
    <td>{{ row }}</td>
  </tr>
{% endfor %}

{# Loop with else (empty collection) #}
{% for item in items %}
  {{ item }}
{% else %}
  No items found.
{% endfor %}

{# Unpacking tuples/pairs #}
{% for key, value in object | items %}
  {{ key }}: {{ value }}
{% endfor %}

{# Filtering during iteration #}
{% for user in users if user.active %}
  {{ user.name }}
{% endfor %}

{# Range loop #}
{% for i in range(5) %}
  Item {{ i }}
{% endfor %}
```

### {% set %} — Variable Assignment

Assign values to variables in the current scope.

```jinja
{% set variable = value %}
{% set name = get_env(name="USER", default="guest") %}
```

**Unpacking:**

```jinja
{% set a, b = [1, 2] %}
{% set first, rest = items | first, items | slice(1) %}
```

**Block form** (capture template output):

```jinja
{% set navigation %}
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>
{% endset %}

{# Use later #}
{{ navigation }}
```

**With filter:**

```jinja
{% set title | upper %}
  page title
{% endset %}
{# title = "PAGE TITLE" #}
```

### {% with %} — Scoped Variables

Create a new inner scope for temporary variables.

```jinja
{% with %}
  {% set temp = "value" %}
  {{ temp }}  {# works here #}
{% endwith %}
{# temp is not accessible here #}
```

**Inline assignment:**

```jinja
{% with foo = 42, bar = "hello" %}
  {{ foo }} - {{ bar }}
{% endwith %}
```

### {% filter %} — Block Filters

Apply filters to entire blocks of content.

```jinja
{% filter upper %}
  this text will be uppercase
{% endfilter %}

{% filter indent(width=4) %}
line 1
line 2
{% endfilter %}
```

### {% raw %} — Escape Template Syntax

Output literal template syntax without processing.

```jinja
{% raw %}
  This {{ will not }} be processed.
  {% for item in seq %} is literal text.
{% endraw %}
```

Useful when generating templates or documentation containing Jinja syntax.

### {% autoescape %} — Control Auto-Escaping

Toggle HTML auto-escaping within template regions.

```jinja
{% autoescape true %}
  {{ user_input }}  {# HTML entities escaped #}
{% endautoescape %}

{% autoescape false %}
  {{ trusted_html }}  {# Output as-is #}
{% endautoescape %}
```

### {% do %} — Silent Execution

Execute expressions without outputting results (useful for side-effects).

```jinja
{% do my_list.append(item) %}
```

---

## Whitespace Control

Control whitespace around template tags using `-`:

```jinja
{# Remove whitespace before tag #}
{%- if true %}

{# Remove whitespace after tag #}
{% if true -%}

{# Remove whitespace on both sides #}
{%- if true -%}

{# Also works with expressions #}
{{- variable -}}
```

**Example:**

```jinja
<ul>
{%- for item in items %}
  <li>{{ item }}</li>
{%- endfor %}
</ul>
```

Without `-`, you'd get extra blank lines. With `-`, whitespace is trimmed.

---

## Is Syntax (Validation & Checks)

The `is` syntax provides readable conditionals for validation and type checking. All is-functions support both function syntax and the more readable "is" syntax:

| Test | Function Equivalent | Description |
|------|---------------------|-------------|
| `{% if x is email %}` | `is_email(string=x)` | Valid email format |
| `{% if x is url %}` | `is_url(string=x)` | Valid URL format |
| `{% if x is ip %}` | `is_ip(string=x)` | Valid IPv4/IPv6 address |
| `{% if x is uuid %}` | `is_uuid(string=x)` | Valid UUID format |
| `{% if y is leap_year %}` | `is_leap_year(year=y)` | Year is a leap year |
| `{% if p is port_available %}` | `is_port_available(port=p)` | Port is free to use |
| `{% if f is file %}` | `is_file(path=f)` | Path is an existing file |
| `{% if d is dir %}` | `is_dir(path=d)` | Path is a directory |
| `{% if s is symlink %}` | `is_symlink(path=s)` | Path is a symbolic link |

**Examples:**

```jinja
{# Validate user input #}
{% if user_email is email %}
  Valid email: {{ user_email }}
{% else %}
  Invalid email format
{% endif %}

{# Check filesystem #}
{% if "config.json" is file %}
  {% set config = read_json_file(path="config.json") %}
{% endif %}

{# Port availability #}
{% if 8080 is port_available %}
  port: 8080
{% elif 3000 is port_available %}
  port: 3000
{% endif %}

{# Negation with "is not" #}
{% if user_input is not uuid %}
  Warning: Invalid ID format
{% endif %}
```

---

## Comments

```jinja
{# This is a comment - not rendered in output #}

{#
  Multi-line comments
  are also supported
#}
```

---

## Filters

```
{{ variable | filter_name }}
{{ variable | filter_name(arg=value) }}
```

**Built-in MiniJinja filters:**
- `upper`, `lower`, `title` - Case conversion
- `trim`, `truncate` - String operations
- `date(format="%Y-%m-%d")` - Date formatting
- `split(pat=",")` - Split string into array
- `length` - Get array/string length

**String manipulation (function + filter syntax):**

All string filters support both function and filter syntax:

- `slugify(string)` / `| slugify` - Convert to URL-friendly slug (e.g., "Hello World" → "hello-world")
- `indent(string, spaces=4)` / `| indent(spaces=4)` - Indent text by N spaces (useful for YAML/configs)
- `dedent(string)` / `| dedent` - Remove common leading whitespace
- `quote(string, style="double")` / `| quote(style="double")` - Quote string (single/double/backtick)
- `escape_quotes(string)` / `| escape_quotes` - Escape quotes in string
- `to_snake_case(string)` / `| to_snake_case` - Convert to snake_case (e.g., "HelloWorld" → "hello_world")
- `to_camel_case(string)` / `| to_camel_case` - Convert to camelCase (e.g., "hello_world" → "helloWorld")
- `to_pascal_case(string)` / `| to_pascal_case` - Convert to PascalCase (e.g., "hello_world" → "HelloWorld")
- `to_kebab_case(string)` / `| to_kebab_case` - Convert to kebab-case (e.g., "HelloWorld" → "hello-world")
- `pad_left(string, length, char=" ")` / `| pad_left(length, char=" ")` - Pad string on left
- `pad_right(string, length, char=" ")` / `| pad_right(length, char=" ")` - Pad string on right
- `repeat(string, count)` / `| repeat(count)` - Repeat string N times
- `reverse(string)` / `| reverse` - Reverse string

**Formatting (function + filter syntax):**
- `filesizeformat(bytes)` / `| filesizeformat` - Format bytes (e.g., "1.5 KB")
- `urlencode(string)` / `| urlencode` - URL encoding (percent-encoding)

**Examples:**
```
{# Case conversion - both syntaxes work #}
{{ "hello_world" | to_camel_case }}           {# Output: helloWorld #}
{{ to_camel_case(string="hello_world") }}     {# Output: helloWorld #}

{{ "HelloWorld" | to_snake_case }}            {# Output: hello_world #}
{{ to_snake_case(string="HelloWorld") }}      {# Output: hello_world #}

{# Slugify #}
{{ "Hello World!" | slugify }}                {# Output: hello-world #}
{{ slugify(string="Hello World!") }}          {# Output: hello-world #}

{# Indentation for configs #}
{{ "host: localhost\nport: 8080" | indent(spaces=2) }}
{{ indent(string="host: localhost", spaces=4) }}

{# Padding for alignment #}
{{ "1" | pad_left(length=4, char="0") }}      {# Output: 0001 #}
{{ pad_left(string="5", length=3, char="0") }} {# Output: 005 #}

{# Creating separators #}
{{ "=" | repeat(count=40) }}                  {# Output: ======================================== #}
{{ repeat(string="-", count=5) }}             {# Output: ----- #}

{# Quoting #}
{{ "hello" | quote(style="single") }}         {# Output: 'hello' #}
{{ quote(string="world", style="backtick") }} {# Output: `world` #}

{# Chaining filters #}
{{ "hello_world" | to_pascal_case | reverse }}  {# Output: dlroWolleH #}

{# Formatting - both syntaxes work #}
{{ 1048576 | filesizeformat }}                {# Output: 1 MB #}
{{ filesizeformat(bytes=1048576) }}           {# Output: 1 MB #}

{{ "hello world" | urlencode }}               {# Output: hello%20world #}
{{ urlencode(string="hello world") }}         {# Output: hello%20world #}
```

---

## Template Includes

Template includes allow you to modularize your templates by splitting them into reusable partials. This promotes code reuse and keeps templates maintainable.

### Basic Syntax

```jinja
{% include "./path/to/partial.tmpltool" %}
```

**Important:** Always use relative paths starting with `./` for includes.

### Same Directory Include

Include a template from the same directory:

```
templates/
├── main.tmpltool
└── header.tmpltool
```

**main.tmpltool:**
```jinja
{% include "./header.tmpltool" %}
Main content here
```

**header.tmpltool:**
```jinja
=== Header ===
```

**Output:**
```
=== Header ===
Main content here
```

### Subdirectory Include

Include templates from subdirectories:

```
templates/
├── main.tmpltool
└── partials/
    ├── header.tmpltool
    └── footer.tmpltool
```

**main.tmpltool:**
```jinja
{% include "./partials/header.tmpltool" %}
Main content
{% include "./partials/footer.tmpltool" %}
```

### Nested Includes

Includes can be nested—an included template can include other templates:

```
templates/
├── main.tmpltool
├── layout.tmpltool
└── components/
    └── nav.tmpltool
```

**main.tmpltool:**
```jinja
{% include "./layout.tmpltool" %}
```

**layout.tmpltool:**
```jinja
<header>
{% include "./components/nav.tmpltool" %}
</header>
<main>Content</main>
```

**components/nav.tmpltool:**
```jinja
<nav>Navigation menu</nav>
```

**Output:**
```html
<header>
<nav>Navigation menu</nav>
</header>
<main>Content</main>
```

### Multiple Includes

Combine multiple partials to build complex configurations:

```jinja
{% include "./partials/header.tmpltool" %}
{% include "./partials/database.tmpltool" %}
{% include "./partials/logging.tmpltool" %}
{% include "./partials/footer.tmpltool" %}
```

### Conditional Includes

Use conditionals to include templates based on environment variables:

```jinja
{% set env = get_env(name="ENVIRONMENT", default="development") %}

{% if env == "production" %}
{% include "./configs/production.tmpltool" %}
{% elif env == "staging" %}
{% include "./configs/staging.tmpltool" %}
{% else %}
{% include "./configs/development.tmpltool" %}
{% endif %}
```

Or toggle optional sections:

```jinja
{% set enable_monitoring = get_env(name="ENABLE_MONITORING", default="false") %}

{% if enable_monitoring == "true" %}
{% include "./partials/monitoring.tmpltool" %}
{% endif %}
```

### Variables in Included Templates

Included templates have access to all variables defined in the parent template:

**main.tmpltool:**
```jinja
{% set app_name = get_env(name="APP_NAME", default="myapp") %}
{% set port = get_env(name="PORT", default="8080") %}
{% include "./server-config.tmpltool" %}
```

**server-config.tmpltool:**
```jinja
server:
  name: {{ app_name }}
  port: {{ port }}
```

Included templates can also use all tmpltool functions like `get_env()`, `read_file()`, etc.

### Security Restrictions

By default, includes are restricted for security:

| Path Type | Default Mode | Trust Mode (`--trust`) |
|-----------|--------------|------------------------|
| Same directory (`./file.tmpltool`) | ✅ Allowed | ✅ Allowed |
| Subdirectory (`./sub/file.tmpltool`) | ✅ Allowed | ✅ Allowed |
| Parent directory (`../file.tmpltool`) | ❌ Blocked | ✅ Allowed |
| Absolute path (`/etc/file`) | ❌ Blocked | ✅ Allowed |

**Parent directory access blocked (default):**
```jinja
{# This will fail without --trust #}
{% include "../shared/common.tmpltool" %}
```

**Error:**
```
Security: Parent directory (..) traversal is not allowed: ../shared/common.tmpltool.
Use --trust to bypass this restriction.
```

**Enable with trust mode:**
```bash
tmpltool --trust template.tmpltool
```

### Real-World Example: Docker Compose

Organize a Docker Compose template with reusable service partials:

```
templates/
├── docker-compose.tmpltool
└── services/
    ├── web.tmpltool
    ├── database.tmpltool
    └── redis.tmpltool
```

**docker-compose.tmpltool:**
```jinja
version: "3.8"

services:
{% include "./services/web.tmpltool" %}

{% set use_db = get_env(name="USE_DATABASE", default="true") %}
{% if use_db == "true" %}
{% include "./services/database.tmpltool" %}
{% endif %}

{% set use_cache = get_env(name="USE_CACHE", default="false") %}
{% if use_cache == "true" %}
{% include "./services/redis.tmpltool" %}
{% endif %}
```

**services/web.tmpltool:**
```jinja
  web:
    image: {{ get_env(name="WEB_IMAGE", default="nginx:latest") }}
    ports:
      - "{{ get_env(name="WEB_PORT", default="80") }}:80"
```

**services/database.tmpltool:**
```jinja
  database:
    image: postgres:15
    environment:
      POSTGRES_DB: {{ get_env(name="DB_NAME", default="app") }}
      POSTGRES_USER: {{ get_env(name="DB_USER", default="postgres") }}
```

**Render:**
```bash
WEB_PORT=8080 USE_CACHE=true tmpltool templates/docker-compose.tmpltool
```

### Real-World Example: Kubernetes Manifests

Split Kubernetes manifests into reusable components:

```
k8s/
├── deployment.tmpltool
└── components/
    ├── metadata.tmpltool
    ├── containers.tmpltool
    └── resources.tmpltool
```

**deployment.tmpltool:**
```jinja
apiVersion: apps/v1
kind: Deployment
{% include "./components/metadata.tmpltool" %}
spec:
  replicas: {{ get_env(name="REPLICAS", default="3") }}
  template:
    spec:
      containers:
{% include "./components/containers.tmpltool" %}
```

**components/metadata.tmpltool:**
```jinja
metadata:
  name: {{ get_env(name="APP_NAME", default="myapp") }}
  namespace: {{ get_env(name="NAMESPACE", default="default") }}
  labels:
    app: {{ get_env(name="APP_NAME", default="myapp") }}
```

### Best Practices

1. **Use `./` prefix** - Always start include paths with `./` for clarity
2. **Organize partials** - Keep partials in subdirectories like `partials/`, `components/`, or `includes/`
3. **Name clearly** - Use descriptive names like `header.tmpltool`, `db-config.tmpltool`
4. **Keep partials focused** - Each partial should do one thing well
5. **Document dependencies** - Comment which variables a partial expects
6. **Avoid deep nesting** - Limit include depth for maintainability (2-3 levels max)