text-template

Text-Template is a library that reads text template files looking for and evaluating embedded bits of Lisp. Similar in concept to Mark Jason Dominus's Text::Template, but not nearly as full-featured

Example

Suppose a file test-template with the following contents:

here is some text
#<(side-effect) (* 2 3)>#
and some more text ## here
we do
TEXT-TEMPLATE> (with-open-file (i "test-template") 
                    (make-instance 'text-template :stream i))
#
TEXT-TEMPLATE> (fill-in * *standard-output*)
here is some text
6
and some more text T here
NIL

Details

Non-default delimiters can be specified as initargs (see the defclass form for TEXT-TEMPLATE in the source). Delimiters don't nest (despite any appearance to the contrary that may have been given by my choice of a matched pair as default) so choose some that won't occur in the code.

Within a pair of delimiters, each form is read (in the current package) and evaluated in sequence for side-effect. The return value from the last form is considered as a list designator: each element of the list in turn is printed to the stream as if by PRINC.

*TEMPLATE* and *STREAM* are bound to the template and the output stream respectively. To pass values to (or between) the delimited code segments, you may do one or more of

Debugging support is close to minimal. During reading and during evaluation of each delimited section, TEXT-TEMPLATE::*OFFSET* is bound to the character offset of the start of that section from the start of the template, which might help a bit. Beyond that, you're on your own

Implementation

When the TEXT-TEMPLATE object is instantiated we suck it into a single string, then find the delimiters and generate a closure for each delimited segment with READ-FROM-STRING and COMPILE. These closures are invoked by FILL-IN.

Where from

The latest version that I had my hands on is darcsable from http://src.telent.net/text-template/ - if you come up with your own interesting developments and I appear not to be taking you/them seriously, add your darcs tree URL to the CLiki page and we'll let the market(sic) decide which is better.

Copyright

(c) 2006 Daniel Barlow; MIT-licensed.

Feedback, comments

are welcome, but my inclination to reply to email varies by day, so don't hope too hard. I'm often to be found as dan`b or dan_b on irc.freenode.net/#lisp - also consider editing this CLiki page.

CLiki pages can be edited by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively