Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Using Template with CGI::Application

by rob_au (Abbot)
on Apr 19, 2002 at 06:35 UTC ( #160446=snippet: print w/ replies, xml ) Need Help??

Description: 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'

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

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;
Comment on Using Template with CGI::Application
Download Code

Back to Snippets Section

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://160446]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-07-11 08:26 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (221 votes), past polls