Using Template Toolkit for Web Pages


PCS Web Design Guide

Template Driven Web Pages

Motivation

In looking over the PCS HTML document library, it occured to me that much of the PCS generated HTML code, on a line-by-line basis, was boiler-plate code that varied little if at all between different files. This part also often contained most of the most complicated or elaborate bits of HTML code in these documents. E.g., formatting the logo and page title, etc. Most of the rest of the documents are mainly long boring text, maybe some tables.

In the old way of doing things, we simply copied a template file and inserted the textual content desired (or copied another page built in a similar fashion, deleted the half or so of it that was not boiler-plate, and inserted our textual content). Even if we used the most current version of the template each time we added a new file (which is questionable assumption), any little tweaks to the basic template only affected files created after the tweak, which meant the look-and-feel was slowly diverging as pages aged.

In looking at this situation, it occurred that using a more dynamic template would be preferred. This could be achieved by using a preprocessor which would enable us to write inputs to the preprocessor to become html files. These inputs would almost entirely be content --- 90% or more of the lines would contain the real content not boiler-plate. For example, at the time of this writing, the preprocessor input for the link_it(index.html,index.html file for this directory) consists of 18 lines, 4 of which are boiler plate, and 3 blank lines, leaving 11 with real content (about 61%). Because the index file tends to be small, it will typically have the worst real content to boiler plate ratios (in either preprocessor or html versions). The html file generated from that was about 80 lines, 11 of which were blank, and still only about the same 11 lines of real content (about 13%).

Another major advantage is that these input files could be processed again trivially at a later date if some of the boiler plate changes. E.g., if we wished to change what links are displayed in the footer at some point, without the templates it would mean editting every file with the footer. With the templates, we merely update the template defining the footer, and do a forced re-make on all the templates. The templates also can allow for more consistent customized boilerplate; by making the template macros more general, we can have multiple versions of parts of the template. E.g., the footers with and without the w3c validation links. The PCS links displayed in the footer are defined in a single place for both variants.

The templating process offers some other advantages as well. One in particular is the ability to add hooks in the templates to enable the pages to generate semi-automated page indexing.


Main Physics Dept site Main UMD site


Valid HTML 4.01! Valid CSS!