Sunday, September 4, 2005

Fun With Templates

My friend Greg wrote this template engine (ezt.py) in Python. It's small (less than 600 lines of code), and useful, and under a very permissive license, and thus has made its way into a variety of different programs (the Subversion build system, ViewCVS, Edna, the new ASF workflow tool, probably lots more I don't know about). If you're writing Python code and need a template engine, and you don't feel like embedding one of the heavier weight solutions, you should really give it a shot.

Lately though, I've been playing around with Apache modules a lot. I guess that's one of the dangers of spending so much time sitting 10 feet from Paul.

Considering that Apache happens to be a web server, lots of Apache modules expose some information via web pages, either diagnostics, control panels of some sort, or even the primary user interface of the program.

There isn't really a C equivalent of EZT though, so you either end up embedding a scripting language of some sort and using a template engine implemented in that, or you use something like clearsilver, which is both ugly and overkill for the kind of thing we're talking about here. Or, more likely, you end up doing what most Apache modules do and just spit out html via the moral equivalent of printf.

I think it's pretty sad that we're this far into the evolution of the web and some of the core portions of the most commonly used web server on the planet are generating their user interface via printf...

So I'm working on a better solution.

It's called APR-Template, because I can't come up with a better name, and it's similar in spirit to EZT, but implemented in C using the Apache Portable Runtime. It even writes output to an APR bucket brigade, so it's really easy to plug it into an Apache module. So far it has conditionals, loops, scalars, arrays, hashes, and weighs in at just under 1000 lines of C code.

Look for the first public release real soon now, as soon as I clean up some of the public APIs a little bit and write some more documentation.