Shell Templates

Shell Templates

tldr

My blogging software composes the "single page" article by cat'ing a head HTML skeletton then adding the articles and then cating the tail HTML skeletton.

The code is ugly.

To improve the code I concluded I need to use templates.

I searched the web, looked at Python template libraries, at ERB, at jinja2 and other stuff.

There are a few problems with those solutions:

My requirements are quite simple, but when using third party templating commands, the additional required machinery gets quite voluminous.

So I though about and looked at templating solutions made with "standard" Unix tools.

A kind of a template is a shell heredoc.

cat << EOT
Hello my name is $NAME
EOT

Variables and expressions in the heredoc get evaluated. However heredocs are contained within a shell script. I wanted a standalone template.

heredocs however can be ripped out of the shell script and sourced, which makes them standalone:

$ cat heredoc.template
cat << EOT
Hello my name is $NAME
EOT

$ NAME=Peter source heredoc.template

Now the only thing remaining to get rid of is the opening and closing ceremony, in order to make the templates displayable and editable in the target programms, in this case in web browsers.

The final step can be found here.

Tomáš Pospíšek, 2017-02-18

Articles