Skip to content

fabiomontefuscolo/rtpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RTPL - Render Template

CI

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.

Purpose

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

Installation

Pre-built Binaries

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 --help

Option 2: Development Builds

Development builds from the main branch (with debug symbols) are available from GitHub Actions artifacts.

From Source

  1. Ensure you have Rust and Cargo installed (https://rustup.rs/)
  2. Clone this repository
  3. Build the project:
cargo build --release
  1. The binary will be available at target/release/rtpl

Usage

RTPL can be used in several ways depending on your needs:

Basic Usage

# 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.txt

Input Methods

RTPL accepts templates and data from various sources:

  1. Template from stdin:
rtpl << EOF > result.txt
Hello {{ _ENV['USER'] }}!
Your home is at {{ _ENV['HOME'] }}
EOF
  1. Template from file, data from stdin:
rtpl --template example.j2 << EOF > output.txt
{
  "name": "John Doe",
  "age": 30
}
EOF
  1. Template and data from files:
rtpl --template example.j2 --data-file data.json --output result.txt

Environment Variables

Environment variables are automatically available in templates via the _ENV object:

rtpl << EOF
Username: {{ _ENV['USER'] }}
Home directory: {{ _ENV['HOME'] }}
EOF

Custom Filters

RTPL includes custom filters that extend Tera's built-in functionality to match Jinja2 behavior:

tojson

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
)

Examples

Example 1: Generate a configuration file

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.conf

Example 2: Generate a report with inline template

rtpl << 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

Parameters

  • --template, -t: The template file to render. If not specified, the script will read from stdin.
  • --stdin: The input type. It can be template or data. 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.

Development

Running Tests

cargo test

Code Quality

The 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 warnings

CI/CD

The project uses GitHub Actions for continuous integration:

  • Code Quality: Runs rustfmt and clippy checks
  • 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)

All workflows run on pushes to main and on pull requests.

Custom Filters

The following custom filters are available in addition to Tera's built-in filters:

  • tojson - Converts a value to JSON format (compact by default, use indent parameter for pretty-printing)

This filter provides Jinja2-compatible JSON serialization, matching the behavior of Jinja2's tojson filter.

Adding Custom Filters

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.

Roadmap

  • Add support for TOML files
  • Add support to input multiple data files
  • Add more Jinja2-compatible filters as needed

Known Issues

  • Tera throws error when variables are not found in the context (use | default(value) filter as workaround)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors