October 21st 2005 #
random, October 21st 2005
Here's trivial-template.lisp — my contribution to the trivial-meme. A really really trivial bit of templating code that is responsible for pasting this text in the right place:
(let ((pars (list :x 0)) (tmpl "<%?x%>, <%(incf ?x)%>, <%(format nil \"~R\" (incf ?x))%>")) (princ-template (parse-template tmpl) pars) pars) ; prints "0, 1, two" ; returns (:X 2)
PARSE-TEMPLATE string
Parses STRING into a template to be printed with PRINC-TEMPLATE or PRINC-TEMPLATE-TO-STRING.
Template expressions are delimited by '<%' and '%>'. Text in template expressions is read as a lisp-expression. Reader settings from the current dynamic environment are used, except for *READTABLE*, which is otherwise identical to the standard readtable but causes tokens starting with '?' to be read as template variables.
PRINC-TEMPLATE template parameters &optional stream
Prints TEMPLATE to STREAM, as is, except for template expressions which are replaced by the result of evaluating the expression, printed as if by PRINC.
PARAMETERS is a plist of keywords and values. Each plist slot is accessed by the template variable of the same name.
There is also LOAD-TEMPLATE that load templates from files, and PRINC-TEMPLATE-TO-STRING that, surprise, prints to a string instead of a stream.