O'Reilly logo

Perl Template Toolkit by David Cross, Andy Wardley, Darren Chamberlain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required