1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>HTML-TEMPLATE - Use HTML templates from Common Lisp</title>
<style type="text/css">
pre { padding:5px; background-color:#e0e0e0 }
h3, h4 { text-decoration: underline; }
a { text-decoration: none; }
a.none { border:1px solid white; }
a.none:hover { border:1px solid white; }
a { border:1px solid white; }
a:hover { border: 1px solid black; }
a.noborder { border:0px }
a.noborder:hover { border:0px }
</style>
</head>
<body bgcolor=white>
<h2>HTML-TEMPLATE - Use HTML templates from Common Lisp</h2>
<blockquote>
<br> <br><h3>Abstract</h3>
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.)
<p>
It is loosely modeled after the Perl module <a
href="http://html-template.sf.net/">HTML::Template</a> 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).
<p>
HTML-TEMPLATE translates templates into <a href="#main">efficient closures</a> which
can be re-used as often as needed. It uses an intelligent <a href="#cache">cache
mechanism</a> so you can nevertheless update templates while your program
is running and have the changes take effect immediately.
<p>
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
<em>can't</em> 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 <a
href="http://perl.apache.org/embperl/">Embperl</a>, <a
href="http://www.masonhq.com/">Mason</a>, <a
href="http://www.php.net/">PHP</a> (yuk!), or Java/XML/XLST stuff, or
employing different <a
href="http://www.cliki.net/Lisp%20Markup%20Languages">Lisp markup
languages</a> 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...
<p>
HTML-TEMPLATE is intended to be portable and should work with all
conforming Common Lisp implementations but is mainly tested and
deployed with <a
href="http://www.lispworks.com/">LispWorks</a>. <a
href="#mail">Let us know</a> if you encounter any
problems.
<p>
It comes with a <a
href="http://www.opensource.org/licenses/bsd-license.php">BSD-style
license</a> so you can basically do with it whatever you want.
<p>
HTML-TEMPLATE is used by <a href="http://planet.lisp.org/">Planet Lisp</a>, <a href="http://www.cleartrip.com/">Cleartrip</a>, <a href="http://lemonodor.com/archives/000950.html">Booble</a>, <a href="http://ergoweb.de/">ERGO</a> and <a href="http://heikestephan.de/">Heike Stephan</a>.
<p>
<font color=red>Download shortcut:</font> <a href="http://weitz.de/files/html-template.tar.gz">http://weitz.de/files/html-template.tar.gz</a>.
</blockquote>
<br> <br><h3><a class=none name="contents">Contents</a></h3>
<ol>
<li><a href="#example">Simple example</a>
<li><a href="#install">Download and installation</a>
<li><a href="#mail">Support and mailing lists</a>
<li><a href="#syntax">Syntax</a>
<li><a href="#semantics">Semantics</a>
<li><a href="#dictionary">The HTML-TEMPLATE dictionary</a>
<ol>
<li><a href="#main">Creating and using printers</a>
<ol>
<li><a href="#create-template-printer"><code>create-template-printer</code></a>
<li><a href="#fill-and-print-template"><code>fill-and-print-template</code></a>
</ol>
<li><a href="#cache">The template cache</a>
<ol>
<li><a href="#clear-template-cache"><code>clear-template-cache</code></a>
<li><a href="#delete-from-template-cache"><code>delete-from-template-cache</code></a>
<li><a href="#*no-cache-check*"><code>*no-cache-check*</code></a>
</ol>
<li><a href="#custom">Customization</a>
<ol>
<li><a href="#*template-start-marker*"><code>*template-start-marker*</code></a>
<li><a href="#*template-end-marker*"><code>*template-end-marker*</code></a>
<li><a href="#*default-template-pathname*"><code>*default-template-pathname*</code></a>
<li><a href="#*default-template-output*"><code>*default-template-output*</code></a>
<li><a href="#*convert-nil-to-empty-string*"><code>*convert-nil-to-empty-string*</code></a>
<li><a href="#*format-non-strings*"><code>*format-non-strings*</code></a>
<li><a href="#*sequences-are-lists*"><code>*sequences-are-lists*</code></a>
<li><a href="#*upcase-attribute-strings*"><code>*upcase-attribute-strings*</code></a>
<li><a href="#*string-modifier*"><code>*string-modifier*</code></a>
<li><a href="#*template-symbol-package*"><code>*template-symbol-package*</code></a>
<li><a href="#*force-default*"><code>*force-default*</code></a>
<li><a href="#*value-access-function*"><code>*value-access-function*</code></a>
<li><a href="#*ignore-empty-lines*"><code>*ignore-empty-lines*</code></a>
<li><a href="#*warn-on-creation*"><code>*warn-on-creation*</code></a>
</ol>
<li><a href="#cond">Conditions</a>
<ol>
<li><a href="#template-error"><code>template-error</code></a>
<li><a href="#template-invocation-error"><code>template-invocation-error</code></a>
<li><a href="#template-missing-value-error"><code>template-missing-value-error</code></a>
<li><a href="#template-not-a-string-error"><code>template-not-a-string-error</code></a>
<li><a href="#template-not-a-string-error-value"><code>template-not-a-string-error-value</code></a>
<li><a href="#template-syntax-error"><code>template-syntax-error</code></a>
<li><a href="#template-syntax-error-stream"><code>template-syntax-error-stream</code></a>
<li><a href="#template-syntax-error-line"><code>template-syntax-error-line</code></a>
<li><a href="#template-syntax-error-col"><code>template-syntax-error-col</code></a>
</ol>
<li><a href="#esc">Escaping</a>
<ol>
<li><a href="#escape-string"><code>escape-string</code></a>
<li><a href="#*escape-char-p*"><code>*escape-char-p*</code></a>
<li><a href="#escape-string-minimal"><code>escape-string-minimal</code></a>
<li><a href="#escape-string-minimal-plus-quotes"><code>escape-string-minimal-plus-quotes</code></a>
<li><a href="#escape-string-iso-8859-1"><code>escape-string-iso-8859-1</code></a>
<li><a href="#escape-string-all"><code>escape-string-all</code></a>
</ol>
</ol>
<li><a href="#ack">Acknowledgements</a>
</ol>
<br> <br><h3><a name="example" class=none>Simple example</a></h3>
Although there's a wealth of functions, special variables, condition
types, and restarts listed <a href="#dictionary">below</a>, most of
the time you'll just have to deal with <a
href="#fill-and-print-template">one function</a> and the syntax of the
templates should be quite easy to grasp.
Here's a small example.
<p>
If you have a text file
<code>#p"/tmp/foo.tmpl"</code> like this
<pre>
<table border=1>
<font color=red><!-- TMPL_LOOP <b>rows</b> --></font>
<tr>
<font color=blue><!-- TMPL_LOOP <b>cols</b> --></font>
<font color=brown><!-- TMPL_IF <b>colorful-style</b> --></font>
<td align="right" bgcolor="pink"><font color=deeppink><!-- TMPL_VAR <b>content</b> --></font></td>
<font color=brown><!-- TMPL_ELSE --></font>
<td align="right" ><font color=deeppink><!-- TMPL_VAR <b>content</b> --></font></td>
<font color=brown><!-- /TMPL_IF --></font>
<font color=blue><!-- /TMPL_LOOP --></font>
</tr>
<font color=red><!-- /TMPL_LOOP --></font>
</table>
</pre>
then the following code
<pre>
(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))
</pre>
will produce this HTML table:
<p>
<table border=1>
<tr>
<td align="right">zero</td>
<td align="right" bgcolor="pink">one</td>
<td align="right">two</td>
<td align="right" bgcolor="pink">three</td>
<td align="right">four</td>
<td align="right" bgcolor="pink">five</td>
<td align="right">six</td>
</tr>
<tr>
<td align="right" bgcolor="pink">seven</td>
<td align="right">eight</td>
<td align="right" bgcolor="pink">nine</td>
<td align="right">ten</td>
<td align="right" bgcolor="pink">eleven</td>
<td align="right">twelve</td>
<td align="right" bgcolor="pink">thirteen</td>
</tr>
<tr>
<td align="right">fourteen</td>
<td align="right" bgcolor="pink">fifteen</td>
<td align="right">sixteen</td>
<td align="right" bgcolor="pink">seventeen</td>
<td align="right">eighteen</td>
<td align="right" bgcolor="pink">nineteen</td>
<td align="right">twenty</td>
</tr>
<tr>
<td align="right" bgcolor="pink">twenty-one</td>
<td align="right">twenty-two</td>
<td align="right" bgcolor="pink">twenty-three</td>
<td align="right">twenty-four</td>
<td align="right" bgcolor="pink">twenty-five</td>
<td align="right">twenty-six</td>
<td align="right" bgcolor="pink">twenty-seven</td>
</tr>
<tr>
<td align="right">twenty-eight</td>
<td align="right" bgcolor="pink">twenty-nine</td>
<td align="right">thirty</td>
<td align="right" bgcolor="pink">thirty-one</td>
<td align="right">thirty-two</td>
<td align="right" bgcolor="pink">thirty-three</td>
<td align="right">thirty-four</td>
</tr>
<tr>
<td align="right" bgcolor="pink">thirty-five</td>
<td align="right">thirty-six</td>
<td align="right" bgcolor="pink">thirty-seven</td>
<td align="right">thirty-eight</td>
<td align="right" bgcolor="pink">thirty-nine</td>
<td align="right">forty</td>
<td align="right" bgcolor="pink">forty-one</td>
</tr>
<tr>
<td align="right">forty-two</td>
<td align="right" bgcolor="pink">forty-three</td>
<td align="right">forty-four</td>
<td align="right" bgcolor="pink">forty-five</td>
<td align="right">forty-six</td>
<td align="right" bgcolor="pink">forty-seven</td>
<td align="right">forty-eight</td>
</tr>
</table>
<br> <br><h3><a name="install" class=none>Download and installation</a></h3>
HTML-TEMPLATE together with this documentation can be downloaded from <a
href="http://weitz.de/files/html-template.tar.gz">http://weitz.de/files/html-template.tar.gz</a>. The
current version is 0.7.0.
<p>
If you're on <a href="http://www.debian.org/">Debian</a> you should
probably use the <a
href="http://packages.debian.org/cgi-bin/search_packages.pl?keywords=cl-html-template&searchon=names&version=all&release=all">cl-html-template
Debian package</a> which is available thanks to <a href="http://pvaneynd.mailworks.org/">Peter van Eynde</a> and <a href="http://b9.com/">Kevin
Rosenberg</a>. There's also a port
for <a href="http://www.cliki.net/gentoo">Gentoo Linux</a> thanks to Matthew Kennedy.
<p>
HTML-TEMPLATE comes with simple system definitions for <a
href="http://www.cliki.net/mk-defsystem">MK:DEFSYSTEM</a> and <a
href="http://www.cliki.net/asdf">ASDF</a> 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
<code>(mk:compile-system "html-template")</code> (or the
equivalent one for asdf) which should compile and load the whole
system. Installation via <a
href="http://www.cliki.net/asdf-install">asdf-install</a> should also
be possible.
<p>
If for some reason you don't want to use MK:DEFSYSTEM or asdf you
can just <code>LOAD</code> the file <code>load.lisp</code> or you
can also get away with something like this:
<pre>
(loop for name in '("packages" "specials" "errors" "util" "template" "api")
do (compile-file (make-pathname :name name
:type "lisp"))
(load name))
</pre>
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:
<pre>
cat {packages,specials,errors,util,template,api}.x86f > html-template.x86f
</pre>
(Replace ".<code>x86f</code>" with the correct suffix for
your platform.)
<p>
The distribution includes a test file
"<code>test.lisp</code>" which you can <code>LOAD</code>
after loading HTML-TEMPLATE itself to check if everything works as
intended. If all is well you should just see these two messages:
<pre>
Please wait a couple of seconds.
All tests passed...
</pre>
Some of the tests assume the existence of a directory
<code>#p"/tmp/"</code> where you can create files. If you're on
Windows you should probably change this - see the variable
<code>TMP-DIR</code> in <code>test.lisp</code>.
<p>
Note that there is <em>no</em> public CVS repository for HTML-TEMPLATE - the repository at <a href="http://common-lisp.net/">common-lisp.net</a> is out of date and not in sync with the (current) version distributed from <a href="http://weitz.de/">weitz.de</a>.
<br> <br><h3><a name="mail" class=none>Support and mailing lists</a></h3>
For questions, bug reports, feature requests, improvements, or patches
please use the <a
href="http://common-lisp.net/mailman/listinfo/html-template-devel">html-template-devel
mailing list</a>. If you want to be notified about future releases
subscribe to the <a
href="http://common-lisp.net/mailman/listinfo/html-template-announce">html-template-announce
mailing list</a>. These mailing lists were made available thanks to
the services of <a href="http://common-lisp.net/">common-lisp.net</a>.
<br> <br><h3><a name="syntax" class=none>Syntax</a></h3>
A template is just ordinary text (a file or a string) interspersed
with <em>template tags</em>. A template tag looks like
<pre>
<!-- <em>name</em> [<em>attribute</em>] -->
</pre>
where <em>name</em> is one of <code>TMPL_VAR</code>,
<code>TMPL_LOOP</code>, <code>TMPL_REPEAT</code>, <code>TMPL_IF</code>, <code>TMPL_UNLESS</code>,
<code>TMPL_INCLUDE</code>, <code>/TMPL_LOOP</code>, <code>/TMPL_REPEAT</code>,
<code>/TMPL_IF</code>, <code>/TMPL_UNLESS</code>, or <code>TMPL_ELSE</code>. Case doesn't matter,
i.e. <code>tmpl_var</code> or <code>Tmpl_Var</code> would also be
legal names.
<p>
If <em>name</em> is one of the first four listed above then
<em>attribute</em> must follow, otherwise it must not follow where
<em>attribute</em> is any sequence of characters delimited by
<code>"</code>, <code>'</code>, or by whitespace. There's
currently no way to escape the delimiters, i.e. if the attribute
starts with <code>"</code> then the next <code>"</code> (and
nothing else) will end it.
<p>
Any amount (including the empty string) of whitespace directly after
'<code><!--</code>' and directly before '<code>--></code>' is
optional and will be ignored. However, at least one whitespace
character between <em>name</em> and <em>attribute</em> (if present) is
mandatory. But note that if <em>attribute</em> is not delimited by
<code>"</code> or <code>'</code> then there must be whitespace to
separate <em>attribute</em> from the closing '<code>--></code>'.
<p>
The following are examples for legal template tags
<pre>
<!-- TMPL_VAR foo -->
<!--TMPL_LOOP 'foo'-->
<!-- tmpl_include "/tmp/foo.html" -->
<!-- Tmpl_Else -->
<!-- /TMPL_LOOP -->
<!-- TMPL_LOOP foo--><!-- -->
</pre>
But note that in the last example the attribute is '<code>foo--><!--</code>' which is probably not what you expected...
<p>
These are <b>not</b> legal:
<pre>
<!-- TMPL_VAR -->
<!-- Tmpl_Else baz -->
<!-- TMPL_VARfoo -->
<!--TMPL_LOOP 'foo\'bar'-->
<tmpl_include "/tmp/foo.html">
<!-- TMPL_VAR NAME="foo" -->
</pre>
<code>TMPL_VAR</code> must always be followed by an attribute (1st
example) while <code>TMPL_ELSE</code> must not (2nd example). The
third one isn't recognized as a template tag because of the missing
whitespace behind <code>TMPL_VAR</code>. The tag will simply be
ignored and the parser will look for the next occurence of
'<code><!--</code>'.
<p>
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 "<code>bar'</code>" 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 <a
href="#*template-start-marker*"><code>*TEMPLATE-START-MARKER*</code></a>
and <a
href="#*template-end-marker*"><code>*TEMPLATE-END-MARKER*</code></a>.)
The last example uses HTML::Template's optional
"<code>NAME=</code>" notation which is not supported by
HTML-TEMPLATE.
<p>
The <code>TMPL_VAR</code> and <code>TMPL_INCLUDE</code> 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
<pre>
<!-- TMPL_IF <em>attribute</em> --> <em>text</em> <!-- /TMPL_IF -->
<!-- TMPL_IF <em>attribute</em> --> <em>text</em> <!-- TMPL_ELSE --> <em>text'</em> <!-- /TMPL_IF -->
<!-- TMPL_LOOP <em>attribute</em> --> <em>text</em> <!-- /TMPL_LOOP -->
<!-- TMPL_REPEAT <em>attribute</em> --> <em>text</em> <!-- /TMPL_REPEAT -->
</pre>
where <em>text</em> and <em>text'</em> themselves must be valid
templates. In other words: These constructs must nest properly.
<p>
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:
<pre>
<!-- Start of comment <!-- TMPL_VAR foo --> End of comment -->
<A HREF="<!-- TMPL_VAR link -->">foobar</A>
</pre>
<br> <br><h3><a name="semantics" class=none>Semantics</a></h3>
The generic function <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>
will convert a template into a <em>template printer</em>. A
<em>template printer</em> is a function which accepts one argument - a
<em>template structure</em> - which describes how the template should
be filled and prints the filled template to the stream bound to the
internal variable <code>*TEMPLATE-OUTPUT*</code>. You can
<a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_funcal.htm"><code>FUNCALL</code></a> a template printer, but the preferred way to use
it is to invoke it with <a
href="#fill-and-print-template"><code>FILL-AND-PRINT-TEMPLATE</code></a>
(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.
<p>
For the rest of this document we will make a distinction between
<em>generation time</em> which is the time the template printer is
created (either explicitely by <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>
or implicitely by <a
href="#fill-and-print-template"><code>FILL-AND-PRINT-TEMPLATE</code></a>)
and <em>invocation time</em> which is the time the printer is used (by
<a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_funcal.htm"><code>FUNCALL</code></a> or <a
href="#fill-and-print-template"><code>FILL-AND-PRINT-TEMPLATE</code></a>)
to fill and print a template.
<p>
Each of the template tags <code>TMPL_VAR</code>, <code>TMPL_IF</code>, <code>TMPL_UNLESS</code>,
<code>TMPL_LOOP</code>, and <code>TMPL_REPEAT</code> is associated with a particular symbol at
generation time. This symbol is the result of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_intern.htm"><code>INTERN</code></a>ing
the tag's attribute string into the package <a
href="#*template-symbol-package*"><code>*TEMPLATE-SYMBOL-PACKAGE*</code></a>. The
<em>template structure</em> - 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 <a
href="#*value-access-function*"><code>*VALUE-ACCESS-FUNCTION*</code></a>. (Note
that the name <em>structure</em> doesn't imply that template structures are <a href="http://www.lispworks.com/documentation/HyperSpec/Body/08_.htm">structures</a> in
the sense of Common Lisp. Usually they aren't.)
<p>
The template tags work as follows:
<p><br><b><code><!-- TMPL_VAR <em>symbol</em> --></code></b>
<blockquote>
This tag will be replaced by the value associated with <em>symbol</em>
which should be (see <a href="#*format-non-strings*"><code>*FORMAT-NON-STRINGS*</code></a>) a string (or maybe <code>NIL</code> - see <a
href="#*convert-nil-to-empty-string*"><code>*CONVERT-NIL-TO-EMPTY-STRING*</code></a>). <a href="#*string-modifier*"><code>*STRING-MODIFIER*</code></a> is applied to the string before it is output.
<pre>
* (let ((tp (create-template-printer "Hello <!-- TMPL_VAR foo -->!")))
(fill-and-print-template tp '(:foo "World"))
(terpri)
(fill-and-print-template tp '(:foo "Folks"))
(terpri)
(fill-and-print-template tp '(:foo symbol)))
Hello World!
Hello Folks!
Hello SYMBOL!
</pre>
</blockquote>
<p><b><code><!-- TMPL_IF <em>symbol</em> --><em>text</em><!-- /TMPL_IF --></code></b>
<br><b><code><!-- TMPL_UNLESS <em>symbol</em> --><em>text</em><!-- /TMPL_UNLESS --></code></b>
<blockquote>
In the first case, if the value associated with
<em>symbol</em> is not <code>NIL</code> the (sub-)template
<em>text</em> 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.
<pre>
* (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
</pre>
</blockquote>
<p><b><code><!-- TMPL_IF <em>symbol</em> --><em>text</em><!-- TMPL_ELSE --><em>text'</em><!-- /TMPL_IF --></code></b>
<br><b><code><!-- TMPL_UNLESS <em>symbol</em> --><em>text</em><!-- TMPL_ELSE --><em>text'</em><!-- /TMPL_UNLESS --></code></b>
<blockquote>
In the first case, if the value associated with <em>symbol</em> is
not <code>NIL</code>, the (sub-)template <em>text</em> will be filled
and printed. Otherwise,
<em>text'</em> is used instead. In the second case, it's the other
way around.
<pre>
* (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
</pre>
</blockquote>
<p><b><code><!-- TMPL_LOOP <em>symbol</em> --><em>text</em><!-- /TMPL_LOOP --></code></b>
<blockquote>
The value associated with <em>symbol</em> should be
a sequence (see <a
href="#*sequences-are-lists*"><code>*SEQUENCES-ARE-LISTS*</code></a>) of
template structures. For each element of this sequence the
(sub-)template <em>text</em> is filled and printed using the
corresponding template structure.
<p>
Note that each template (sub-)structure which is used to fill
<em>text</em> introduces a new set of associations between symbols and
their values. While the template printer is within <em>text</em> the
outer template structure is temporarily "forgotten"
unless <a
href="#*value-access-function*"><code>*VALUE-ACCESS-FUNCTION*</code></a>
(note the <code><i>in-loop-p</i></code> paramter in particular) takes
care of that.
<pre>
* (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]
* (let ((*value-access-function*
(lambda (symbol values &optional in-loop-p)
(declare (ignore in-loop-p))
(getf values symbol))))
(fill-and-print-template *tp* '(:baz "ONE"
:foo ((:bar "EINS")
(:bar "UNO")))))
[EINS,][UNO,]
* (fill-and-print-template *tp* '(:baz "ONE"
:foo ((:bar "EINS")
(:bar "UNO"))))
[EINS,ONE][UNO,ONE]
</pre>
</blockquote>
<p><b><code><!-- TMPL_REPEAT <em>symbol</em> --><em>text</em><!-- /TMPL_REPEAT --></code></b>
<blockquote>
If the value associated with <em>symbol</em> is a positive integer <code>N</code>, then the (sub-)template <em>text</em> will be filled and printed <code>N</code> times. Otherwise, the whole construct will be replace with an empty string.
<pre>
* (let ((tp (create-template-printer "The <!-- TMPL_REPEAT three -->very <!-- /TMPL_REPEAT -->fast brown fox")))
(fill-and-print-template tp '(:three 3))
(terpri)
(fill-and-print-template tp '(:three "3")))
The very very very fast brown fox
The fast brown fox
</pre>
Note that <a href="http://html-template.sourceforge.net/">the original HTML::Template</a> library doesn't have the <code>TMPL_REPEAT</code> tag - if that matters to you.
</blockquote>
<p><b><code><!-- TMPL_INCLUDE <em>pathname</em> --></code></b>
<blockquote>
The string <em>pathname</em>
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 to <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>. At
invocation time the tag will be replaced by the result
of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_funcal.htm"><code>FUNCALL</code></a>ing 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 a
<code>TMPL_IF</code> tag in your main file and put the closing
<code>/TMPL_IF</code> into the included file.)
<pre>
* (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 dog
</pre>
<p>
These tags can be nested, i.e. included files can themselves include
other files. Included template printers are <em>always</em> taken from the <a href="#cache">cache</a> at
invocation time which means you can update them seperately from the
including printer.
</blockquote>
<br> <br><h3><a class=none name="dictionary">The HTML-TEMPLATE dictionary</a></h3>
HTML-TEMPLATE exports the following symbols (some of which are also
exported by <a href="http://weitz.de/cl-who/">CL-WHO</a>, by the way,
so beware if you're using both libraries):
<h4><a name="main" class=none>Creating and using template printers</a></h4>
If you're OK with the default settings you will probably only use the
two functions described in this section.
<p><br>[Generic function]
<br><a class=none name="create-template-printer"><b>create-template-printer</b> <i>template <tt>&key</tt> force element-type if-does-not-exist external-format</i> => <i>printer</i></a>
<blockquote><br>
This function will create and return a <a
href="#semantics">template printer</a> <code><i>printer</i></code> created from the template denoted by
<code><i>template</i></code>. The behaviour of this function depends on the
type of <code><i>template</i></code>.
<ul>
<li>If <code><i>template</i></code> is a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/t_stream.htm"><b>stream</b></a>, it should be an open character input stream which is read character by character with <a href="http://www.lispworks.com/reference/HyperSpec/Body/f_rd_cha.htm"><code>READ-CHAR</code></a> and the resulting text is used as the template.
<li>If <code><i>template</i></code> is a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/t_string.htm"><b>string</b></a>, it is converted into a <a href="http://www.lispworks.com/reference/HyperSpec/Body/t_stg_st.htm">string stream</a> which is again fed into <code>CREATE-TEMPLATE-PRINTER</code>.
<li>If <code><i>template</i></code> is a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/t_pn.htm"><b>pathname</b></a>, it should denote an existing text file. The file is opened by <a href="http://www.lispworks.com/reference/HyperSpec/Body/m_w_open.htm"><code>WITH-OPEN-FILE</code></a> and the resulting stream is again fed into <code>CREATE-TEMPLATE-PRINTER</code>. All keyword arguments except <code><i>force</i></code> are used as keyword arguments for <code>WITH-OPEN-FILE</code>. The pathname will be <a href="http://www.lispworks.com/reference/HyperSpec/Body/f_merge_.htm">merged</a> with <a
href="#*default-template-pathname*"><code>*DEFAULT-TEMPLATE-PATHNAME*</code></a> before it is used.
<p>HTML-TEMPLATE maintains a <a href="#cache"><em>cache</em></a> 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 <a href="http://www.lispworks.com/reference/HyperSpec/Body/f_file_w.htm"><code>FILE-WRITE-DATE</code></a>) the cached value will be used and no new template printer will be created. This can be overriden by the keyword argument <code><i>force</i></code>, i.e. when <code><i>force</i></code> is true a new template printer will unconditionally be created (and cached unless <code><i>force</i></code> is the keyword <code>:DO-NOT-CACHE</code>). The default value for <code><i>force</i></code> is the value of <a href="#*force-default*"><code>*FORCE-DEFAULT*</code></a>. (See also <a
href="#*no-cache-check*"><code>*NO-CACHE-CHECK*</code></a>.) Note that you may have to use <code><i>force</i></code> if you've changed one of the customization variables described <a href="#custom">below</a> and want to create a template printer based on these new settings although the template file itself hasn't changed. Also note that <code>FILE-WRITE-DATE</code> might not be able to see a difference if the newer version of a file is only fractions of a second newer than the older one.
</ul>
This function will signal an error of type <a href="#template-invocation-error"><code>TEMPLATE-INVOCATION-ERROR</code></a> if <code><i>template</i></code> is not a pathname and one of the keyword arguments is provided.
<pre>
* (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"
</pre>
</blockquote>
<p><br>[Generic function]
<br><a class=none name="fill-and-print-template"><b>fill-and-print-template</b> <i>template/printer values <tt>&key</tt> stream <tt>&allow-other-keys</tt></i> => <i>|</i></a>
<blockquote><br>
This function will fill the template denoted by <code><i>template/printer</i></code> with the values provided by <code><i>values</i></code> and print the resulting text to <code><i>stream</i></code> which defaults to <a
href="#*default-template-output*"><code>*DEFAULT-TEMPLATE-OUTPUT*</code></a>. The value of <code><i>values</i></code> should be a <a
href="#semantics">template structure</a> matching the current value of <a
href="#*value-access-function*"><code>*VALUE-ACCESS-FUNCTION*</code></a>.
<p>
If <code><i>template/printer</i></code> is a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/t_fn.htm">function</a>, it will be used as if it were a template printer. Otherwise, <code><i>template/printer</i></code> will first be fed into <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a> and the resulting template printer will be used. Note that this implies that the caching mechanism <a href="#create-template-printer">described above</a> is in effect here as well.
<p>
If <code><i>template/printer</i></code> is a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/t_pn.htm">pathname</a>, all keyword arguments except for <code><i>stream</i></code> will be used as keyword arguments for <a href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>. If it is not a pathname, keyword arguments other than <code><i>stream</i></code> will result in an error of type <a href="#template-invocation-error"><code>TEMPLATE-INVOCATION-ERROR</code></a>.
<pre>
* (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
</pre>
</blockquote>
<h4><a name="cache" class=none>Template cache</a></h4>
The functions and variables in this section are related to the cache mechanism described in the entry for <a href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>.
<p><br>[Function]
<br><a class=none name="clear-template-cache"><b>clear-template-cache</b> => <i>|</i></a>
<blockquote><br>
This function will completely clear the <a href="#create-template-printer">cache</a> used by HTML-TEMPLATE.
</blockquote>
<p><br>[Function]
<br><a class=none name="delete-from-template-cache"><b>delete-from-template-cache</b> <i>pathname</i> => <i>result</i></a>
<blockquote><br>
This function will remove the template printer associated with <code><i>pathname</i></code> from HTML-TEMPLATE's <a href="#create-template-printer">cache</a>. <code><i>result</i></code> is <em>true</em> if there was such a template printer, or <code>NIL</code> otherwise.
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*no-cache-check*"><b>*no-cache-check*</b></a>
<blockquote><br>
If the value of this variable is <em>true</em> (the default is
<code>NIL</code>) <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>
and <a
href="#fill-and-print-template"><code>FILL-AND-PRINT-TEMPLATE</code></a>
won'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.
</blockquote>
<h4><a name="custom" class=none>Customizations</a></h4>
This section assembles more than a
dozen <a
href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_s.htm#special_variable">special
variables</a> which can be used to customize HTML-TEMPLATE's behaviour.
<p><br>[Special variable]
<br><a class=none name="*template-start-marker*"><b>*template-start-marker*</b></a>
<blockquote><br>
This should be a string (the default is <code>"<!--"</code>) which is used at <a href="#semantics">generation time</a> to determine the start of a template tag.
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*template-end-marker*"><b>*template-end-marker*</b></a>
<blockquote><br>
This should be a string (the default is
<code>"-->"</code>) which is used at <a
href="#semantics">generation time</a> to determine the end of
a template tag.
<pre>
* (let ((*template-start-marker* "<")
(*template-end-marker* ">"))
(fill-and-print-template "The <TMPL_VAR 'speed'> <brown> fox" '(:speed "quick")))
The quick <brown> fox
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*default-template-pathname*"><b>*default-template-pathname*</b></a>
<blockquote><br>
This should be a pathname (the default is the result of calling <a
href="http://www.lispworks.com/reference/HyperSpec/Body/f_mk_pn.htm"><code>MAKE-PATHNAME</code></a>
with no arguments) which is merged with the sole argument of <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>
if this argument is a pathname.
<pre>
* (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
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*default-template-output*"><b>*default-template-output*</b></a>
<blockquote><br>
This should be a stream (the default is the value of
<a
href="http://www.lispworks.com/documentation/HyperSpec/Body/v_debug_.htm#STstandard-outputST"><code>*STANDARD-OUTPUT*</code></a>
when HTML-TEMPLATE is loaded) which is used as the output stream of
<a
href="#fill-and-print-template"><code>FILL-AND-PRINT-TEMPLATE</code></a>
if no <code><i>stream</i></code> keyword argument was provided.
<pre>
* (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"
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*convert-nil-to-empty-string*"><b>*convert-nil-to-empty-string*</b></a>
<blockquote><br>
If the values of this variable is <em>true</em> (which is the default), <code>TMPL_VAR</code>
tags will be replaced by the empty string if the associated value is
<code>NIL</code>, otherwise an error of type <a
href="#template-missing-value-error"><code>TEMPLATE-MISSING-VALUE-ERROR</code></a>
is signaled. This variable takes
effect at <a href="#semantics">invocation time</a>.
<pre>
* (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
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*format-non-strings*"><b>*format-non-strings*</b></a>
<blockquote><br> If the value of this variable is <em>true</em> (which
is the default), <code>TMPL_VAR</code> will accept non-string values
and convert them to strings
using <code>(FORMAT NIL "~A" ...)</code>. Note that
the check
for <a
href="#*convert-nil-to-empty-string*"><code>*CONVERT-NIL-TO-EMPTY-STRING*</code></a>
will happen first, though.
This variable takes effect at <a
href="#semantics">invocation time</a>.
<pre>
* (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed :tardy))
The TARDY brown fox
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*sequences-are-lists*"><b>*sequences-are-lists*</b></a>
<blockquote><br>
If the values of this variable is <em>true</em> (which is the default)
the code generated by a <a
href="#semantics"><code>TMPL_LOOP</code></a> tag expects its
associated value to be a list, otherwise it expects it to be a
vector. This variable takes effect at <a href="#semantics">generation time</a>.
<pre>
* (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]
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*upcase-attribute-strings*"><b>*upcase-attribute-strings*</b></a>
<blockquote><br>
If the values of this variable is <em>true</em> (which is the default)
<a href="#syntax">attribute strings</a> are fed to
<code>STRING-UPCASE</code> before they are interned. This variable
takes effect at <a href="#semantics">generation time</a>.
<pre>
* (let ((*upcase-attribute-strings* nil))
(fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "quick" :|speed| "slow")))
The slow brown fox
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*string-modifier*"><b>*string-modifier*</b></a>
<blockquote><br> A <a href="http://www.lispworks.com/documentation/HyperSpec/Body/01_dae.htm">designator</a> for the function which is applied to strings which
replace <code>TMPL_VAR</code> tags. The default
is <a
href="#escape-string-iso-8859-1"><code>#'ESCAPE-STRING-ISO-8859-1</code></a>. Use <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_identi.htm"><code>#'CL:IDENTITY</code></a> if you want to leave the string as is.
<pre>
* (fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "<quick>"))
The &lt;quick&gt; brown fox
* (let ((*string-modifier* #'identity))
(fill-and-print-template "The <!-- TMPL_VAR speed --> brown fox" '(:speed "<quick>")))
The <quick> brown fox
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*template-symbol-package*"><b>*template-symbol-package*</b></a>
<blockquote><br> The value of this variable should be
a <a
href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_p.htm#package_designator">package
designator</a> designating the package <a href="#syntax">attribute
strings</a> are interned into. The default is the KEYWORD
package. This variable takes effect at <a href="#semantics">generation
time</a>.
<pre>
* *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
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*force-default*"><b>*force-default*</b></a>
<blockquote><br>
The default value for the <code><i>force</i></code> keyword argument to
<a href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>. Its initial value is <code>NIL</code>.
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*value-access-function*"><b>*value-access-function*</b></a>
<blockquote><br> The value of this variable should be
a <a
href="http://www.lispworks.com/documentation/HyperSpec/Body/01_dae.htm">designator</a>
for a function with
the <a
href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_l.htm#lambda_list">lambda
list</a> <code><i>(symbol values <tt>&optional</tt> in-loop-p)</i></code> which is
used to associate symbols with their values when a template printer is
invoked. <code><i>in-loop-p</i></code> is true whenever this
function is called from within a <code>TMPL_LOOP</code> tag.
<p>
The default
value is
<pre>
(lambda (symbol values &optional in-loop-p)
(let ((result (getf values symbol)))
(cond (in-loop-p
(loop for element in result
when (listp element)
<font color=orange>;; keep values from upper levels</font>
collect (append element values)
else
collect element))
(t result))))
</pre>
This variable takes effect at <a href="#semantics">invocation
time</a>.
<pre>
* (let ((tp (create-template-printer "The <!-- TMPL_VAR speed --> brown fox"))
<font color=orange>;; for brevity, we'll ignore the third argument here</font>
(*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
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*ignore-empty-lines*"><b>*ignore-empty-lines*</b></a>
<blockquote><br>
If the value of this variable is <em>true</em> (the default is
<code>NIL</code>), template printers will suppress any whitespace in
front of template tags up to (but excluding) the first
<code>#\Newline</code> and any whitespace behind template tags up to
(and including) the first <code>#\Newline</code>. This holds for all
tags except <code>TMPL_VAR</code>. The idea is that you might want to
put tags like <code>TMPL_LOOP</code> on 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
<a href="#semantics">generation time</a>.
<pre>
* (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>
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*warn-on-creation*"><b>*warn-on-creation*</b></a>
<blockquote><br>
If this variable is <em>true</em> (which is the default), <a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>
will <a
href="http://www.lispworks.com/reference/HyperSpec/Body/f_warn.htm">warn</a>
you whenever a template printer is newly created from a pathname
argument instead of being taken from the <a href="#create-template-printer">cache</a>.
</blockquote>
<h4><a name="cond" class=none>Conditions</a></h4>
This section lists
the <a
href="http://www.lispworks.com/documentation/HyperSpec/Body/09_.htm">conditions</a>
signaled by HTML-TEMPLATE and the corresponding accessors.
<p><br>[Condition type]
<br><a class=none name="template-error"><b>template-error</b></a>
<blockquote><br>
Every error signaled by HTML-TEMPLATE is of type
<code>TEMPLATE-ERROR</code>. This is a direct subtype of <a
href="http://www.lispworks.com/reference/HyperSpec/Body/e_smp_er.htm"><code>SIMPLE-ERROR</code></a>
without any additional slots or options.
</blockquote>
<p><br>[Condition type]
<br><a class=none name="template-invocation-error"><b>template-invocation-error</b></a>
<blockquote><br>
Errors of type <code>TEMPLATE-INVOCATION-ERROR</code> are signaled if
<a
href="#create-template-printer"><code>CREATE-TEMPLATE-PRINTER</code></a>
or <a
href="#fill-and-print-template"><code>FILL-AND-PRINT-TEMPLATE</code></a>
are called with wrong keyword arguments. This is a direct subtype of
<a href="#template-error"><code>TEMPLATE-ERROR</code></a> without any
additional slots or options.
</blockquote>
<p><br>[Condition type]
<br><a class=none name="template-missing-value-error"><b>template-missing-value-error</b></a>
<blockquote><br>
An error of type <code>TEMPLATE-MISSING-VALUE-ERROR</code> is signaled
if a template printer for <code>TMPL_VAR</code> is provided with a
<code>NIL</code> value although <a
href="#*convert-nil-to-empty-string*"><code>*CONVERT-NIL-TO-EMPTY-STRING*</code></a>
is <em>false</em>. This is a direct subtype of <a
href="#template-error"><code>TEMPLATE-ERROR</code></a> without any
additional slots or options. Whenever a
<code>TEMPLATE-MISSING-VALUE-ERROR</code> is signaled, an associated
<a href="http://www.lispworks.com/documentation/HyperSpec/Body/r_use_va.htm"><code>USE-VALUE</code> restart</a> is available.
</blockquote>
<p><br>[Condition type]
<br><a class=none name="template-not-a-string-error"><b>template-not-a-string-error</b></a>
<blockquote><br>
An error of type <code>TEMPLATE-NOT-A-STRING-ERROR</code> is signaled
if a template printer for <code>TMPL_VAR</code> is provided with a
value which is neither a string nor <code>NIL</code> and <a href="#*format-non-strings*"><code>*FORMAT-NON-STRINGS*</code></a> is <em>false</em>. This is a
direct subtype of <a
href="#template-error"><code>TEMPLATE-ERROR</code></a> with one
additional slot for the value which can be read by <a
href="#template-not-a-string-error-value"><code>TEMPLATE-NOT-A-STRING-ERROR-VALUE</code></a>.
Whenever a <code>TEMPLATE-NOT-A-STRING-ERROR</code> is signaled, an
associated <a href="http://www.lispworks.com/documentation/HyperSpec/Body/r_use_va.htm"><code>USE-VALUE</code> restart</a> is available.
</blockquote>
<p><br>[Generic function]
<br><a class=none name="template-not-a-string-error-value"><b>template-not-a-string-error-value</b></a> <i>condition</i> => <i>value</i>
<blockquote><br>
If <code><i>condition</i></code> is a condition of type <a
href="#template-not-a-string-error"><code>TEMPLATE-NOT-A-STRING-ERROR</code></a>,
this function will return the (non-string) value causing the error.
<pre>
* (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))))))
(let ((*format-non-strings* nil))
(fill-and-print-template tp '(:number 4)))))
A square has four corners
</pre>
</blockquote>
<p><br>[Condition type]
<br><a class=none name="template-syntax-error"><b>template-syntax-error</b></a>
<blockquote><br>
An error of type <code>TEMPLATE-SYNTAX-ERROR</code> is signaled at <a
href="#semantics">generation time</a> when HTML-TEMPLATE is
not able to create a template printer due to syntax errors in the
template. This is a direct subtype of <a
href="#template-error"><code>TEMPLATE-ERROR</code></a> with 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.)
<p>
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.
<pre>
* (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'.
</pre>
Note that column 26 is the position directly behind <code>"TMPL_VAR"</code>.
</blockquote>
<p><br>[Generic function]
<br><a class=none name="template-syntax-error-stream"><b>template-syntax-error-stream</b></a> <i>condition</i> => <i>stream</i>
<blockquote><br>
If <code><i>condition</i></code> is a condition of type <a
href="#template-syntax-error"><code>TEMPLATE-SYNTAX-ERROR</code></a>,
this function will return the stream the parser was reading from when
the error was encountered.
</blockquote>
<p><br>[Generic function]
<br><a class=none name="template-syntax-error-line"><b>template-syntax-error-line</b></a> <i>condition</i> => <i>number</i>
<blockquote><br>
If <code><i>condition</i></code> is a condition of type <a
href="#template-syntax-error"><code>TEMPLATE-SYNTAX-ERROR</code></a>,
this function will return the line number which was associated with
this error. As in <a href="http://www.gnu.org/software/emacs/">Emacs</a>, lines are counted beginning
with 1. HTML-TEMPLATE increases the line counter whenever it reads a <code>#\Newline</code> from its input stream.
</blockquote>
<p><br>[Generic function]
<br><a class=none name="template-syntax-error-col"><b>template-syntax-error-col</b></a> <i>condition</i> => <i>number</i>
<blockquote><br>
If <code><i>condition</i></code> is a condition of type <a
href="#template-syntax-error"><code>TEMPLATE-SYNTAX-ERROR</code></a>,
this function will return the column number which was associated with
this error. As in Emacs, columns are counted beginning
with 0.
</blockquote>
<h4><a name="esc" class=none>Escaping</a></h4>
Functions and variables which can be used to encode characters for
HTML documents -
see <a href="#*string-modifier*"><code>*STRING-MODIFIER*</code></a>.
<p><br>[Function]
<br><a class=none name="escape-string"><b>escape-string</b></a> <i>string <tt>&key</tt> test</i> => <i>escaped-string</i>
<blockquote><br>
This function will accept a string <code><i>string</i></code> and will replace every character for which <code><i>test</i></code> returns <em>true</em> with its (decimal) character entity. <code><i>test</i></code> must be a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/01_dae.htm">designator</a> for a function of one argument which accepts a character and returns a <a href="http://www.lispworks.com/reference/HyperSpec/Body/26_glo_g.htm#generalized_boolean">generalized boolean</a>. The default is the value of <a href="#*escape-char-p*"><code>*ESCAPE-CHAR-P*</code></a>.
<pre>
* (escape-string "<Hhner> 'nave'")
"&lt;H&#252;hner&gt; &#039;na&#239;ve&#039;"
</pre>
</blockquote>
<p><br>[Special variable]
<br><a class=none name="*escape-char-p*"><b>*escape-char-p*</b></a>
<blockquote><br>
This is the default for the <code><i>test</i></code> keyword argument to <a href="#escape-string"><code>ESCAPE-STRING</code></a>. Its initial value is
<pre>
#'(lambda (char)
(or (find char "<>&'\"")
(> (char-code char) 127)))
</pre>
</blockquote>
<p><br>[Function]
<br><a class=none name="escape-string-minimal"><b>escape-string-minimal</b> <i>string</i> => <i>escaped-string</i></a>
<br>[Function]
<br><a class=none name="escape-string-minimal-plus-quotes"><b>escape-string-minimal-plus-quotes</b> <i>string</i> => <i>escaped-string</i></a>
<br>[Function]
<br><a class=none name="escape-string-iso-8859-1"><b>escape-string-iso-8859-1</b> <i>string</i> => <i>escaped-string</i></a>
<br>[Function]
<br><a class=none name="escape-string-all"><b>escape-string-all</b> <i>string</i> => <i>escaped-string</i></a>
<blockquote><br>
These are convenience function based on <a href="#escape-string"><code>ESCAPE-STRING</code></a>. They are defined as follows:
<pre>
(defun escape-string-minimal (string)
"Escape only #\<, #\>, and #\& in STRING."
(escape-string string :test #'(lambda (char) (find char "<>&"))))
(defun escape-string-minimal-plus-quotes (string)
"Like ESCAPE-STRING-MINIMAL but also escapes quotes."
(escape-string string :test #'(lambda (char) (find char "<>&'\""))))
(defun escape-string-iso-8859-1 (string)
"Escapes all characters in STRING which aren't defined in ISO-8859-1."
(escape-string string :test #'(lambda (char)
(or (find char "<>&'\"")
(> (char-code char) 255)))))
(defun escape-string-all (string)
"Escapes all characters in STRING which aren't in the 7-bit ASCII
character set."
(escape-string string :test #'(lambda (char)
(or (find char "<>&'\"")
(> (char-code char) 127)))))
</pre>
</blockquote>
<br> <br><h3><a class=none name="ack">Acknowledgements</a></h3>
Thanks to Sam Tregar for <a
href="http://html-template.sf.net/">HTML::Template</a> which I've used
successfully for many years together with mod_perl and which inspired
me to write HTML-TEMPLATE.
<p>
Thanks to James Anderson and Kent M. Pitman who helped to de-confuse
me about restarts.
<p>
$Header: /usr/local/cvsrep/html-template/doc/index.html,v 1.47 2006/09/29 22:36:28 edi Exp $
<p><a href="http://weitz.de/index.html">BACK TO MY HOMEPAGE</a>
</body>
</html>
|