There have been a number of questions posted previously by people asking about the feasibility of using an alternative templating mechanism with CGI::Application. This framework by default implements methods for directly calling and using HTML::Template templating objects.

The following code demonstrates how easy it can be to incorporate an alternate templating mechanism into the CGI::Application framework, in this case, Template Toolkit and includes some comments on usage.
 

#!/usr/bin/perl -Tw use Self; my $app = Self->new({ PARAMS => { 'tmpl_path' => '../tt2/templates' } }); $app->run; exit 0; package Self; use Template; use base qw/ CGI::Application /; use strict; sub setup { my $self = shift; . . . # Create a Template object and store it within a # CGI::Application parameter so that it is visible # across all run-modes - Note that this construction # also includes a reference to a CGI::Application # parameter called 'tmpl_path' which may be defined # to set the template include path for Template # objects. This parameter is set within my scripts # at the invocation of the Self object via the PARAMS # argument. # # Other Template parameters can easily be # incorporated here. # $self->param( 'template' => Template->new({ 'INCLUDE_PATH' => $self->param( 'tmpl_path' ) || '../templat +es' })); } sub _run_mode_subroutine { my $self = shift; # The $html variable is that to which the output of # Template will be bound and subsequently returned to # CGI::Application in place of the output method of # HTML::Template. The $html scalar is empty but # defined so that if no output is passed into $html # via the $template->process method, an error is not # generated within the server log files from # CGI::Application from concatenation with an # undefined variable ($html, returned at the end of # this method to the CGI::Application output # subroutines). # my $html = ''; my %params = (); # Pass the Template object stored within the # CGI::Application parameter 'template' into a local # variable - This is not necessary but included for # clarity of code process. The alternate arrangement # would be to subsequently call the Template process # method as $self->param( 'template' )->process # ('filename.tt2', \%params, \$html) # my $template = $self->param( 'template' ); . . . # Process the template incorporating the template # parameters from the %params hash, returning the # output in the $html variable. # $template->process('filename.tt2', \%params, \$html); return $html; }