This directory contains example templates demonstrating various features of tmpltool.
All examples can be run using:
tmpltool examples/<template-name>.tmpltool -o output.txtOr print to stdout:
tmpltool examples/<template-name>.tmpltoolA simple example showing basic variable substitution and conditionals.
Usage:
CUSTOM_VAR="Hello World" tmpltool examples/basic.txt.tmpltoolFeatures demonstrated:
- Basic variable substitution (USER, HOME, SHELL, PATH)
- Conditional blocks (if/else)
A minimal greeting template with sensible defaults using the env() function.
Usage:
# Run with defaults
tmpltool examples/greeting.txt.tmpltool
# With custom values
NAME="Alice" GREETING_TIME="morning" tmpltool examples/greeting.txt.tmpltoolOutput with defaults:
Hello World!
This message was generated by user username.
Output with custom values:
Hello Alice!
Good morning!
This message was generated by user username.
Features demonstrated:
env()function with defaults- Optional variables with conditionals
- Fallback to default values when variables not set
Application configuration file generation.
Usage:
SERVER_HOST=localhost \
SERVER_PORT=8080 \
DATABASE_URL=postgres://localhost/mydb \
DB_MAX_CONNECTIONS=10 \
LOG_LEVEL=info \
DEBUG=true \
APP_ENV=development \
APP_VERSION=1.0.0 \
tmpltool examples/config.toml.tmpltool -o app.confFeatures demonstrated:
- Configuration file generation
- Multiple variables
- Boolean conditionals for debug mode
Docker Compose file generation with env() function and sensible defaults.
Usage:
# Run with all defaults - creates a working docker-compose.yml
tmpltool examples/docker-compose.yaml.tmpltool -o docker-compose.yml
# Override specific values
SERVICE_NAME=web \
DOCKER_IMAGE=node:18-alpine \
DATABASE_URL=postgres://db:5432/myapp \
ENABLE_VOLUMES=true \
tmpltool examples/docker-compose.yaml.tmpltool -o docker-compose.ymlDefault values:
SERVICE_NAME: appDOCKER_IMAGE: node:18-alpineCONTAINER_NAME: my-appHOST_PORT: 3000CONTAINER_PORT: 3000NODE_ENV: developmentNETWORK_NAME: app-networkENABLE_VOLUMES: false
Features demonstrated:
- Complex YAML generation
env()function with sensible defaults- Multiple conditional blocks
- Optional sections based on environment variables
- Production-ready with zero configuration
- Real-world use case
Advanced configuration file with the env() function and fallback values.
Usage:
# Run with all defaults
tmpltool examples/config-with-defaults.toml.tmpltool
# Override specific values
SERVER_HOST=0.0.0.0 \
SERVER_PORT=3000 \
DB_MAX_CONNECTIONS=100 \
LOG_LEVEL=debug \
DEBUG=true \
tmpltool examples/config-with-defaults.toml.tmpltool -o app.confFeatures demonstrated:
env()function - Get environment variables with fallback values- Default value handling - No errors when variables are missing
- Mixed approach - Combine env vars with sensible defaults
- Production-ready configuration
Key syntax:
{{ env(name="VAR_NAME", default="fallback_value") }}
This is the recommended approach for configuration files where you want sensible defaults but allow overrides via environment variables.
tmpltool provides a custom env() function that allows you to safely access environment variables with optional default values.
Get environment variable with default:
{{ env(name="VARIABLE_NAME", default="default_value") }}
Get environment variable (error if not found):
{{ env(name="VARIABLE_NAME") }}
# Simple default value
port = {{ env(name="PORT", default="8080") }}
# Numeric default
max_connections = {{ env(name="MAX_CONN", default="100") }}
# Use in conditionals
{% if env(name="DEBUG", default="false") == "true" %}
debug_mode = enabled
{% endif %}
# String default
database = {{ env(name="DATABASE_URL", default="postgres://localhost/mydb") }}
- No more template errors when environment variables are missing
- Sensible defaults for development environments
- Override in production by setting environment variables
- Self-documenting - defaults show expected values
Templates use the Tera template engine syntax:
Variables:
{{ VARIABLE_NAME }}
Conditionals:
{% if VARIABLE %}
content
{% else %}
alternative
{% endif %}
Loops:
{% for item in items %}
{{ item }}
{% endfor %}
Filters:
{{ VARIABLE | lower }}
{{ VARIABLE | upper }}
See the main README for more details and the Tera documentation for complete syntax reference.