Building a Static Site with Template Toolkit
Template Toolkit is great for dynamic sites, but it can also make the task of keeping a static site up-to-date. Perl Guru Randal Schwartz sings TT's virtues while building a site for budding karaoke stars.
As described in previous articles, I’m a big fan of the Template Toolkit (or TT for short). I frequently use TT to generate dynamic web pages based on input parameters, but TT can help static web sites as well. Let’s take a look at a typical small, static website and how TT can help things.
Now Appearing: Randal
I’m a karaoke singer. I admit it. Karaoke helped get me through my initial stage fright when I started teaching: moving from behind the keyboard to the front of a room wasn’t what I had in mind when I studied software design in my youth. Karaoke also got me through the initial potential homesickness when I started travelling to teach: no matter what city I ended up in, I could find at least one night at a Karaoke Bar during the week, and things would seem “familiar.” And yes, there are two reasons you clap at the end of every singer’s performance: you’re either happy they sang or you’re happy they’re done!
Many of the local Karaoke Bars in my home area are serviced by a company — call it The Music Guy — that has the beginnings of a good web site, but fails to keep the site up to date or even consistent. I can understand that: it’s hard to synchronize the list of places a person working for the company will appear with the list of people that appear at each place. And then there’s the “look” of each page: maintaining the same navigation and fonts and colors can be troublesome. Cascaded Style Sheets (CSS) fix some of that, but you still have to have consistent HTML on which to hang the CSS.
This is a perfect application for a static web site built by a templating system. And Template Toolkit Version 2 (TT2) even includes a nice tool (ttree) to take a file hierarchy and run some of the files as templates, while simply copying or ignoring others.
So, I sketched up a sample web site for The Music Guy using ttree and some dummy text. Let’s explore the result.
Swinging from the Tree
To control ttree for a project of any size, you need a ttreerc file. The simplest way to use ttree for a project is to set the TTREERC environment variable to point at a file for the project. Yes, this is inconvenient: apparently Andy Wardley thinks you’re only ever working on one thing at a time. My sample ttreerc file is given in Listing One.