HTML templating in Scheme?

Templating is one of those must-haves for building websites; separating content from presentation is sliced bread compared to cavalierly interleave logic and HTML into one file. In Scheme, I think that quasiquotation could be used in wonderful ways to create templates; namely, every template can be a function that simply takes parameters and directly uses quasiquotation as the templater:

(define blog-post-template
(lambda (title date message comments)
(head (title "My Blog – " ,title))
(div (@ (class "post"))
(h1 ,title)
(div (@ (class "comments"))
(p ,comments) …)))))

and then to use it, you would just use

"Another pedantic blog post"
"Nov 20, 2010"
"Today I’m going to complain about everything …"
‘("Awesome blog!"
"Actually, I disagree with you on everything"

You could also add a little more flexibility into your template by accepting an association list of parameters rather than fixed parameters:

(define blog-post-template
(lambda params
(head (title "My Blog – " ,(cadr (assq ‘title params))))

which can be made easier to use by redefining quasiquote with some templating-specific conveniences.

Leave a Reply