stpl - a simple template system

This library provides functions to generate e.g. configuration files from templates. In the template file values/elements which needs to be substitute are defined by its KEY and enclosed by square brackets [KEY]. Variables, like $TPL_KEY, defined in the shell script are then used to fill-up the template.




stpl_fill TPL_FILE [VPREFIX]
Returns the template file (TPL_FILE) filled with variables.
stpl_check TPL_FILE [VPREFIX]
Check if all variables appearing in template file (TPL_FILE) are set.
stpl_show TPL_FILE [VPREFIX]
Returns a table containing the key/value pairs for variables appearing in template file (TPL_FILE).
stpl_data TPL_FILE [VPREFIX]
Returns the variable definitions to write in a file, which can be included by a shell script, or imported with libinclude.


(default: TPL) Default prefix for input variables. Can be individually reset at each function call.
stpl_KEYOPEN and stpl_KEYCLOSE
(default: \\[ and \\]) Opening and closing string for the keys in the template file (TPL_FILE). Note: Special characters needs to be mask two times!
(default: 0) Do automatic check (1) if all variables used in template file (TPL_FILE) are set and not empty or not (0)?
(default: 0) Should stpl_check exit (1) or cast only a warning (0) if a variable (used in TPL_FILE) is empty or not set?
(default: 'SW0 SW1 SW2 SW3 SW4 SW5 SW6 SW7 SW8 SW9') List of keys/variables which will be ignored if empty or not set while variables used in TPL_FILE are checked.


For all examples we refer to the following template file (example.tpl):

# example file for library
temperate = [TEMP]
pressure = [PRESS]
type = [TYP]

First we include, import the stpl library and define the variables we wish to fill in the template above:

import stpl

# We define the template file where the variable
# fields (like: [KEY]) are included.

# Here we define the variables VPREFIX_KEY to
# fill the template. The default for VPREFIX is 'TPL'.
TPL_SW0='# (to comment out) '

fill a template

To fill the template you now just need to call:

stpl_fill $TPL_FILE

which returns:

# example file for library
temperature = 200.9
pressure = 1.0
type = NVE
# (to comment out) statement-0

or you can simply pipe it into a file (outfile) like:

stpl_fill $TPL_FILE > outfile

check variables

You can check if all variables appearing in the template file are set and not empty with:

stpl_check $TPL_FILE

This will return 0 if all variables are defined and not empty. The empty variable $TPL_SW1 will be ignored, because the key SW1 is in $stpl_SVAR, see above.

If we unset a variable:

stpl_check $TPL_FILE

it will drop a warning ( Warning: Variable (TPL_)TYP is empty or not set!, or if stpl_HCHECK=1 the script will drop an error message and terminate with exit code 1.

show variables

The function call stpl_show $TPL_FILE returns a table containing all keys and values:

key       value
========  ========
TEMP      200.9
PRESS     1.0
TYP       NVE
SW0       # (to comment out)

write data to file

With the function call

stpl_data $TPL_FILE >

all variables (key/value pairs) appearing in the template file are now written to an include-able file (

SW0='# (to comment out) '

read data from file

Now we can use the import function from libinclude to read this data from file:

import example_data as TPL2
# do some changes
TPL2_SW1='# (now this) '
# output...
stpl_fill $TPL_FILE TPL2

which will return:

# example file for library
temperature = 200.9
pressure = 1.0
type = NVT
# (now this) statement-1