Templating Systems for Perl

From Dev411: The Code Wiki

In web applications, templating systems are popular for separating HTML presentation code from application code. In Perl, they eliminate the need for embedded HTML displayed via print statements in the Perl code itself.

There are several popular templating systems for Perl, each with a different level of capability. The popular ones include HTML::Template (H::T), Template Toolkit (TT), and Mason in order of increasing sophistication. HTML::Template uses a very restrictive lexicon to limit logic in the template, TT uses a mini-language, while Mason provides the most logic capabilities.

This page is intended to compare the solutions in the context of a web application framework such as Catalyst and CGI::Application.

Table of contents

Solutions

HTML::Template

H::T enforces a strong separation of logic and layout by not allowing many kinds of logic to take place, either in the templating language or via hooks to Perl. This strong separation forces a lot of logic to be embedded in the application, including display logic which can be complex. H::T is a single file and an easy install.

Matthew Robertson maintains a version an enhanced version of HTML::Template (http://members.optusnet.com.au/~mathew/html_template.html) that is not discussed here.

Template Toolkit

TT is a much larger templating system than H::T, requiring many more modules to be installed, and it has enough functionality that it is often used as a web application framework on its own. TT has a much richer templating language than H::T and allows direct access to Perl as well. When taken to the extreme, Perl application logic can be placed in TT, however this is discouraged. When used with a web application framework TT allows display logic to be placed in the templating system and the application logic to be placed in the controller. This tends to result cleaner code for the typical application.

Comparisons

Template Toolkit vs. HTML::Template

The primary difference between TT and H::T is that TT allows greater functionality in the template which means that certain transformations don't have to take place and display logic can be separated from the rest of the logic, resulting in cleaner application code.

  • Installation size: H::T is a much smaller installation (one file) than TT.
  • Complex Perl datatypes: TT can accept arbitrary complex Perl datatypes removing the need to reformat data into the templating language. H::T cannot process these datatypes so the data must be formatted into the template language in the application. When used with a MVC framework, TT also allows data extracted from the model to be sent directly to the view without reformatting. Eliminating the need to reformat data tends to keep the application code cleaner.
  • Templating language: TT's richer templating language includes functionality such as complex IF statement conditionals and the join function (useful for an arrayref of breadcrumbs). This functionality allows display logic to be placed in the template as opposed to the application as needed with H::T.
  • Framework object access: When used through a web framework plugin (Catalyst::View::TT, CGI::Application::Plugin::TT, etc.), TT can pull information directly out of the framework object (Catalyst Context object, CGI::Application object, etc.), removing the need for setting template parameters directly. With Catalyst, this is known to work with the FormValidator, FormValidator::Simple and Prototype plugins. With CGI::Application, this is known to work with the HTMLPrototype plugin.