CGI::Application::Plugin::TemplateRunner - CGI::App plugin to display HTML::Templates


  package MyApp;
  use base 'CGI::Application'
  use CGI::Application::Plugin::TemplateRunner
        qw( show_tmpl);
  sub setup{
          my $self = shift;
                       'show_tmpl' => 'show_tmpl',
                       'some_action' => 'some_action',
  sub some_action{
          my $self = shift;
          # do some stuff with the database
          return $self->show_tmpl;


This module is a plugin for CGI::Application that provides a runmode to automatically get the name of an HTML::Template from the path_info string, load the template, fill it with data from an associated Perl data file and display the template.


There are three methods that you can use in CGI::App subclass. None of them are exported by default, you have to explicitly import them.


This is a runmode. It extracts a page name from path_info. That name must end in .html and a file of the same name must be present in the applications tmpl_path (if you have multiple tmpl_path, in the first one). For example if you have


it will look for


That template will be loaded and displayed. See the detailed description below about where the data for the template is coming from.


This method is used internally to load the template and fill in the data. You can also use it inside of your own runmodes if you want.

        my $tmpl = $self->prepare_tmpl(
                $filename, %extras);

You can use %extras to specify additional data to be used as template parameters (not found or overriding the data from the data file).


Another internal method that takes an HTML::Template instance and some data to put into it. It basically wraps around $tmpl->param to provide the additional functionality needed by this plugin (descending into hashes, calling coderefs).

        $self->fill_tmpl($tmpl, {
        '/somehash' => { one => 1, two=>2 }};

Where does the template get its data?

CGI parameters and cookies

CGI request parameters and cookies are automatically made available to the template. If you have


you can get it as

        <tmpl_var /request/page>

and your cookie "ID" will become

        <tmpl_var /cookie/id>

Application parameters

Parameters set for the CGI::Application instance (using $app->param() ) are also automatically available to the template

        $app->param(foo => bar);
        <tmpl_var /app/foo>

The data file

When this module loads a template, it also tries to load an associated data file, which has the same name as the template plus ".pl" at the end. So for /bbs/index.html it will look for /bbs/ (you have to put the data file next to the HTML file into your tmpl_path)

That data file is just a Perl file and gets eval'ed. It must return a hash ref with the data.

Here is an example:

                page_title => 'BBS page',
                # becomes <tmpl_var /page_title>
                categories => [
                { name => 'Sports',  link => 'sports.html'},
                { name => 'TV', link => 'tv.html'},
                # becomes <tmpl_loop categories>
                nested => { 
                        a=> 1, b => 2
                # become <tmpl_var nested/a>
                # and <tmpl_var nested/b>
                articles => sub{
                        my $app = shift;
                        # subroutines get the CGI::App
                        # instance as their only parameter
                        my $q = $app->query;
                        my $page = $q->param('page')||1;
                        my $total = MyDB::get_article_count;
                        my $page = MyDB::get_article_list($page);
                        return {
                                total => $total,
                                page => $page};
                # becomes
                # <tmpl_var articles/total>
                # <tmpl_loop articles/page>

extra parameters to prepare_tmpl

If you use prepare_tmpl in your runmodes, you can stuff in extra data:

       my $tmpl = $self->prepare_tmpl(
                $filename, 'more' => 'data')
        <tmpl_var /more>

Using this class as a CGI::App subclass

For very simple applications, especially ones that only display some data but do not allow to edit it, the single runmode provided by this module is probably all you need. In this case, you do not have to make your own CGI::App subclass at all, but can use this module directly from your instance scripts:

        use CGI::Application::Plugin::TemplateRunner;
        my $app = new CGI::Application::Plugin::TemplateRunner();



Thilo Planz, <>


Copyright 2004 by Thilo Planz

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.