Home

Awesome

Build Status Pythons

j2cli - Jinja2 command-line tool

j2cli is a command-line tool for templating in shell-scripts, leveraging the Jinja2 library. It supports several formats for loading the context used for rendering the Jinja2 templates. Loading environment variables as rendering context is also supported, which makes j2cli a great pair with Docker. Moreover, j2cli supports using multiple input data files, eliminating the need to seperately preprocess data files with external tools.

Getting started

Installation

# simple install
$ pip install j2cli

# install with yaml support
$ pip install j2cli[yaml]

Basic usage

# render config from template config.j2
$ j2 config.j2 config.json -o config

# render using yaml data from stdin
$ wget -O - http://example.com/config.yml | j2 --format=yml config.j2

For an extensive list of examples, see docs/examples.md.

Context data

Supported formats

j2cli supports importing context from several different sources:

For examples with each supported format, see docs/formats.md.

Context squashing

One of the main strengths of j2cli is that it is not limited to using a single data file as context. Several data files—perhaps in different formats—can be used to construct the rendering context. As the contents of the data files may "overlap", j2cli recursively squashes their contents to produce the context that will be used for rendering. The order of squashing is from left to right. I.e. the contents of a data file may be overriden by any data files specified after it on the command line.

Here is a simple example illustrating how context squashing works:

Loading data as a context subtree

By default, loaded data are squashed with the top-level context. However, this may not always be desired, especially when . E.g., when you load all the environment variables from the shell, the variables may overwri

For this, j2cli supports attaching the data from a source under a variable of the top-level context.

Reference

j2 accepts the following arguments:

Options:

There is some special behavior with environment variables:

Extras

Filters

For convenience, j2cli offers several additional Jinja2 filters that can be used in your templates. These filters should help you avoid having to implement an advanced customization module for many use cases.

See docs/filters.md for details on the available filters.

Advanced customization

j2cli offers several hooks that allow for more advanced customization of its operation. This includes:

See docs/advanced.md for details on advanced customization.

Credits

j2cli is inspired by Matt Robenolt's jinja2-cli.