Jinja CLI
Jinja CLI (current version 1.2.2) provides a command-line interface for rendering Jinja2 templates. It allows users to pass data via JSON, YAML, or environment variables and render templates directly from the terminal. The project is actively maintained with infrequent but stable releases, primarily serving as a wrapper for Jinja2.
Common errors
-
jinja: command not found
cause The `jinja-cli` package is not installed, or the directory where pip installs executables is not included in your system's PATH environment variable.fixFirst, ensure `jinja-cli` is installed: `pip install jinja-cli`. If it's installed but still not found, check your PATH. Python often installs scripts to `~/.local/bin` on Linux/macOS or `Scripts` subdirectory in your Python install path on Windows. Add this directory to your PATH. -
Error: --data parameter must be a valid JSON string.
cause The string provided to the `--data` flag is not valid JSON, or it contains unescaped characters that are being misinterpreted by the shell.fixReview your `--data` string for syntax errors (missing quotes, commas, brackets). For complex data, it's often safer to save it to a `.json` file and use `jinja --json-data your_data.json template.j2` instead of inline `--data`. -
Error: No template specified.
cause You ran the `jinja` command without providing the path to a Jinja2 template file.fixAlways provide the path to your template file as the first positional argument, e.g., `jinja my_template.j2 --data '{"key":"value"}'`.
Warnings
- gotcha When providing data via multiple sources (e.g., --env, --yaml-data, --json-data, --data), there's a specific precedence. Environment variables (--env) are loaded first, followed by YAML files, then JSON files, and finally inline --data strings. Later sources override earlier ones for conflicting keys.
- gotcha Passing complex JSON or YAML data directly via the `--data` flag can require careful shell escaping, especially with nested structures, quotes, or special characters. Incorrect escaping can lead to parsing errors or truncated data.
- gotcha By default, Jinja2 (and thus `jinja-cli`) auto-escapes HTML characters in variables. This is generally a security feature, but it can be unexpected if you intend to output raw HTML or certain characters.
Install
-
pip install jinja-cli
Imports
- jinja-cli
This package is primarily a command-line interface tool.
Quickstart
echo "Hello {{ name }}! Today is {{ day }}." > template.j2
jinja --data '{"name": "World", "day": "Friday"}' template.j2