HTML Templates

HTML Template is a new solution to an old problem – Perl CGI development. In order to explain what makes HTML Template new I’ll briefly describe the history of Perl CGI development. If you want to get straight to the useful stuff, you can skip ahead.

In the beginning, people wrote CGIs by scattering print statements throughout Perl scripts. The HTML that ended up on the user’s screen was literally embedded in the Perl script. These scripts were very difficult to maintain. Even a small change, like switching fonts, required a programmer to edit the script. It was almost impossible for a non-programmer to edit the HTML without endangering the script itself! Thus, programmers were required for even the smallest design change. Given the relative shortage of programmers versus HTML designers, this is clearly not desired situation.

Many solutions to this problem have appeared, and many share a common trait – they invert the situation by embedding programming inside HTML. Examples of this approach are HTML::Mason, Embperl, ASP and PHP. Used well these tools can allow programmers and non-programmers to work on the same script/template. However, the Perl programming and the HTML are still in the same file, and opportunities for mishap are constantly present. Maintainence is easier in this situation, but still problematic.

HTML::Template approaches the problem differently. Using HTML::Template, the Perl code and the HTML text are kept totally separate. The HTML is put in a file called a template, and the Perl code is written in separate script files and modules. The HTML is enhanced with a few new tags providing variable substitution, looping and branching. On the Perl side, the programmer can set values for the variables, fill in loops and control branching.

Used well, HTML::Template allows Perl programmers and HTML designers to work totally independently. The HTML designers only need to learn a few new tags. All the programming takes place in Perl scripts and modules using HTML::Template’s simple API.