tpl
tpl is an extremely simple general-purpose templating tool written in
POSIX-2024-compliant AWK.
To get started, define a list of variables in a file:
# This is a comment.
food = pizza
beverage = coffee
# Previously defined variables can be used in variable definitions.
# Whitespace is optional inside braces.
dinner = {{ food }} and {{beverage}}
# The = character can be surrounded by any number of whitespace
# characters.
one = 1
two = 2
three=3
Then reference those variables in your template file by enclosing them in double braces:
My favorite food is {{ food }}, and my favorite beverage is
{{ beverage }}. I'm going to have {{ dinner }} for dinner tonight.
The first positive integers are {{one}}, {{two}}, and {{three}}. {{one}} is
the smallest of the three.
Then pass your variable file and template file, in that order, as arguments to
the template script. To see the examples above in action, run the following
command:
./tpl example/vars example/text.tpl
The result will be written to stdout.
Specification
This section describes the behavior of the program in excruciating detail and will be of little interest to most users.
The program may be invoked as follows:
tpl [variable file] [template file...]
The variable file will be parsed, the variables defined within will be substituted for all apperances of corresponding template variables that appear in the given template file(s), and the processed text will be printed to stdout. If multiple template files are specified, their contents will be concatenated in the output.
The following subsections specify the behavior of the program in detail. Illegal inputs proscribed below will not necessarily produce an error message but may result in undefined behavior.
Template Variable Format
A template variable consists of a variable name enclosed in double braces, with zero or more whitespace characters separating the variable name and the braces. The following examples are all legal:
{{ my_var }}
{{my_var}}
{{ my_var }}
{{ my_var}}
Variable File
Variables are defined as key-value pairs separated by the character =.
The = character may be surrounded by an arbitrary number of whitespace
characters on either side. The following examples are all legal:
my_var = foo
my_var=foo
my_var = foo
Lines beginning with the character # are ignored. Note that text following
# is not otherwise ignored, so trailing inline comments are not supported.
Variable names may contain any character supported by the version of awk used
to execute this script except for the = character or any of the regular
expression metacharacters:
\ ^ $ . [ ] | ( ) * + ? { }
Variable values may reference variables defined earlier in the file, in which case they must use the brace-delimited variable format described above:
foo = {{ bar }} baz
When a value references a previously defined variable, the most recent value of that variable is used. If a variable name appears more than once, the previous value is overwritten on each assignment; previous assignments are not affected.
For example, suppose a variable file contained the following lines:
food = pizza
favorite_food = {{ food }}
food = apple
After the value was parsed, the value of favorite_food would be pizza and
the value of food would be apple.
Template File
A template consists of arbitrary text containing zero or more template variables in the brace-delimited format described above. A variable may appear any number of times within a file or a line. A template variable that does not reference a valid variable name defined in the variable file will be ignored.