Skip to main content

Jinja2 template renderer with yaml input files

Project description

Tool to render Jinja2 templates with mutliple input YAML data sources.


  • Render files as Jinja2 templates (usually ending with .j2)
  • Use YAML as data source for templating
  • Multiple YAML data sources are allowed to overlay sources


Let’s show some examples what j2gen is good for.

Simple rendering

A test.j2 template:

Hello {{ variable }} !

A test.yaml file containing the data:

variable: world

Will render to:

$ j2gen generate test.j2 test.yaml
Hello world !

By default, j2gen will write its output to stdout. Use the –output (or -o) flag to write to a file:

$ j2gen generate -o output test.j2 test.yaml
$ cat output
Hello world !

Rendering with multiple sources

There are cases where you want a common data source (eg. common.yaml) but need to overwrite parts of the common source (eg. with special.yaml):

common.yaml looks like:

  base: openSUSE-Leap-15.1
  name: memcached container for openSUSE
    short: memcached container
    long: memcached container for openSUSE

special.yaml looks like:

  base: SLES15SP1
  name: memcached container for SLES15SP1
    long: memcached container for SUSE Linux Enterprise Server 15 SP1

template.j2 looks like:

Base      : {{ container.base }}
Name      : {{ }}
Desc short: {{ container.description.short }}
Desc long : {{ container.description.long }}

This results in:

$ j2gen generate template.j2 common.yaml special.yaml
Base      : SLES15SP1
Name      : memcached container for SLES15SP1
Desc short: memcached container
Desc long : memcached container for SUSE Linux Enterprise Server 15 SP1

Note here that container.description.short is taken from the common.yaml input while all the other variables are overwritten with the values from special.yaml. So dict/hash like structures in the YAML data sources are deep merged. Another important part is the order of the input files when calling j2gen generate. The last source wins (in this case special.yaml).


I’m happy about every contribution like bugfixes, filling issues, improving documentation or whatever.

Running tests

The testsuite can be executed locally with:

$ tox

Project details

Download files

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

Files for j2gen, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size j2gen-0.1.0.tar.gz (8.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page