j2cli: Jinja2 Command-Line Tool

0.3.10 · maintenance · verified Sat Apr 11

j2cli is a command-line tool that enables Jinja2 templating directly within shell scripts, leveraging the powerful Jinja2 library. It supports various data sources, including INI, YAML, JSON files, and environment variables. The current PyPI version is 0.3.10. While still functional for many use cases, active development on PyPI appears to have slowed, with the last release in 2019, though newer versions (0.3.12b*) are seen in some Linux distributions.

Warnings

Install

Quickstart

This example demonstrates rendering a Jinja2 template (`config.j2`) using data from a JSON file (`data.json`) via the `j2` command-line tool. The `-f json` flag specifies the input data format. The output is printed to stdout.

# Create a Jinja2 template file
cat <<EOF > config.j2
server {
  listen 80;
  server_name {{ app.hostname }};
  root {{ app.webroot }};
  index index.htm;
}
EOF

# Create a JSON data file
cat <<EOF > data.json
{
  "app": {
    "hostname": "localhost",
    "webroot": "/var/www/myproject"
  }
}
EOF

# Render the template using j2cli
j2 -f json config.j2 data.json

# Expected output:
# server {
#   listen 80;
#   server_name localhost;
#   root /var/www/myproject;
#   index index.htm;
# }

view raw JSON →