Skip to main content

Simple templating program to generate plain text (like config files) from name-value pairs.

Project description

Simple templating program to generate plain text (like config files) from name-value pairs.

Lets you create text files from templates in a versatile way. It’s designed with easy operation from the command line or scripts, Makefiles, etc. in mind. You can make use of several third party templating engines like mako or empy as well as simple built-in ones.


from a git repository into a virtualenv

This is a good approach if you work on the repository and want to test the changes.

Install eztemplate

$ git clone
$ cd eztemplate                    # change into the cloned repository
$ virtualenv venv                  # create a new environment in a subdirectory
$ . venv/bin/activate              # switch to virtualenv (important)
$ pip install .                    # just specify the directory to install from
$ eztemplate --version             # check if the correct version was installed

Upgrade eztemplate

$ git pull                         # pull latest commits from remote repository
$ . venv/bin/activate              # switch to virtualenv (if not there already)
$ pip install --upgrade --force-reinstall . # force upgrade eztemplate
$ eztemplate --version             # check if the correct version was installed

from PyPI as system command

Install eztemplate

$ pip install eztemplate           # install eztemplate from PyPI
$ eztemplate --version             # check if the correct version was installed

Upgrade eztemplate

$ pip install --upgrade eztemplate # upgrade eztemplate from PyPI
$ eztemplate --version             # check if the corrent version was installed


Getting quick help

Use the help option:

$ eztemplate --help

You can also call the package explictly with Python (and thereby choose which Python installation to use):

$ python -m eztemplate --help

Running without arguments

When you run eztemplate without arguments, it will expect a template on standard input, possibly waiting forever:

$ eztemplate
Hello, world!
Hello, world!

On __*ix__ terminals you can manually cause an end of file by pressing Ctrl-D.

Quick demonstration

You can check that substitution is working by piping a template into the program and specifying a name-value pair (make sure to protect the string with single quotes, otherwise the shell believes you want to substitute a shell variable, replacing it by an empty string):

$ echo 'Hello, $entity.' | eztemplate entity=world
Hello, world.

When you’re calling eztemplate from a script or similar - i. e. non-interactively - you should specify everything as explicitly as possible (in particular all input files or stdin as well as name-value pairs) and refrain from using positional arguments. Everything can be specified using options, which avoids ambiguities:

$ echo 'Hello, $entity.' | eztemplate --stdin --arg entity=world
Hello, world.

Templating engines

eztemplate supports several templating engines. You select the one you want to use with the -e or --engine option. Specifying help instead of a name will list all currently available engines:

$ eztemplate -e help
Available templating engines:
  empy             -  Empy templating engine.
  mako             -  Mako templating engine.
  string.Template  -  String.Template engine.

Engines missing the required packages, modules or libraries will not be displayed. For instance to be able to use the mako or the empy engine, you need to have the respective python packages installed and working.

However, eztemplate comes with simple built-in engines which are available at all times. The string.Template engine is the default when you don’t explicitly specify one.

string.Template engine

This engine is named after the string.Template class in the Python standard library. It substitutes identifiers beginning with a dollar sign. To resolve ambiguities, you can also enclose the identifier in curly braces. It’s similar to shell variable subsitution minus the more sophisticated features. It suffices for simple cases where you just need to insert some values into a text:

$ eztemplate --stdin \
>   --arg user="$( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 )" \
>   --arg food=cake --arg vendor=cafeteria --arg price="$RANDOM" \
>   <s><<\EOF
> Hello, $user.
> If you're hungry, get some ${food}s from the $vendor.
> They're only $$$price per piece.
> EOF</s>
Hello, Niels Boehm.

If you're hungry, get some cakes from the cafeteria.
They're only $29993 per piece.

Project details

Download files

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

Source Distributions

eztemplate-0.2.6.tar.gz (16.0 kB view hashes)

Uploaded source

eztemplate-0.2.6.linux-x86_64.tar.gz (17.8 kB view hashes)

Uploaded source

Built Distributions

eztemplate-0.2.6-py3-none-any.whl (41.3 kB view hashes)

Uploaded py3

eztemplate-0.2.6-py2-none-any.whl (41.3 kB view hashes)

Uploaded py2

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