Templating Tools


PCS Web Design Guide

Template Driven Web Pages

Overview of Some Templating Tools

There are a number of tools that exist that enable us to do templating of our web pages. In evaluating these, there are a number of factors that should be considered:

The first forray into templating static web pages in Physics involved the m4 preprocessor. While the results of this were generally successful, the difficulty in programming the preprocessor was an issue.

At the time of this writing, the preferred method is to use perl in conjunction with the Template Toolkit package of modules. Although the m4 templates are still on the system, all new pages or migrated pages should be done with the template toolkit package.

The Perl Template Toolkit

The Perl Template Toolkit is a package of modules which can be added to Perl. Although it is probably most commonly used in the generation of dynamic content from CGI or mod_perl scripts, it is a truly generalized templating system and is not even limitted to templating HTML. It has its own object orientated language, which while not elaborate, is sufficient for most tasks that would come up in templating output. And it can fairly readily make use of Perl modules and objects, thereby leveraging great functionality when needed.

There are actually a number of Perl modules relating to templating, of which the Template Toolkit is but one. Template Toolkit offers several advantages over the others, in that:

HTML::Mason is another templating package for perl, but as implied by the name is very HTML/web orientated. Physics uses it for the RT request system, but that is all.

The template toolkit is very powerful because can easily get the full power of perl behind it. It is reasonable fast. Like PHP and other templating languages, the code is sort of intermixed with the literal content. Unlike m4 (but like most other solutions), this is done by having special tags to start and stop processing of the template language. This makes the template a little more verbose, and decreases the ease of use in designing new content, but greatly enhances the programmability of the language.

Templating with the M4 preprocessor

A previous attempt (now deprecated) of preprocessing of web pages in Physics made use of the m4 preprocessor. This is a very fast and fairly powerful preprocessor engine, however it is not the friendliest thing to work with.

Being a preprocessor, it is not as powerful as the template toolkit solution. It is possible, for example, to write macros in m4 that do looping, but there is no built in loop command. Also, the distinction between the m4 macros and the literal text is not as obvious; the m4 preprocessor reads the input text, parses into words by punctuation and spaces, sees if any of the words match a macro name, and if they do expand them. When everything is working properly, this actually makes entering new content a bit nicer; macros and functions are written rather naturally, without having to transition back and forth from template command and literal modes.

However, these ease of input comes at a cost. First off, it is possible to have the m4 preprocessor expand a "macro" where the user did not intend a macro. E.g., if we defined a h1 macro to wrap its arguments in h1 tags, this would allow us to simply write something like

h1(This is a title)
This however could cause problems if the user wrote an h1 tag in pure HTML somewhere in the input, as the h1 in the tag might get interpretted as a macro to be expanded.

The more serious problem is that defining macros became a major pain. In particular, determining exactly how many times something should be quoted to ensure that it was macro expanded at the correct time to do what was desired was quite tedious, non-obvious, and difficult to debug.

Why Template Toolkit over M4

The shift to the perl Template Toolkit is motivated by:


Main Physics Dept site Main UMD site


Valid HTML 4.01! Valid CSS!