Skip to main content

Jinja CLI with all features

Project description

jinjitsu

Minimal Jinja CLI for rendering templates from files or stdin with variables, config files, and Python modules. Fast, predictable way to render Jinja2 templates from the terminal.

PyPI - Python Version PyPI - Status PyPI - License

InstallationQuick StartCLIExamplesTroubleshootingContributingLicense

Quick Start

  • Hello world:

    $ echo "Hello {{ name }}" | jinjitsu --stdin -D name=World
    Hello World
    
  • Render a template with includes from a different directory

    docs/README.j2.md:

    # Dynamically rendered README
    
    ```ts
    {% include 'code.ts' %}
    ```
    

    examples/code.ts:

    export const greeting = "Hello, User";
    

    Now, rendering it with jinjitsu and adding examples as a --searchpath will produce the following result:

    $ jinjitsu docs/README.j2.md -s examples/
    # Dynamically rendered README
    
    ```ts
    export const greeting = "Hello, User";
    ```
    

    Or render it directly to a file by redirecting > the output or using the -o option:

    # Like so
    $ jinjitsu docs/README.j2.md -s examples/ -o README.md
    
    # Or like so
    $ jinjitsu docs/README.j2.md -s examples/ > README.md
    
  • Autoescape HTML and feed template as a Heredoc (<<EOF)

    $ jinjitsu --stdin -D name='<World>' --autoescape on <<EOF        
    Hello {{ name }}
    EOF
    Hello &lt;World&gt;
    

CLI

Usage: jinjitsu [OPTIONS] [TEMPLATE]

Render a Jinja template.

Source (choose one)
  TEMPLATE                  Path to a template file.
  --stdin                   Read template from STDIN (heredoc/pipe).

Variables
  -D, --var KEY=VALUE       Set a string variable (repeatable). Highest precedence.
  --vars FILE               Load variables from FILE [json|yaml|toml|ini] (repeatable).
  -m, --module PATH         Import Python file; its top‑level names become variables (repeatable).

Template search paths
  --searchpath PATH         Add a directory for includes/imports (repeatable).

Jinja behavior
  --autoescape {smart,on,off}   Default smart (by extension).
  --autoescape-exts EXT,EXT     Override smart extensions (default: html,htm,xml,xhtml).
  --undefined {strict,default,debug,chain}   Missing variables policy (default: strict).
  --trim-blocks, --lstrip-blocks, --keep-trailing-newline
  --newline-sequence {\n,\r\n,\r}

Output and diagnostics
  -o, --output PATH         Write output to PATH (use - for stdout).
  --traceback               Show full Python tracebacks on errors.

Installation

Run using uv without needing to install anything:

uvx jinjitsu

Or install as a system-wide tool:

# Using uv
uv tool install jinjitsu

# Using pipx
pipx install jinjitsu

Of course, you can also install as a package in the current virtual environment:

./.venv/bin/activate && pip install jinjitsu

Examples

Let's consider an example with dynamically generated release notes. We want to render a template that uses a function from a Python module, includes a reusable sub-template, and uses variables from a vars file and the CLI.

# First, let's create a Python module with a two functions and a private variable
$ cat > extras.py <<'PY'
from datetime import datetime

def greet(name: str) -> str:
    """Greet someone."""
    return f"Greetings, {name}!"

def today() -> str:
    """Return today's date."""
    return datetime.now().strftime('%Y-%m-%d')

_private_variable = "visible"
PY

# Then, we create a vars.json file with additional variables
$ echo '{ "changelog": "CHANGELOG.md" }' > vars.json

# Let's also create a reusable template in the "shared" directory
$ mkdir -p shared && echo 'Release type: {{ type }}' > shared/type.j2

# Finally, let's define the main template
$ cat > deployment_summary.j2 <<'J2'
Deployment summary {{ today() }}

* {% include 'type.j2' %}
* Changelog: {{ changelog }}
* Status: {{ _private_variable }}
J2

# Now, render the template with jinjitsu
$ jinjitsu deployment_summary.j2 \
  --module extras.py \
  --vars vars.json \
  -D type=PROD \
  --searchpath shared

Expected output:

Deployment summary 2023-04-10

* Release type: PROD
* Changelog: CHANGELOG.md
* Status: visible

Configuration & Notes

  • Variable precedence:
    • Later sources override earlier ones
    • -D/--var flags override --vars files, which override -m/--modules.
  • Include/import search order:
    • template’s directory;
    • for --stdin, falls back to the current working directory;
    • always adds all provided -s/--searchpath directories to the above.

Troubleshooting

  • “YAML file not supported” or similar

    • Install a YAML parser: pip install PyYAML (or ruamel.yaml).
  • “Unsupported vars file type …”

    • Only .json, .yaml/.yml, .toml, .ini are supported.
  • “Template not found” or includes fail

    • Check the path and add directories with --searchpath PATH.
  • “UndefinedError” or missing variables

    • Default mode is strict. Provide values via -D/--var, --vars FILE, or --module; or use --undefined default.
  • Newline/whitespace looks wrong

    • Try --trim-blocks, --lstrip-blocks, and set --newline-sequence. Use --keep-trailing-newline to preserve a single trailing newline.
  • I need a Python traceback

    • Add --traceback for full tracebacks on errors.

Contributing

Pull requests and issues are welcome.

License

Apache 2.0. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinjitsu-0.1.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jinjitsu-0.1.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file jinjitsu-0.1.1.tar.gz.

File metadata

  • Download URL: jinjitsu-0.1.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for jinjitsu-0.1.1.tar.gz
Algorithm Hash digest
SHA256 78f420b35390239146bf6e248f284e2ef6c9f4530a86ac827b0937efa4ff7f3f
MD5 7f629153cba4b5058d3b85fc5482fda9
BLAKE2b-256 808bc115ab487245778c4d08e427e2d3ec1dfe1a6bcf989c36f926846c5b8e9d

See more details on using hashes here.

File details

Details for the file jinjitsu-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: jinjitsu-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for jinjitsu-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3206ac6affb04c84ee99fb7523701a083ae93c0d435e54cc53c4cb9b8582adfb
MD5 c0dffb837c1d63637e117294d6c49380
BLAKE2b-256 0a9c6d5be70e40fe00e9bbe2c2f1988375ec9c595ab44509b62bc9b7c365f197

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page