Archives

How many template languages have you written?

It seems to be one of those programming techniques that, eventually,
everyone ends up needing. You want to generate output which includes a
lot of boilerplate, fixed content (which might be HTML, XML, a
programming language, or something else entirely) but has some of its
content substituted in by code.

To solve this you define a mini-language that consists primarily of
just typing the output, but has some mechanism for substituting in
values that come from the code, and you write a mini-interpreter that
evaluates this language.

I've now done this 5 different times, with various levels of sophistication:

dbmagic - at a previous company, I wrote scripts in a combination
of awk and sh (the only scripting languages available on their ancient
unix machines) which generated C code for talking to databases. The
"template language" in this case was really just "cat <<EOF"
though.

In an early version of nrdo I generated Java code from a
mini-language that supported variable-substitution, looping, and some
constructs that were specific to outputting Java.

Later I replaced this language with CGL, a very powerful and
complex language with looping, variable declaration, lots of string
manipulation operations and some interesting constructs for dealing
with lists of data. A specific design goal of CGL was to be able to
produce the same output as its predecessor without any Java-specific
constructs, which led to a lot of required complexity. This is, by far,
the most powerful template language I've written, but suffers a bit
from its complexity and could use some cleanup.

cmScribe uses a simple template-like syntax for its "Layouts"
which involves simply adding [[Name]] constructs to HTML code to act as
placeholders where active content can be placed. I'd say this counts as
the least inherently powerful template language I ever wrote - but one of the most useful, as that active content can be anything, and is GUI-manageable.

This
last week I was faced with the need to generate PDF based on some data
that's also used on a website. Of course I used Apache FOP to generate
the PDF from XSL-FO, but I needed an easy way to generate the XSL-FO
from the data. XSLT didn't apply because my original data wasn't in XML
(so I'd have been faced with the exact same problem for generating that
XML). CGL would have been overkill and also requires extra work to make
the data available in the right format. What I came up with was a
mini-language, simple enough that it took me less than two days to
implement, with variable-substitution, looping and conditional code. A
nice feature is that it automatically hooks into JavaBeans property
getters meaning that it could talk to my existing object model without
any glue, and the syntax is so simple that the files themselves are
extremely readable (unlike CGL). This was intended as a quick hack, but
I like it so much that I may try to incorporate some of its advantages
into CGL to make it simpler to use.