Version 2 of TclHttpd Templates

Updated 2004-04-15 01:56:44

TclHttpd uses subst to generate web pages from .tml files, optionally caching the results.

First, all .tml files back to the document root are sourced into the template interpreter (which may be the global interprester, or another interpreter dedicated to template expansion.)


Extended Templates - Aggregation

In a recent modification/hack, a ${template}.tml is permitted to be a directory. In that case, a file ${template}.tml/index.tml is evaluated, and its result returned to the client (after potentially being cached.)

This means that a template can, conceptually, be a collection of other files, and provides a kind of file context for processing. An example of use might be to provide a summary or assembled page from these components.

Since TclHttpd will default to caching the result of evaluating ${template}.tml/index.tml as ${template}.html, and will compare the cached version against the last modification time of the whole ${template}.tml/ directory, this provides an efficient mechanism for aggregation.

Note that any directory's index.html file will now be compared by modification time to that of the whole directory, when considering generation from index.tml, thus any directory can be considered as a collection and aggregated, but since changes to a subdirectory do not propagate to its parent directory, index.tml aggregation doesn't propagate.

Use of index.tml, in combination with ${template}.tml/ generation, you can control the propagation of changes from a sub-collection to collections in which they are contained.


Hints and Tricks

Access to per-socket data: A lot of tclhttpd context is stored in a global array called Httpd$sock where socket is the file descriptor of the current connection. To access this data, you can use upvar #0 Httpd[Httpd_CurrentSocket] data and reference the array as data.

Working Directory: to discover the directory containing the current template file use file dirname $page(filename). TclHttpd doesn't itself presume anything about the current working directory.