Render jinja2 templates on the command line without python runtime.
- Python 3.11+ for development.
- Poetry for build system.
- Poe for task runner.
- Up for dependency updates.
- Podman for container packaging.
- pre-commit for git management.
- 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.
- We needed a way to generate files using data sources in a container environment.
- The solution should be standalone and delivered as a binary.
The following steps will ensure your project is cloned properly.
- Clone repository:
git clone https://github.com/amannocci/temply cd temply
- Use version defined in .python-version:
pyenv install
- Install dependencies and setup environment:
poetry install poetry shell poetry poe env:configure
- To lint you have to use the workflow.
poetry poe lint
- To format you have to use the workflow.
poetry poe fmt
- It will format the project code using
black
andisort
.
- To build you have to use the workflow.
poetry poe build
- It will compile project code with the current environment.
- To test
temply
you have to use the workflow. - Tests are based on
pytest
.
poetry poe test
- 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.
- 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'
- 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 !
- 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 !"
- 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 = ''
- 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 !"
- 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"]
- 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
- 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
- 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
withfoobar
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
- Create a file where you want
/path/to/template.yml.tpl
with the following content.
foobar="{{ FOOBAR }}"
- Then create a dotenv file named
dotenv
withFOOBAR=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
- 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"
- By default, temply will remove template file.
- If you want to keep template you will have to use the flag
--keep-template
.
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.
The temply
project is free and open-source software licensed under the Apache-2.0 license.