Package: jinja2-render
jinja2-renderA Python Tool to Render Jinja2 Templates on the Command Line with Different Contextsjinja2-renderhugely simplifies creating different versions of a text file.
It takes a jinja2 Template file as input and renders it to a final document
using a context loaded from a Python script.
It was invented for use with Dockerfiles but its use case goes far beyond.This is useful for example in the following cases:The desired document (eg. Dockerfile) contains a lot of repeated statements.
In a Jinja2 Template they can be replaced by a [For Loop][].The configuration space for the desired document is large and the differences
would be small, so maintaining a single template file is much easier.Workflowjinja2-render
                    (& contexts.py)
                          ↓

Jinja2 Template File            Rendered File
(eg. Dockerfile.jinja2)   🡆    (eg. Dockerfile)Synopsis$ jinja2-render -h

usage: jinja2-render [-h] [-c CONTEXTS] [-f TEMPLATE]
                     [-o OUTPUT]
                     [which]

Render a Jinja2 template from the command line.

positional arguments:
  which        Context to choose. Omit for a list of contexts
               available in the contexts file (-c).
               (default: None)

optional arguments:
  -h, --help   show this help message and exit
  -c CONTEXTS  The Python file defining the contexts to
               render the template. (default: ./contexts.py)
  -f TEMPLATE  The Jinja2 template to use. (default:
               Dockerfile.jinja2)
  -o OUTPUT    The output file to write to. (default:
               Dockerfile)Minimal ExampleContent ofDockerfile.jinja2:FROM{{base_img}}RUNapt-getupdate\&&apt-getinstall-yq{{packages|join(' ')}}RUNecho"Done!"Content ofcontexts.py:CONTEXTS={"v1.0":{"base_img":"debian:10-slim","packages":["glibc-devel","turboshutdown"],}}Call tojinja2-render:jinja2-renderv1.0Resulting in the following rendered Dockerfile:FROMdebian:10-slimRUNapt-getupdate\&&apt-getinstall-yqglibc-develturboshutdownRUNecho"Done!"The Contexts FileThe contexts file (defaults to./contexts.py) contains
one ore multiple context definitions to render the template.
This includes variables to be substituted, lists to render for loops, etc.The contexts filemustcontain a globalCONTEXTS = {}with keys corresponding to different contexts that can be selected.
(Pro Tip™: Instead of a dictionary, this CONTEXTS object could also
be a class that derives from dict.)The items inCONTEXTSare the actual context handed over tojinja2.Template.render().
Usually this would also be a dictionary.Original Use CaseThe script was originally developed to automate building different configurations
of the control system software EPICS and it's various modules.
Traditionally, those modules were installed by a single shell script with
fixed versions (i.e. synApps), which is bad in the Docker world
as a build failure at the end if it would require rebuilding everything before.How it compares to other templating toolsBy using a Python file to provide the context for the template, much more
flexibility is possible than with a simple solution such as jinja2-cli or j2cli.
They allow to use files such as YAML, INI or JSON as input for variables.A Python script can - however - in addition to defining static information
ease additional tasks tasks such as dependency management.
As an elaborate example of such a use case, have a look atthis,
where I use a custom class in thecontexts.pyalong with additional code
to validate the resulting context.AuthorsPhilipp Klaus, University Frankfurtinitial authorFlorian Feldbauer, University Bochumfurther improvements