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.

2 comments:

  1. Cool beans! I have always lamented the fact that mod_autoindex is rather singleminded about its output format.
    Is this strictly for internal use by modules for now, or do you already have any plans to expose it to users somehow such that it becomes some sort of super-SSI? It would be nice to have a way of doing less trivial things than SSI is capable of without having to reach for PHP

    ReplyDelete
  2. Well, in it's current form it's really only good for embedding inside an Apache module or some other program. The only way to get data into the template is to wrap it in a variable and pass that variable in as part of the template's environment. The template language doesn't have any other way to pull in data, and without data to power your conditionals, loops, and prints the template might as well be a flat file. Thus, without some application specific code to send the appropriate data into the template the whole thing is pretty useless.

    ReplyDelete