Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-10-21 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (96 votes), past polls