Abstract
HTML-TEMPLATE is a portable library for Common Lisp which can be used to fill templates with arbitrary (string) values at runtime. (Actually, it doesn't matter whether the result is HTML. It's just very likely that this will be what the library is mostly used for.)It is loosely modeled after the Perl module HTML::Template and compatible with a subset of its syntax, i.e. it should be possible to use your HTML-TEMPLATE templates with HTML::Template as well (but usually not the other way around).
HTML-TEMPLATE translates templates into efficient closures which can be re-used as often as needed. It uses an intelligent cache mechanism so you can nevertheless update templates while your program is running and have the changes take effect immediately.
The rationale behind something like HTML-TEMPLATE or HTML::Template is that you want to separate code and layout (I think in Newspeak these are called the "Business Layer" and the "Presentation Layer") as much as possible when generating HTML, especially if you work with graphical artists who are responsible for the visual appearance of your site but aren't programmers. Matter of fact, you can't separate code and layout completely. I've worked (or had to work) with several different approaches over the years, including emitting HTML from CGI scripts directly, using tools like Embperl, Mason, PHP (yuk!), or Java/XML/XLST stuff, or employing different Lisp markup languages but found that HTML::Template's approach usually works best for me: The graphical designers only need to learn a minimal set of new tags (three of them) and can update their templates independently from the work done on the backend. It is simple and it just works. YMMV, of course...
HTML-TEMPLATE is intended to be portable and should work with all conforming Common Lisp implementations but is mainly tested and deployed on Linux with LispWorks and CMUCL. Let us know if you encounter any problems.
It comes with a BSD-style license so you can basically do with it whatever you want.
HTML-TEMPLATE is used by Planet Lisp, Booble, ERGO and Heike Stephan.
Download shortcut: http://weitz.de/files/html-template.tar.gz.
create-template-printer
fill-and-print-template
clear-template-cache
delete-from-template-cache
*template-start-marker*
*template-end-marker*
*default-template-pathname*
*default-template-output*
*convert-nil-to-empty-string*
*sequences-are-lists*
*upcase-attribute-strings*
*template-symbol-package*
*no-cache-check*
*force-default*
*value-access-function*
*ignore-empty-lines*
*warn-on-creation*
template-error
template-invocation-error
template-missing-value-error
template-not-a-string-error
template-not-a-string-error-value
template-syntax-error
template-syntax-error-stream
template-syntax-error-line
template-syntax-error-col
If you have a text file
#p"/tmp/foo.tmpl" like this
<table border=1>
<!-- TMPL_LOOP rows -->
<tr>
<!-- TMPL_LOOP cols -->
<!-- TMPL_IF colorful-style -->
<td align="right" bgcolor="pink"><!-- TMPL_VAR content --></td>
<!-- TMPL_ELSE -->
<td align="right" ><!-- TMPL_VAR content --></td>
<!-- /TMPL_IF -->
<!-- /TMPL_LOOP -->
</tr>
<!-- /TMPL_LOOP -->
</table>
then the following code
(let* ((rows (loop for i below 49 by 7
collect (list :cols
(loop for j from i below (+ i 7)
for string = (format nil "~R" j)
collect (list :content string
:colorful-style (oddp j))))))
(values (list :rows rows)))
(fill-and-print-template #p"/tmp/foo.tmpl" values))
will produce this HTML table:
| zero | one | two | three | four | five | six |
| seven | eight | nine | ten | eleven | twelve | thirteen |
| fourteen | fifteen | sixteen | seventeen | eighteen | nineteen | twenty |
| twenty-one | twenty-two | twenty-three | twenty-four | twenty-five | twenty-six | twenty-seven |
| twenty-eight | twenty-nine | thirty | thirty-one | thirty-two | thirty-three | thirty-four |
| thirty-five | thirty-six | thirty-seven | thirty-eight | thirty-nine | forty | forty-one |
| forty-two | forty-three | forty-four | forty-five | forty-six | forty-seven | forty-eight |
If you're on Debian you should probably use the cl-html-template Debian package which is available thanks to Peter van Eynde and Kevin Rosenberg. There's also a port for Gentoo Linux thanks to Matthew Kennedy.
HTML-TEMPLATE comes with simple system definitions for MK:DEFSYSTEM and asdf so you can either adapt it
to your needs or just unpack the archive and from within the HTML-TEMPLATE
directory start your Lisp image and evaluate the form
(mk:compile-system "html-template") (or the
equivalent one for asdf) which should compile and load the whole
system. Installation via asdf-install should also
be possible.
If for some reason you don't want to use MK:DEFSYSTEM or asdf you
can just LOAD the file load.lisp or you
can also get away with something like this:
(loop for name in '("packages" "specials" "errors" "util" "template" "api")
do (compile-file (make-pathname :name name
:type "lisp"))
(load name))
Note that on CL implementations which use the Python compiler
(i.e. CMUCL, SBCL, SCL) you can concatenate the compiled object files
to create one single object file which you can load afterwards:
cat {packages,specials,errors,util,template,api}.x86f > html-template.x86f
(Replace ".x86f" with the correct suffix for
your platform.)
The distribution includes a test file
"test.lisp" which you can LOAD
after loading HTML-TEMPLATE itself to check if everything works as
intended. If all is well you should just see these two messages:
Please wait a couple of seconds. All tests passed...Some of the tests assume the existence of a directory
#p"/tmp/" where you can create files. If you're on
Windows you should probably change this - see the variable
TMP-DIR in test.lisp.
Note that there is no public CVS repository for HTML-TEMPLATE - the repository at common-lisp.net is out of date and not in sync with the (current) version distributed from weitz.de.
<!-- name [attribute] -->where name is one of
TMPL_VAR,
TMPL_LOOP, TMPL_IF, TMPL_UNLESS,
TMPL_INCLUDE, /TMPL_LOOP,
/TMPL_IF, /TMPL_UNLESS, or TMPL_ELSE. Case doesn't matter,
i.e. tmpl_var or Tmpl_Var would also be
legal names.
If name is one of the first four listed above then
attribute must follow, otherwise it must not follow where
attribute is any sequence of characters delimited by
", ', or by whitespace. There's
currently no way to escape the delimiters, i.e. if the attribute
starts with " then the next " (and
nothing else) will end it.
Any amount (including the empty string) of whitespace directly after
'<!--' and directly before '-->' is
optional and will be ignored. However, at least one whitespace
character between name and attribute (if present) is
mandatory. But note that if attribute is not delimited by
" or ' then there must be whitespace to
separate attribute from the closing '-->'.
The following are examples for legal template tags
<!-- TMPL_VAR foo --> <!--TMPL_LOOP 'foo'--> <!-- tmpl_include "/tmp/foo.html" --> <!-- Tmpl_Else --> <!-- /TMPL_LOOP --> <!-- TMPL_LOOP foo--><!-- -->But note that in the last example the attribute is '
foo--><!--' which is probably not what you expected...
These are not legal:
<!-- TMPL_VAR --> <!-- Tmpl_Else baz --> <!-- TMPL_VARfoo --> <!--TMPL_LOOP 'foo\'bar'--> <tmpl_include "/tmp/foo.html"> <!-- TMPL_VAR NAME="foo" -->
TMPL_VAR must always be followed by an attribute (1st
example) while TMPL_ELSE must not (2nd example). The
third one isn't recognized as a template tag because of the missing
whitespace behind TMPL_VAR. The tag will simply be
ignored and the parser will look for the next occurence of
'<!--'.
The fourth example doesn't work because the second apostrophe
isn't escaped by the backslash as you might have thought. Instead, the
parser will think the attribute ends there and will complain about the
string "bar'" following it. The next example
fails because, other than with HTML::Template, the HTML comment
markers are mandatory. (But note that you can change that by setting
the variables *TEMPLATE-START-MARKER*
and *TEMPLATE-END-MARKER*.)
The last example uses HTML::Template's optional
"NAME=" notation which is not supported by
HTML-TEMPLATE.
The TMPL_VAR and TMPL_INCLUDE tags can
appear anywhere and as often as you like in your templates while the
other tags must obey certain rules - they must follow one of these
patterns
<!-- TMPL_IF attribute --> text <!-- /TMPL_IF --> <!-- TMPL_IF attribute --> text <!-- TMPL_ELSE --> text' <!-- /TMPL_IF --> <!-- TMPL_LOOP attribute --> text <!-- /TMPL_LOOP -->where text and text' themselves must be valid templates. In other words: These constructs must nest properly.
Note that, despite of its name, HTML-TEMPLATE knows absolutely nothing about HTML syntax so you can use it for other texts as well. Also, because the templates are filled in before they're sent to the browser, you can use template tags anywhere you like and not only in places where HTML comments would be legal. These two examples, e.g., will work:
<!-- Start of comment <!-- TMPL_VAR foo --> End of comment --> <A HREF="<!-- TMPL_VAR link -->">foobar</A>
CREATE-TEMPLATE-PRINTER
will convert a template into a template printer. A
template printer is a function which accepts one argument - a
template structure - which describes how the template should
be filled and prints the filled template to the stream bound to the
internal variable *TEMPLATE-OUTPUT*. You can
FUNCALL a template printer but the preferred way to use
it is to invoke it with FILL-AND-PRINT-TEMPLATE
(which can also create template printers as needed). Note that
template printers are compiled closures but although you'll usually
create them at runtime the Lisp compiler isn't invoked for this task
so you can safely excise it from your image if you so wish.
For the rest of this document we will make a distinction between
generation time which is the time the template printer is
created (either explicitely by CREATE-TEMPLATE-PRINTER
or implicitely by FILL-AND-PRINT-TEMPLATE)
and invocation time which is the time the printer is used (by
FUNCALL or FILL-AND-PRINT-TEMPLATE)
to fill and print a template.
Each of the template tags TMPL_VAR, TMPL_IF, TMPL_UNLESS,
and TMPL_LOOP is associated with a particular symbol at
generation time. This symbol is the result of INTERNing
the tag's attribute string into the package *TEMPLATE-SYMBOL-PACKAGE*. The
template structure - the argument given to the template
printer - associates these symbols with values. By default this is
done by means of a property list but this can be changed at template invocation
time by changing the contents of the variable *VALUE-ACCESS-FUNCTION*. (Note
that the name doesn't imply that template structures are structures in
the sense of Common Lisp. Usually they aren't.)
The template tags work as follows:
<!-- TMPL_VAR symbol -->
This tag will be replaced by the value associated with symbol which should be a string (or maybeNIL- see*CONVERT-NIL-TO-EMPTY-STRING*).* (let ((tp (create-template-printer "Hello <!-- TMPL_VAR foo -->!"))) (fill-and-print-template tp '(:foo "World")) (terpri) (fill-and-print-template tp '(:foo "Folks"))) Hello World! Hello Folks!
<!-- TMPL_IF symbol -->text<!-- /TMPL_IF -->
<!-- TMPL_UNLESS symbol -->text<!-- /TMPL_UNLESS -->
In the first case, if the value associated with symbol is notNILthe (sub-)template text will be filled and printed. Otherwise, the whole construct will be replaced by an empty string. In the second case, it's the other way around.* (let ((tp (create-template-printer "The <!-- TMPL_IF fast -->quick <!-- /TMPL_IF -->brown fox"))) (fill-and-print-template tp '(:fast t)) (terpri) (fill-and-print-template tp '(:fast nil))) The quick brown fox The brown fox
<!-- TMPL_IF symbol -->text<!-- TMPL_ELSE -->text'<!-- /TMPL_IF -->
<!-- TMPL_UNLESS symbol -->text<!-- TMPL_ELSE -->text'<!-- /TMPL_UNLESS -->
In the first case, if the value associated with symbol is notNILthe (sub-)template text will be filled and printed. Otherwise, text' is used instead. In the second case, it's the other way around.* (let ((tp (create-template-printer "The <!-- TMPL_IF fast -->quick<!-- TMPL_ELSE -->slow<!-- /TMPL_IF --> brown fox"))) (fill-and-print-template tp '(:fast t)) (terpri) (fill-and-print-template tp '(:fast nil))) The quick brown fox The slow brown fox
<!-- TMPL_LOOP symbol -->text<!-- /TMPL_LOOP -->
The value associated with symbol should be a sequence (see*SEQUENCES-ARE-LISTS*) of template structures. For each element of this sequence the (sub-)template text is filled and printed using the corresponding template structure.Note that each template (sub-)structure which is used to fill text introduces a new set of associations between symbols and their values. While the template printer is within text the outer template structure is temporarily "forgotten".
* (defparameter *tp* (create-template-printer "<!-- TMPL_LOOP foo -->[<!-- TMPL_VAR bar -->,<!-- TMPL_VAR baz -->]<!-- /TMPL_LOOP -->")) *TP* * (fill-and-print-template *tp* '(:foo ((:bar "EINS" :baz "ONE") (:bar "ZWEI" :baz "TWO")))) [EINS,ONE][ZWEI,TWO] * (fill-and-print-template *tp* '(:baz "ONE" :foo ((:bar "EINS") (:bar "UNO")))) [EINS,][UNO,]
<!-- TMPL_INCLUDE pathname -->
The string pathname should be a valid pathname for an existing textfile. This textfile is implicitely converted into a template printer at generation time as if it were explicitely converted by a call toCREATE-TEMPLATE-PRINTER. At invocation time the tag will be replaced by the result ofFUNCALLing this template printer with the current template structure. (Note that this implies that the included file has to be a valid template, i.e. you can't, say, have aTMPL_IFtag in your main file and put the closing/TMPL_IFinto the included file.)* (with-open-file (s "/tmp/foo" :direction :output :if-exists :supersede) (write-string "The <!-- TMPL_IF fast -->quick <!-- /TMPL_IF -->brown fox" s)) "The <!-- TMPL_IF fast -->quick <!-- /TMPL_IF -->brown fox" * (fill-and-print-template "<!-- TMPL_INCLUDE '/tmp/foo' --> jumps over the lazy dog" '(:fast t)) Warning: New template printer for #p"/tmp/foo" created The quick brown fox jumps over the lazy dog * (fill-and-print-template "<!-- TMPL_INCLUDE '/tmp/foo' --> jumps over the lazy dog" '(:fast nil)) The brown fox jumps over the lazy dogThese tags can be nested, i.e. included files can themselves include other files. Included template printers are always taken from the cache at invocation time which means you can update them seperately from the including printer.
[Generic function]
create-template-printer template &key force element-type if-does-not-exist external-format => printer
This function will create and return a template printerprintercreated from the template denoted bytemplate. The behaviour of this function depends on the type oftemplate.This function will signal an error of type
- If
templateis a stream it should be an open input stream which is read character by character withREAD-CHARand the resulting text is used as the template.- If
templateis a string it is converted into a string stream which is again fed intoCREATE-TEMPLATE-PRINTER.- If
templateis a pathname it should denote an existing text file. The file is opened byWITH-OPEN-FILEand the resulting stream is again fed intoCREATE-TEMPLATE-PRINTER. All keyword arguments exceptforceare used as keyword arguments forWITH-OPEN-FILE. The pathname will be merged with*DEFAULT-TEMPLATE-PATHNAME*before it is used.HTML-TEMPLATE maintains a cache of previously created template printers. If the (merged) pathname can be found in this cache and the file denoted by this pathname hasn't changed since the associated cached template printer was created (which is tested by
FILE-WRITE-DATE) the cached value will be used and no new template printer will be created. This can be overriden by the keyword argumentforce, i.e. whenforceis true a new template printer will unconditionally be created (and cached unlessforceis the keyword:DO-NOT-CACHE). The default value forforceis the value of*FORCE-DEFAULT*. (See also*NO-CACHE-CHECK*.) Note that you may have to useforceif you've changed one of the special variables described below and want to create a template printer based on these new settings although the template file itself hasn't changed. Also note thatFILE-WRITE-DATEmight not be able to see a difference if the never version of a file is only fractions of a second newer than the older one.TEMPLATE-INVOCATION-ERRORiftemplateis not a pathname and one of the keyword arguments is provided.* (with-input-from-string (stream "The <!-- TMPL_VAR speed --> brown fox") (funcall (create-template-printer stream) '(:speed "quick"))) The quick brown fox * (funcall (create-template-printer "The <!-- TMPL_VAR speed --> brown fox") '(:speed "slow")) The slow brown fox * (with-open-file (stream "/tmp/foo.tmpl" :direction :output) (write-string "The <!-- TMPL_VAR speed --> brown fox" stream)) "The <!-- TMPL_VAR speed --> brown fox" * (funcall (create-template-printer #p"/tmp/foo.tmpl") '(:speed "fast")) Warning: New template printer for #p"/tmp/foo.tmpl" created The fast brown fox * (funcall (create-template-printer #p"/tmp/foo.tmpl") '(:speed "extremely fast")) The extremely fast brown fox * (funcall (create-template-printer #p"/tmp/foo.tmpl" :force t) '(:speed "very fast")) Warning: New template printer for #p"/tmp/foo.tmpl" created The very fast brown fox * (probe-file "/tmp/bar.tmpl") NIL * (funcall (create-template-printer #p"/tmp/bar.tmpl" :if-does-not-exist :create) '(:foo "foo")) Warning: New template printer for #p"/tmp/bar.tmpl" created * (probe-file "/tmp/bar.tmpl") #p"/tmp/bar.tmpl"
[Generic function]
fill-and-print-template template/printer values &key stream &allow-other-keys => <no values>
This function will fill the template denoted bytemplate/printerwith the values provided byvaluesand print the resulting text tostreamwhich defaults to*DEFAULT-TEMPLATE-OUTPUT*. The value ofvaluesshould be a template structure matching the current value of*VALUE-ACCESS-FUNCTION*.If
template/printeris a function it will be used as if it were a template printer. Otherwise,template/printerwill first be fed intoCREATE-TEMPLATE-PRINTERand the resulting template printer will be used. Note that this implies that the caching mechanism described above is in effect here as well.If
template/printeris a pathname all keyword arguments except forstreamwill be used as keyword arguments forCREATE-TEMPLATE-PRINTER. If it is not a pathname keyword arguments other thanstreamwill result in an error of typeTEMPLATE-INVOCATION-ERROR.* (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "slow")) The slow brown fox * (with-input-from-string (stream "The <!-- TMPL_VAR speed --> brown fox") (fill-and-print-template stream '(:speed "quick"))) The quick brown fox * (with-open-file (stream "/tmp/foo.tmpl" :direction :output :if-exists :supersede) (write-string "The <!-- TMPL_VAR speed --> brown fox" stream)) "The <!-- TMPL_VAR speed --> brown fox" * (fill-and-print-template #p"/tmp/foo.tmpl" '(:speed "fast")) Warning: New template printer for #p"/tmp/foo.tmpl" created The fast brown fox * (fill-and-print-template #p"/tmp/foo.tmpl" '(:speed "very fast")) The very fast brown fox * (let ((tp (create-template-printer "The <!-- TMPL_VAR speed --> brown fox"))) (fill-and-print-template tp '(:speed "tardy"))) The tardy brown fox
[Function]
clear-template-cache => <no values>
This function will completely clear the cache used by HTML-TEMPLATE.
[Function]
delete-from-template-cache pathname => result
This function will remove the template printer associated withpathnamefrom HTML-TEMPLATE's cache.resultis true if there was such a template printer, orNILotherwise.
[Special variable]
*template-start-marker*
This should be a string (the default is"<!--") which is used at generation time to determine the start of a template tag.
[Special variable]
*template-end-marker*
This should be a string (the default is"-->") which is used at generation time to determine the end of a template tag.* (let ((*template-start-marker* "<") (*template-end-marker* ">")) (fill-and-print-template "The <TMPL_VAR 'speed'> <brown> fox" '(:speed "quick"))) The quick <brown> fox
[Special variable]
*default-template-pathname*
This should be a pathname (the default is the result of callingMAKE-PATHNAMEwith no arguments) which is merged with the sole argument ofCREATE-TEMPLATE-PRINTERif this argument is a pathname.* (with-open-file (stream "/tmp/foo.tmpl" :direction :output :if-exists :supersede) (write-string "The <!-- TMPL_VAR speed --> brown fox" stream)) "The <!-- TMPL_VAR speed --> brown fox" * (setq *default-template-pathname* #p"/tmp/") #p"/tmp/" * (fill-and-print-template #p"foo.tmpl" '(:speed "very fast")) Warning: New template printer for #p"/tmp/foo.tmpl" created The very fast brown fox
[Special variable]
*default-template-output*
This should be a stream (the default is*STANDARD-OUTPUT*) which is used as the output stream ofFILL-AND-PRINT-TEMPLATEif nostreamkeyword argument was provided.* (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "slow")) The slow brown fox * (with-output-to-string (*default-template-output*) (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "slow"))) "The slow brown fox"
[Special variable]
*convert-nil-to-empty-string*
If the values of this variable is trueTMPL_VARtags will be replaced by the empty string if the associated value isNIL, otherwise an error of typeTEMPLATE-MISSING-VALUE-ERRORis signaled. The default isT. This variable takes effect at invocation time.* (let ((tp (create-template-printer "The <!-- TMPL_VAR speed --> brown fox"))) (handler-bind ((template-missing-value-error (lambda (condition) (declare (ignore condition)) (use-value "slow")))) (let ((*convert-nil-to-empty-string* nil)) (fill-and-print-template tp '(:foo "bar"))))) The slow brown fox
[Special variable]
*sequences-are-lists*
If the values of this variable is true (which is the default) the code generated by aTMPL_LOOPtag expects its associated value to be a list, otherwise it expects it to be a vector. This variable takes effect at generation time.* (fill-and-print-template "<!-- TMPL_LOOP list -->[<!-- TMPL_VAR item -->]<!-- /TMPL_LOOP -->" '(:list ((:item "1") (:item "2") (:item "3")))) [1][2][3] * (let ((*sequences-are-lists* nil)) (fill-and-print-template "<!-- TMPL_LOOP vector -->[<!-- TMPL_VAR item -->]<!-- /TMPL_LOOP -->" '(:vector #((:item "1") (:item "2") (:item "3"))))) [1][2][3]
[Special variable]
*upcase-attribute-strings*
If the values of this variable is true (which is the default) attribute strings are fed toSTRING-UPCASEbefore they are interned. This variable takes effect at generation time.* (let ((*upcase-attribute-strings* nil)) (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "quick" :|speed| "slow"))) The slow brown fox
[Special variable]
*template-symbol-package*
The value of this variable should be a package designator designating the package attribute strings are interned into. The default is the KEYWORD package. This variable takes effect at generation time.* *package* #<The COMMON-LISP-USER package, 20/21 internal, 0/9 external> * (let ((*template-symbol-package* (find-package :common-lisp-user))) (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "quick" speed "slow"))) The slow brown fox
[Special variable]
*no-cache-check*
If the value of this variable is true (the default isNIL)CREATE-TEMPLATE-PRINTERandFILL-AND-PRINT-TEMPLATEwon't check whether a template file has changed since it has been cached but instead will always use the cached template printer if there is one, i.e. there will be no more disk I/O once all template printers are generated. This option is intended to be used for sites with heavy traffic when you don't expect your templates to change anymore.
[Special variable]
*force-default*
The default value for theforcekeyword argument toCREATE-TEMPLATE-PRINTER.
[Special variable]
*value-access-function*
The value of this variable should be a function of two arguments (symbolandvalues) which is used to associate symbols with their values when a template printer is invoked. The default value is#'(lambda (symbol values) (getf values symbol)). This variable takes effect at invocation time.* (let ((tp (create-template-printer "The <!-- TMPL_VAR speed --> brown fox")) (*value-access-function* #'gethash) (hash (make-hash-table :test #'eq))) (setf (gethash :speed hash) "fast") (fill-and-print-template tp hash)) The fast brown fox
[Special variable]
*ignore-empty-lines*
If the value of this variable is true (the default isNIL) template printers will suppress any whitespace in front of template tags up to (but excluding) the first#\Newlineand any whitespace behind template tags up to (and including) the first#\Newline. This holds for all tags exceptTMPL_VAR. The idea is that you might want to put tags likeTMPL_LOOPon lines of their own in order to increase the legibility of your template files without creating unnecessary empty lines in your output. This variable takes effect at generation time.* (with-open-file (s "/tmp/foo.tmpl" :direction :input) (loop for line = (read-line s nil nil) while line do (print line)) (values)) "<table>" " <!-- TMPL_LOOP row-loop -->" " <tr>" " <!-- TMPL_LOOP col-loop -->" " <td><!-- TMPL_VAR item --></td>" " <!-- /TMPL_LOOP -->" " </tr>" " <!-- /TMPL_LOOP -->" "</table>" * (let ((values (list :row-loop (loop for row in '((1 2 3 4) (2 3 4 5) (3 4 5 6)) collect (list :col-loop (loop for col in row collect (list :item (format nil "~A" col))))))) (*ignore-empty-lines* t)) (fill-and-print-template #p"/tmp/foo.tmpl" values :force t)) Warning: New template printer for #p"/tmp/foo.tmpl" created <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> <tr> <td>3</td> <td>4</td> <td>5</td> <td>6</td> </tr> </table>
[Special variable]
*warn-on-creation*
If this variable is true (which is the default)CREATE-TEMPLATE-PRINTERwill warn you whenever a template printer is newly created from a pathname argument instead of being taken from the cache.
[Condition type]
template-error
Every error signaled by HTML-TEMPLATE is of typeTEMPLATE-ERROR. This is a direct subtype ofSIMPLE-ERRORwithout any additional slots or options.
[Condition type]
template-invocation-error
Errors of typeTEMPLATE-INVOCATION-ERRORare signaled ifCREATE-TEMPLATE-PRINTERorFILL-AND-PRINT-TEMPLATEare called with wrong keyword arguments. This is a direct subtype ofTEMPLATE-ERRORwithout any additional slots or options.
[Condition type]
template-missing-value-error
An error of typeTEMPLATE-MISSING-VALUE-ERRORis signaled if a template printer forTMPL_VARis provided with aNILvalue although*CONVERT-NIL-TO-EMPTY-STRING*is false. This is a direct subtype ofTEMPLATE-ERRORwithout any additional slots or options. Whenever aTEMPLATE-MISSING-VALUE-ERRORis signaled, an associatedUSE-VALUErestart is available.
[Condition type]
template-not-a-string-error
An error of typeTEMPLATE-NOT-A-STRING-ERRORis signaled if a template printer forTMPL_VARis provided with a value which is neither a string norNIL. This is a direct subtype ofTEMPLATE-ERRORwith one additional slot for the value which can be read byTEMPLATE-NOT-A-STRING-ERROR-VALUE. Whenever aTEMPLATE-NOT-A-STRING-ERRORis signaled, an associatedUSE-VALUErestart is available.
[Function]
template-not-a-string-error-value condition => value
Ifconditionis a condition of typeTEMPLATE-NOT-A-STRING-ERRORthis function will return the (non-string) value causing the error.* (let ((tp (create-template-printer "A square has <!-- TMPL_VAR number --> corners"))) (handler-bind ((template-not-a-string-error (lambda (condition) (use-value (format nil "~R" (template-not-a-string-error-value condition)))))) (fill-and-print-template tp '(:number 4)))) A square has four corners
[Condition type]
template-syntax-error
An error of typeTEMPLATE-SYNTAX-ERRORis signaled at generation time when HTML-TEMPLATE is not able to create a template printer due to syntax errors in the template. This is a direct subtype ofTEMPLATE-ERRORwith three additional slots. These denote the stream from which HTML-TEMPLATE was reading when it encountered the error and a line and column within this stream. (See the next three entries on how to access these slots.)As many syntax errors can't be detected before the parser is at the end of the stream, the row and column usually denote the last position where the parser was happy and not the position where it gave up.
* (handler-case (fill-and-print-template "A square has <!-- TMPL_VAR number--> corners" '(:number "four")) (template-syntax-error (condition) (format t "Houston, we've got a problem on stream ~A:~%~ Looks like something went wrong after line ~A, column ~A.~%~ The last message we received was '~?'." (template-syntax-error-stream condition) (template-syntax-error-line condition) (template-syntax-error-col condition) (simple-condition-format-control condition) (simple-condition-format-arguments condition)) (values))) Houston, we've got a problem on stream #<String-Input Stream>: Looks like something went wrong after line 1, column 26. The last message we received was 'Unexpected EOF'.Note that column 26 is the position directly behind"TMPL_VAR".
[Function]
template-syntax-error-stream condition => stream
Ifconditionis a condition of typeTEMPLATE-SYNTAX-ERRORthis function will return the stream the parser was reading from when the error was encountered.
[Function]
template-syntax-error-line condition => number
Ifconditionis a condition of typeTEMPLATE-SYNTAX-ERRORthis function will return the line number which was associated with this error. As in Emacs, lines are counted beginning with 1. HTML-TEMPLATE increases the line counter whenever it reads a#\Newlinefrom its input stream.
[Function]
template-syntax-error-col condition => number
Ifconditionis a condition of typeTEMPLATE-SYNTAX-ERRORthis function will return the column number which was associated with this error. As in Emacs, columns are counted beginning with 0.
Thanks to James Anderson and Kent M. Pitman who helped to de-confuse me about restarts.
$Header: /usr/local/cvsrep/html-template/doc/index.html,v 1.37 2006/04/03 08:35:54 edi Exp $