Chapter 8. Extending the Template Toolkit

Most of the customization you are likely to perform will fall under one of two categories: creating new frontends and writing filters and plugins. However, some things cannot be handled with a new frontend or by writing a plugin, such as modifying how the provider finds templates to process or limiting access to certain plugins. Luckily, the Template Toolkit makes it easy to replace or extend any of the core components; its modular design makes replacing individual components simple. Chapter 7 gives public API details for each component.

Using and Implementing Noncore Components

Each Template Toolkit module knows about the other modules it needs to do its job, and will create instances of these objects unless one is passed to it explicitly. This means that modules are loaded and instances created on demand. The Template::Config module provides a convenient and centralized place to override core elements of the Template Toolkit, in the form of factory methods for each major component—context, filters, iterator, parser, plugins, provider, service, stash, and constants. The type of object that each method creates is, in turn, controlled by a series of variables in the $Template::Config namespace:

$CONTEXT = 'Template::Context'; $FILTERS = 'Template::Filters'; $ITERATOR = 'Template::Iterator'; $PARSER = 'Template::Parser'; $PLUGINS = 'Template::Plugins'; $PROVIDER = 'Template::Provider'; $SERVICE = 'Template::Service'; $STASH = 'Template::Stash'; ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.