Chapter 7. Anatomy of the Template Toolkit

Now that we’ve spent a great deal of time looking at what you can do with the Template Toolkit, let’s take a look inside and get a feel for how it actually works. We’ll follow the flow of processing a template from the frontend (such as Template or ttree), to getting the file from disk (Template::Provider), to compiling it (Template::Parser, Template::Grammar, and Template::Directive), and to executing it (Template::Context and Template::Document).

We’ll be using pseudocode versions of the methods to illustrate the major thrust of each component, mainly to gloss over tedious details of error checking, parameter handling, file opening and closing, and syntax. Feel free to get a copy of each .pm file and follow along with the real code; however, the best way to understand any complex system is to look at the innards, and the Template Toolkit is no exception.

Template Modules

The Template module is simply a frontend that creates and uses a Template::Service object and then pipes the output wherever you want it to go (standard output by default, or maybe a file, scalar variable, etc.). The Apache::Template module is another frontend, which uses a Template::Service::Apache object under the hood and sends the output back to the relevant Apache object. The now-familiar tpage and ttree scripts are command line-based frontends; tpage simply connects standard input and output by way of the Template Toolkit, while ttree does the same for source and ...

Get Perl Template Toolkit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.