sab39

... indistinguishable from magic
effing the ineffable since 1977

Categories

Show All

Recent Posts

Archives

How many template languages have you written?

12/8/2004
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:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
How many template languages have you written?

 

New site, new blog

9/15/2004
This is my initial attempt at starting a website and a blog. At the moment there's a skeleton of the structure of the site, and a very minimal bloggy thing with very few bloggy features.

The site is built with NetReach's cmScribe product, which is great for easily building websites but has been shoehorned into acting as a blog - something it wasn't designed to do, as yet. It doesn't yet support any of the standard, useful blog features, like RSS and comments. I'm going to add that in the future (both to cmScribe and to this site), because I've become a strong believer in the potential and importance of blogging.

Basically, none of the pages within the site have content, yet. I have a pretty good idea as to what should go on each of the pages, but I still need to go through and actually write them. I'll update the blog as I fill in the pages.

 
Previous Page
RSS