Jinjanator

25.3.1 · active · verified Sun Apr 12

Jinjanator is an active command-line interface (CLI) tool for rendering Jinja2 templates, offering features like multiple data source formats (INI, YAML, JSON, dotenv) and environment variable support. It originated as a fork of j2cli and jinja2-cli, both of which are no longer maintained. The library releases new versions roughly every few months, often including Python version support updates and plugin enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `jinjanator` via its command-line interface within a Python script. It creates a temporary Jinja2 template file, sets environment variables, and then executes the `jinjanate` command to render the template. The output is printed to the console.

import subprocess
import os
import tempfile

# Create a simple Jinja2 template content
template_content = "<data><name>{{ name }}</name><age>{{ age }}</age></data>"

# Create a temporary template file
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".j2") as tmp_template:
    tmp_template.write(template_content)
    template_path = tmp_template.name

try:
    # Set environment variables for the subprocess that runs jinjanate
    env = os.environ.copy()
    env["name"] = "Andrew"
    env["age"] = "31"

    print(f"Rendering template: {template_path} with name={env['name']}, age={env['age']}")

    # Run jinjanate command. Both 'j2' and 'jinjanate' are valid entry points.
    result = subprocess.run(
        ["jinjanate", template_path],
        capture_output=True,
        text=True,
        check=True, # Raise CalledProcessError if the command returns a non-zero exit code
        env=env
    )

    print("\n--- Rendered output ---")
    print(result.stdout)
    print("-----------------------")

except subprocess.CalledProcessError as e:
    print(f"Error rendering template: {e}")
    print(f"Stderr: {e.stderr}")
finally:
    # Clean up the temporary file
    os.remove(template_path)

view raw JSON →