Text templates

Author: Adrian Perez <aperez@igalia.com>, initial concept and implementation by Andrés J. Díaz <ajdiaz@mundo-r.com>. This implementation shares no code with the original one.
Copyright: 2008-2009 Igalia S.L.
License:GPL3

Abstract

Miminalistic template engine to be used from shell scripts. Can be used to dynamically generate any kind of text-based content, including (but not limited to) HTML.

Contents

Template language

Templates reuse the Bash parser, so the followgin constructs can be used inside the template text:

Literal shell code execution
Lines prefixed with #! will be passed unmodified to the shell. This can be used e.g. when one want to define new variables or functions.
Variable expansion
Using ${var}. All modifiers which can be used in shell variable expansion can be used in BSP templates as well. If you want to output a literal dollar sign, use \$.
Command expansion
Using $(command). Commands can be arbitrarily complex, including for-loops and the like.

Variables are expanded from the ones defined in the environment, but only variables starting with a given prefix (var_ by default) will be seen by the templates. For example the following template:

Hello ${name}!

would use a variable named var_name when expanding it, for example using the following command:

var_name='Peter' template_expand < input.txt > output.txt

The final rendered result would be (as expected):

Hello, Peter!

Functions

template_expand

template_expand [ prefix ]

Expands shell variables in lines given as input. Optionally a prefix can be specified (default is var_) so a reference to ${xyz} in the input will be expanded with the contents of ${var_xyz}. The template is read from the standard input and the expanded result dumped as standard output. Typical usage would be:

var_foo='This is foo' var_bar='This is bar' \
    template_expand < input_template > expanded_output

template_expand_cgi

template_expand_cgi [ prefix ]

Works exactly like template_expand, but adds automatically the variables specified by the CGI standard.