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; }

In reply to Using Template with CGI::Application by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.