Skip to main content

Template

pipeline status Latest version on PyPI Project license PyPI Stats

Supported Python implementations Supported Python versions

A CLI tool for generating files from Jinja2 templates and environment variables.

Installation

pip install template[all]

This will install Template along with the dependencies for all of the filters available with it. However, you can use narrower specifiers if you want to avoid a specific dependency (the filters listed below include the needed specifier). In previous releases of Template all of the dependencies were required. To not break existing templates, for the foreseeable future those dependencies will remain a requirement and new dependencies for specific filters will be optional. This will change in the 0.9 release where all filter specific dependencies will be optional. Please update your project before that happens.

Examples

$ template -h
usage: template [-h] [-o OUTPUT] [filename]

A CLI tool for generating files from Jinja2 templates and environment
variables.

positional arguments:
  filename              Input filename

  optional arguments:
    -h, --help            show this help message and exit
    -o OUTPUT, --output OUTPUT
                          Output to filename
$ export name='John'
$ echo 'Hello {{ name if name is defined else 'world' }}. | template
Hello John.
$ echo '{{ USER }}' > username.j2
$ template --output username.txt username.j2
$ cat username.txt
John

Jinja filters

The following Jinja filters were added:

  • combine: Merges 2 dictionaries with the 2nd overriding the 1st. Returns the result.

  • to_yaml: Convert to yaml (requires the yaml package specifier).

  • from_yaml: Convert from yaml (requires the yaml package specifier).

  • to_json: Convert to json.

  • from_json: Convert from json.

  • to_toml: Convert to toml (requires the toml package specifier).

  • from_toml: Convert from toml (requires the toml package specifier).

  • jmespath: Queries data using the JMESPath query language (requires the jmespath package specifier).

  • run: Runs a command and returns the stdout, stderr and returncode using run. This filter is replaced with the run function and will be removed in the 0.10 release.

  • ipaddress: Returns an IPAddress object from the netaddr library (requires the netaddr package specifier).

  • ipnetwork: Returns an IPNetwork object from the netaddr library (requires the netaddr package specifier).

  • iprange: Returns an IPRange object from the netaddr library. (requires the netaddr package specifier).

  • ipglob: Returns an IPGlob object from the netaddr library (requires the netaddr package specifier).

  • ipset: Returns an IPSet object from the netaddr library (requires the netaddr package specifier).

Example usage can be seen in tests and for specific filters in the docstrings in template/filters.py.

Jinja functions

  • run: Runs a command and returns the stdout, stderr and returncode using run. This function replaces the run filter.

  • readfile: Returns the contents of a file.

Example usage can be seen in tests and for specific filters in the docstrings in template/functions.py.

Testing

Tests require Pipenv and Bats. Run the tests with the following commands:

pipenv run lint  # Pre-commit hooks.
pipenv run doctest  # Doc tests.
pipenv run bats  # Bats tests.
pipenv run check  # Twine check.

Also, GitLab CI is setup for this project so every push to this repository is checked with all Python 2.7, Python 3.6 and later and all supported versions of PyPy.

Release

Release requires Pipenv and Python 3.6 or later. To bump the version run pipenv run bumpversion major|minor|patch to update the version and git commit and tag the changes, then run git push --follow-tags to push the git commit and tag. The GitLab CI will then build and upload a release to PyPI. To manually upload to PyPI run pipenv run build to build the Python package and pipenv run upload -s dist/* to upload a signed version.

License

This software is licensed under the AGPL 3+ license (see the LICENSE.txt file).

Author

Nimrod Adar, contact me or visit my website. Patches are welcome via git send-email. The repository is located at: https://git.shore.co.il/nimrod/.

Metadata

Release files for template 0.7.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for template 0.7.6
File Size Uploaded Provenance
template-0.7.6.tar.gz 20.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for template 0.7.6
File Interpreter ABI Platform
template-0.7.6-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 40.0 kB

Release files / template-0.7.6.tar.gz

Download URL template-0.7.6.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d7b4c0f699f9e8528e1d16a853a4cee0f945c5a9db75c0a7cddb51df573f05ea
BLAKE2b-256 checksum
How to use checksums
9e260a429d2e5bb2022e55577c2167eb8e3548f072976b7a0c2486ab0fe3f40f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.8

Release files / template-0.7.6-py2.py3-none-any.whl

Download URL template-0.7.6-py2.py3-none-any.whl
Size 19.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9404c263631671d571de564fd44bba327a87c20079754f03981bad40317fd6c5
BLAKE2b-256 checksum
How to use checksums
44ad830433d28f52cf5adf5a93c324d0db1963d875af2250b8b943f5bda91a60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.8

Security for template 0.7.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page