DESCRIPTION

This is a driver for CGI::Application::Plugin::AnyTemplate, which provides the implementation details specific to rendering templates via the Template::Toolkit templating system.

All \*(C`AnyTemplate\*(C' drivers are designed to be used the same way. For general usage instructions, see the documentation of CGI::Application::Plugin::AnyTemplate.

EMBEDDED COMPONENT SYNTAX (Template::Toolkit)

The Template::Toolkit syntax for embedding components is:

    [% CGIAPP.embed("some_run_mode", param1, param2, 'literal string3') %]

This can be overridden by the following configuration variables:

embed_tag_name # default 'CGIAPP'

For instance by setting the following values in your configuration file:

embed_tag_name 'MYAPP'

Then the embedded component tag will look like:

[% MYAPP.embed("some_run_mode") %]

TT OBJECT CACHING (singleton support)

Introduction

In a persistent environment, rather than creating a Template::Toolkit object each time you fill a template, it is much more efficient to load a single Template::Toolkit object and use this object to render all of your templates.

However, in a persistent environment, you may have several different applications running, and they all might need to set different Template::Toolkit options (such as \*(C`POST_CHOMP\*(C', etc.).

By default, when the \*(C`TemplateToolkit\*(C' driver creates a Template::Toolkit object, it caches it. From that point on, whenever the same application needs a Template::Toolkit object, the driver uses the cached object rather than creating a new one.

Multiple Applications in a Shared Persistent Environment

An attempt is made to prevent different applications from sharing the same \s-1TT\s0 object.

Internally, the \s-1TT\s0 objects are stored in a private hash keyed by the web application's class name.

You can explicitly specify the class name when you call \*(C`config\*(C':

$self->template->config( type => 'TemplateToolkit', TemplateToolkit => { storage_class => 'My::Project', }, );

If you don't specify the class name, then the package containing the subroutine that called \*(C`config\*(C' is used. For instance:

package My::Project; sub setup { my $self = shift; $self->template->config( # My::Project is used to store type => 'TemplateToolkit', # cached TT object ); }

A typical \*(C`CGI::Application\*(C' module hierarchy looks like this:

CGI::Application My::Project My::Webapp

In this hierarchy, it makes sense to store the cached \s-1TT\s0 object in \*(C`My::Project\*(C'. To make this happen, either call \*(C`$self->template->config\*(C' from within \*(C`My::Project\*(C', or explicitly name the \*(C`storage_class\*(C' when you call \*(C`$self->template->config\*(C'.

Disabling \s-1TT\s0 Object Caching

You can disable Template::Toolkit object caching entirely by providing a false value to the \*(C`object_caching\*(C' driver config parameter:

$self->template->config( type => 'TemplateToolkit', TemplateToolkit => { object_caching => 0, }, );

\s-1TT\s0 Object Caching and Include Paths

The \*(C`include_paths\*(C' driver config parameter is not cached; it is set every time you call \*(C`$self->template->load\*(C'. So you can safely used cached \s-1TT\s0 objects even if the applications sharing the \s-1TT\s0 object need different \*(C`include_paths\*(C'.

CONFIGURATION

The CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit driver accepts the following config parameters:

embed_tag_name

The name of the tag used for embedding components. Defaults to \*(C`CGIAPP\*(C'.

template_extension

If \*(C`auto_add_template_extension\*(C' is true, then CGI::Application::Plugin::AnyTemplate will append the value of \*(C`template_extension\*(C' to \*(C`filename\*(C'. By default the \*(C`template_extension\*(C' is \*(C`.xhtml\*(C'.

emulate_associate_query

This feature is now deprecated and will be removed in a future release. If this config parameter is true, then CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit will copy all of the webapp's query params into the template. This is similar to what would happen if you used HTML::Template's \*(C`associate\*(C' feature with the webapp's query object: my $driver = HTML::Template->new( associate => $self->query, ); By default \*(C`emulate_associate_query\*(C' is false.

object_caching

Whether or not to cache the Template::Toolkit object in a persistent environment By default, \*(C`object_caching\*(C' is enabled. See \*(L"\s-1TT\s0 \s-1OBJECT\s0 \s-1CACHING\s0 (singleton support)\*(R", above.

storage_class

What class to use as the storage key when object caching is enabled. By default, \*(C`storage_class\*(C' defaults to the package containing the subroutine that called \*(C`$self->template->config\*(C'. See \*(L"\s-1TT\s0 \s-1OBJECT\s0 \s-1CACHING\s0 (singleton support)\*(R", above.

All other configuration parameters are passed on unchanged to Template::Toolkit.

CONFIGURING UTF-8 TEMPLATES

\*(C`AnyTemplate\*(C' does \s-1NOT\s0 support Template::Toolkit's \*(C`binmode\*(C' option at runtime:

# not possible with AnyTemplate $tt->process($infile, $vars, $outfile, { binmode => 1 }) || die $tt->error(), "\n";

# not possible with AnyTemplate $tt->process($infile, $vars, $outfile, binmode => 1) || die $tt->error(), "\n";

# not possible with AnyTemplate $tt->process($infile, $vars, $outfile, binmode => ':utf8') || die $tt->error(), "\n";

Instead, use the \*(C`ENCODING\*(C' option in the initial config:

$self->template->config( default_type => 'TemplateToolkit', TemplateToolkit => { ENCODING => 'UTF-8' } );

If you have a mix of encodings in your templates, use a separate \*(C`AnyTemplate\*(C' configuration for each encoding:

$self->template('ascii')->config( default_type => 'TemplateToolkit', ); $self->template('utf-8')->config( default_type => 'TemplateToolkit', TemplateToolkit => { ENCODING => 'UTF-8' } );

required_modules

The \*(C`required_modules\*(C' function returns the modules required for this driver to operate. In this case: \*(C`Template\*(C'.

DRIVER METHODS

initialize

Initializes the \*(C`TemplateToolkit\*(C' driver. See the docs for CGI::Application::Plugin::AnyTemplate::Base for details.

render_template

Fills the Template::Toolkit object with \*(C`$self->param\*(C' If the param \*(C`emulate_associate_query\*(C' is true, then set params for each of $self->{'webapp'}->query, mimicking HTML::Template's associate mechanism. Also set up a CGI::Application::Plugin::AnyTemplate::ComponentHandler object so that the \*(C`CGIAPP.embed\*(C' callback will work. Returns the output of the filled template as a string reference. See the docs for CGI::Application::Plugin::AnyTemplate::Base for details.

RELATED TO CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit…

CGI::Application::Plugin::AnyTemplate CGI::Application::Plugin::AnyTemplate::Base CGI::Application::Plugin::AnyTemplate::ComponentHandler CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExpr CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggable CGI::Application::Plugin::AnyTemplate::Driver::Petal

CGI::Application

Template::Toolkit HTML::Template

HTML::Template::Pluggable HTML::Template::Plugin::Dot

Petal

Exporter::Renaming

CGI::Application::Plugin::TT

ACKNOWLEDGEMENTS

Thanks to Cees Hek for discussing the issues of caching in a persistent environment. And also for his excellent CGI::Application::Plugin::TT module, from which I stole ideas and some code: especially the bit about how to change the include path in a \s-1TT\s0 object after you've initialized it.

AUTHOR

Michael Graham, \*(C`<[email protected]>\*(C'

COPYRIGHT & LICENSE

Copyright 2005 Michael Graham, All Rights Reserved.

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