Skip to content

Render jinja2 templates using the command line without python runtime.

License

Notifications You must be signed in to change notification settings

amannocci/temply

Repository files navigation

Temply

Render jinja2 templates on the command line without python runtime.

πŸ“¦ Prerequisites

✨ Features

  • Render jinja2 template using command line without python environment.
  • Standalone executable with jinja2 templating engine.
  • Support inclusion template rendering.
  • Command line fully compatible with envtpl.
  • Support loading from environment variables.

🎯 Motivation

  • We needed a way to generate files using data sources in a container environment.
  • The solution should be standalone and delivered as a binary.

πŸ”¨ Workflow

Setup

The following steps will ensure your project is cloned properly.

  1. Clone repository:
    git clone https://github.com/amannocci/temply
    cd temply
  2. Use version defined in .python-version:
    pyenv install
  3. Install dependencies and setup environment:
    poetry install
    poetry shell
    poetry poe env:configure

Lint

  • To lint you have to use the workflow.
poetry poe lint

Format

  • To format you have to use the workflow.
poetry poe fmt
  • It will format the project code using black and isort.

Build

  • To build you have to use the workflow.
poetry poe build
  • It will compile project code with the current environment.

Test

  • To test temply you have to use the workflow.
  • Tests are based on pytest.
poetry poe test

πŸ“– Usage

How it works

  • We use PyInstaller to create a standalone executable.
  • This means you don't need python runtime to run it.
  • This means also that we have a dependency with glibc version.
  • To be compatible with a wide range of linux, we build the project using an old glibc version.
  • If the project don't run, you will have to re-build it using your distribution.
  • When the project start, it will look at the template, found the directory of the template and then configure jinja2 the filesystem.
  • It will then attempt to render the template and create a file or display on stdout.
  • You can use any jinja2 syntax.

How to render a simple configuration

  • Before anything, note that you can render any file with any extension because jinja2 is based on text templating.
  • Create a file where you want /path/to/template.yml.tpl with the following content.
variable = '{{ variable }}'
another_one = '{{ another_one }}'
default_var = '{{ default_missing_var | default("default") }}'
  • Then launch the command below to render.
variable=foo another_one=bar temply -o /path/to/template.yml /path/to/template.yml.tpl
  • It will create a file /path/to/template.yml with the following content.
variable = 'foo'
another_one = 'bar'
default_var = 'default'

How to render a configuration from stdin

  • Launch the command below to render.
echo 'Hello {{ name }} !' | name=world temply -o /path/to/template.txt
  • It will create a file /path/to/template.txt with the following content.
Hello world !

How to render an advanced configuration

  • Create a file where you want /path/to/template.yml.tpl with the following content.
{% include "include.yml.tpl" %}
  • And another one at /path/to/include.yml.tpl with the following content.
foobar="Hello world !"
  • Then launch the command below to render.
temply -o /path/to/template.yml /path/to/template.yml.tpl
  • It will create a file /path/to/template.yml with the following content.
foobar="Hello world !"

How to render a configuration with missing environment variables.

  • Create a file where you want /path/to/template.yml.tpl with the following content.
missing_var = '{{ missing_var }}'
  • Then launch the command below to render.
temply --allow-missing -o /path/to/template.yml /path/to/template.yml.tpl
  • It will create a file /path/to/template.yml with the following content.
missing_var = ''

How to render a configuration on stdout.

  • Create a file where you want /path/to/template.yml.tpl with the following content.
foobar="{{ foobar }}"
  • Then launch the command below to render.
foobar='Hello world !' temply /path/to/template.yml.tpl
  • It will output on stdout the following content.
foobar="Hello world !"

How to render a configuration with a json environment variable.

  • Create a file where you want /path/to/template.json.tpl with the following content.
{{ json_var | fromjson }}
# Or {{ json_var | from_json }}
  • Then launch the command below to render.
json_var='["string"]' temply /path/to/template.json.tpl
  • It will output on stdout the following content.
["string"]

How to render a configuration with a yaml environment variable.

  • Create a file where you want /path/to/template.yaml.tpl with the following content.
{{ (yaml_var | fromyaml).foo }}
# Or {{ (yaml_var | from_yaml).foo }}
  • Then launch the command below to render.
yaml_var='foo: bar' temply /path/to/template.yaml.tpl
  • It will output on stdout the following content.
bar

How to render a configuration with a wildcard environment variable.

  • Create a file where you want /path/to/template.yml.tpl with the following content.
{% for key, value in environment('MY_') -%}
{{ key }} = {{ value }}
{% endfor %}
  • Then launch the command below to render.
MY_FOO=foo MY_BAR=bar temply /path/to/template.yml.tpl
  • It will output on stdout the following content.
BAR = bar
FOO = foo

How to render a configuration with an envdir.

  • Create a file where you want /path/to/template.yml.tpl with the following content.
foobar="{{ FOOBAR }}"
  • Then create an envdir with a file named FOOBAR with foobar as content.
  • Then launch the command below to render.
temply --envdir /path/to/envdir /path/to/template.yml.tpl
  • It will output on stdout the following content.
foobar = foobar

How to render a configuration with a dotenv file.

  • Create a file where you want /path/to/template.yml.tpl with the following content.
foobar="{{ FOOBAR }}"
  • Then create a dotenv file named dotenv with FOOBAR=foobar as content.
  • Then launch the command below to render.
temply --dotenv /path/to/dotenv /path/to/template.yml.tpl
  • It will output on stdout the following content.
foobar = foobar

How to render a configuration with a json file.

  • Create a file where you want /path/to/template.yml.tpl with the following content.
foo="{{ FOO }}"
bar="{{ BAR }}"
  • Then create a json file named file.json with the following content.
[
  {
    "key": "FOO",
    "value": "foo"
  },
  {
    "key": "BAR",
    "value": "bar"
  }
]
  • Then launch the command below to render.
temply --json-file /path/to/file.json /path/to/template.yml.tpl
  • It will output on stdout the following content.
foo="foo"
bar="bar"

How to render a configuration and keep template after rendering.

  • By default, temply will remove template file.
  • If you want to keep template you will have to use the flag --keep-template.

❀️ Contributing

If you find this project useful here's how you can help, please click the πŸ‘οΈ Watch button to avoid missing notifications about new versions, and give it a 🌟 GitHub Star!

You can also contribute by:

  • Sending a Pull Request with your awesome new features and bug fixed.
  • Be part of the community and help resolve Issues.

🧾 License

The temply project is free and open-source software licensed under the Apache-2.0 license.