A command-line tool for rendering Jinja2 templates with data from various sources. RTPL (Render Template) allows you to combine template files with JSON data and environment variables to generate text output.
RTPL simplifies the process of generating configuration files, documents, or any text-based output by:
- Rendering Jinja2 templates with context data
- Supporting multiple input sources (files or stdin)
- Providing access to environment variables within templates
- Outputting to files or stdout
Pre-built binaries for Linux (x86_64) and macOS (Apple Silicon) are available.
Option 1: Download from GitHub Releases (Recommended)
Official releases are available at: https://github.com/fabiomontefuscolo/rtpl/releases
# Download and extract (replace VERSION with the desired version)
curl -L https://github.com/fabiomontefuscolo/rtpl/releases/download/VERSION/rtpl-VERSION-linux-x86_64.tar.gz | tar -xz
# or for macOS
curl -L https://github.com/fabiomontefuscolo/rtpl/releases/download/VERSION/rtpl-VERSION-macos-aarch64.tar.gz | tar -xz
chmod +x rtpl
./rtpl --helpOption 2: Development Builds
Development builds from the main branch (with debug symbols) are available from GitHub Actions artifacts.
- Ensure you have Rust and Cargo installed (https://rustup.rs/)
- Clone this repository
- Build the project:
cargo build --release- The binary will be available at
target/release/rtpl
RTPL can be used in several ways depending on your needs:
# Render a template file with environment variables
rtpl --template example.j2 --output result.txt
# Render a template from stdin
rtpl < example.j2 > result.txt
# Render a template with JSON data
rtpl --template example.j2 --data-file data.json --output result.txtRTPL accepts templates and data from various sources:
- Template from stdin:
rtpl << EOF > result.txt
Hello {{ _ENV['USER'] }}!
Your home is at {{ _ENV['HOME'] }}
EOF- Template from file, data from stdin:
rtpl --template example.j2 << EOF > output.txt
{
"name": "John Doe",
"age": 30
}
EOF- Template and data from files:
rtpl --template example.j2 --data-file data.json --output result.txtEnvironment variables are automatically available in templates via the _ENV object:
rtpl << EOF
Username: {{ _ENV['USER'] }}
Home directory: {{ _ENV['HOME'] }}
EOFRTPL includes custom filters that extend Tera's built-in functionality to match Jinja2 behavior:
Converts a value to JSON format. By default, produces compact JSON without whitespace. Use the indent parameter to pretty-print with indentation:
# Compact JSON (default)
echo '{"settings": {"host": "localhost", "port": 8080}}' \
| rtpl --template <(cat << 'EOF'
{
"config": {{ settings | tojson }}
}
EOF
)# Pretty-printed JSON with indentation
echo '{"settings": {"host": "localhost", "port": 8080}}' \
| rtpl --template <(cat << 'EOF'
Configuration:
{{ settings | tojson(indent=2) }}
EOF
)Example:
echo '{"name": "app", "version": "1.0", "features": ["a", "b"]}' \
| rtpl --template <(cat << 'EOF'
Settings: {{ name | tojson }}
Details:
{{ features | tojson(indent=2) }}
EOF
)Template (config.j2):
server {
listen {{ port | default(80) }};
server_name {{ server_name }};
root {{ _ENV['HOME'] }}/sites/{{ site_name }};
}
Data (config.json):
{
"port": 8080,
"server_name": "example.com",
"site_name": "my-website"
}Command:
rtpl --template config.j2 --data-file config.json --output nginx.confrtpl << EOF > report.txt
# System Report
Generated by: {{ _ENV['USER'] }}
Date: {{ _ENV['DATE'] | default("Unknown") }}
## System Information
- Hostname: {{ hostname | default("Unknown") }}
- Environment: {{ environment | default("development") }}
EOF--template, -t: The template file to render. If not specified, the script will read from stdin.--stdin: The input type. It can betemplateordata. If not specified, it tries to guess based on other parameters.--output, -o: The output file. If not specified, the script will print to stdout.--data-file: The JSON data file to use for template rendering.
cargo testThe project uses rustfmt for code formatting and clippy for linting:
# Check formatting
cargo fmt --all -- --check
# Run clippy
cargo clippy --all-targets --all-features -- -D warningsThe project uses GitHub Actions for continuous integration:
- Code Quality: Runs
rustfmtandclippychecks - Tests: Runs the full test suite
- Builds: Creates release binaries for:
- Linux x86_64 (
x86_64-unknown-linux-gnu) - macOS Apple Silicon (
aarch64-apple-darwin)
- Linux x86_64 (
All workflows run on pushes to main and on pull requests.
The following custom filters are available in addition to Tera's built-in filters:
tojson- Converts a value to JSON format (compact by default, useindentparameter for pretty-printing)
This filter provides Jinja2-compatible JSON serialization, matching the behavior of Jinja2's tojson filter.
Developers can easily add new custom filters to extend RTPL's functionality. All filters are organized in src/filters.rs for maintainability. See the module documentation in src/filters.rs for instructions on creating custom filters.
- Add support for TOML files
- Add support to input multiple data files
- Add more Jinja2-compatible filters as needed
- Tera throws error when variables are not found in the context (use
| default(value)filter as workaround)