November 18, 2009

Templates and code generation

Earlier this month Google released Closure Templates, a new templating library intended for generating HTML. Who cares? I personally dislike HTML templating, and avoid it whenever possible in lieu of s-expression based generation tools like CL-WHO. At first look, Closure Templates didn't seem to be anything new or useful.

The one thing that makes Closure Templates somewhat interesting is that the library works in both Java and JavaScript, so you get client and server-side templates in one place. I did a similar thing with uri-template by having the URI template expand into code that executed in both Common Lisp and JavaScript via Parenscript (here I have to state that despite being an author of a templating library, I still dislike templating libraries and even find my own creation annoying at times).

About a week after Closure Templates was released, Andrey Moskvitin (archimag) wrote an implementation in Common Lisp (interesting note: it took him only five days and 1/15 the number of lines of code as the original). The resulting system, cl-closure-template, similarly generates JavaScript via Parenscript.

I still didn't understand the motivation. Yesterday, Andrey was kind enough to explain it. Those pampered by web development in Common Lisp using s-expression HTML generation tools simply don't encounter the problem of trying to fit HTML templating onto the paradigm of incremental page updating via AJAX. So if you want to build an AJAX web application and use HTML templating, give cl-closure-template a try.

[Blog metanote: if you're reading this blog through its feed, you will shortly see this post show up in Russian. a CONS is an object which cares is now syndicated on Russian Lisp Planet, and I'm going to be writing Lisp-related posts in Russian as well as English. All such posts will be tagged 'lisp-ru'. Filter out that tag if you don't want the Russian version of the posts to show up in your feed reader.]

4 comments:

Захарджан said...

За будущие посты на русском заранее спасибо :)

synchromesh said...

That looks very interesting, thanks for that. Those Google translations were surprisingly readable, too.

I haven't used template systems much, but a good, lightweight, "Ajax-tolerant" CL webapp framework is always nice.

If you don't like templating systems, what's your preferred architecture for Common Lisp web apps?

I've written my first such system and I'm ready to throw it away, so I'm interested in better alternatives.

Cheers,

John :^P

Anonymous said...

I love how one of the touted "benefits" is that they are "Language-neutral": "work with JavaScript or Java".

We got both kinds of music: country *and* western!

Vladimir Sedach said...

I like to use CL-WHO for generating HTML. If I need to generate HTML on the client-side, I use the HTML generation macros included with Parenscript, which can work with either CL-WHO or LHTML syntax.