| CGI-Application-Plugin-AnyTemplate documentation | Contained in the CGI-Application-Plugin-AnyTemplate distribution. |
CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit - Template::Toolkit plugin to AnyTemplate
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 AnyTemplate drivers are designed to be used the same way. For
general usage instructions, see the documentation of
CGI::Application::Plugin::AnyTemplate.
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") %]
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 POST_CHOMP, etc.).
By default, when the TemplateToolkit 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.
You can disable Template::Toolkit object caching entirely by
providing a false value to the object_caching driver config
parameter:
$self->template->config(
type => 'TemplateToolkit',
TemplateToolkit => {
object_caching => 0,
},
);
The include_paths driver config parameter is not cached; it is set
every time you call $self->template->load. So you can safely used
cached TT objects even if the applications sharing the TT object need
different include_paths.
The CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit driver accepts the following config parameters:
The name of the tag used for embedding components. Defaults to
CGIAPP.
If auto_add_template_extension is true, then
CGI::Application::Plugin::AnyTemplate will append the value of
template_extension to filename. By default
the template_extension is .xhtml.
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
associate feature with the webapp's query object:
my $driver = HTML::Template->new(
associate => $self->query,
);
By default emulate_associate_query is false.
Whether or not to cache the Template::Toolkit object in a persistent environment
By default, object_caching is enabled.
See "TT OBJECT CACHING (singleton support)", above.
What class to use as the storage key when object caching is enabled.
By default, storage_class defaults to the package containing the
subroutine that called $self->template->config.
See "TT OBJECT CACHING (singleton support)", above.
All other configuration parameters are passed on unchanged to Template::Toolkit.
AnyTemplate does NOT support Template::Toolkit's binmode 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 ENCODING 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
AnyTemplate configuration for each encoding:
$self->template('ascii')->config(
default_type => 'TemplateToolkit',
);
$self->template('utf-8')->config(
default_type => 'TemplateToolkit',
TemplateToolkit => {
ENCODING => 'UTF-8'
}
);
The required_modules function returns the modules required for this driver
to operate. In this case: Template.
Initializes the TemplateToolkit driver. See the docs for
CGI::Application::Plugin::AnyTemplate::Base for details.
Fills the Template::Toolkit object with $self->param
If the param emulate_associate_query 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 CGIAPP.embed 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.
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
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 TT object after you've initialized it.
Michael Graham, <mgraham@cpan.org>
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.
| CGI-Application-Plugin-AnyTemplate documentation | Contained in the CGI-Application-Plugin-AnyTemplate distribution. |
package CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit;
use strict; use Carp; use CGI::Application::Plugin::AnyTemplate::ComponentHandler; use CGI::Application::Plugin::AnyTemplate::Base; use vars qw(@ISA); @ISA = ('CGI::Application::Plugin::AnyTemplate::Base');
sub driver_config_keys { qw/ storage_class object_caching cache_storage_keys embed_tag_name template_extension emulate_associate_query /; } sub default_driver_config { ( object_caching => 1, template_extension => '.tmpl', embed_tag_name => 'CGIAPP', emulate_associate_query => 0, ); }
sub required_modules { return qw( Template ); }
# create the Template::Toolkit object, # using: # $self->{'driver_config'} # config info # $self->{'include_paths'} # the paths to search for the template file # $self->filename # the template file my %TT_Object_Store; sub initialize { my $self = shift; $self->_require_prerequisite_modules; my %config = %{ $self->{'native_config'} }; $config{'INCLUDE_PATH'} = $self->{'include_paths'}; my $driver; my $storage_class = $self->{'driver_config'}{'storage_class'}; $storage_class ||= $self->{'callers_package'}; my $config_name = $self->{'conf_name'}; if ($self->{'driver_config'}{'object_caching'} and exists $TT_Object_Store{$storage_class}) { if (defined $config_name) { $driver = $TT_Object_Store{$storage_class}{'named'}{$config_name}; } else { $driver = $TT_Object_Store{$storage_class}{'default'}; } } if (!$driver) { $driver = Template->new(\%config); } if ($self->{'driver_config'}{'object_caching'}) { if (defined $config_name) { $TT_Object_Store{$storage_class}{'named'}{$config_name} = $driver; } else { $TT_Object_Store{$storage_class}{'default'} = $driver; } } # Stolen from Cees's CAP::TT $driver->context->load_templates->[0]->include_path($self->{'include_paths'}); $self->{'driver'} = $driver; }
sub render_template { my $self = shift; my $driver_config = $self->{'driver_config'}; my $template = $self->{'driver'}; my $output = ''; # emulate HTML::Template's 'associate' behaviour if ($driver_config->{'emulate_associate_query'}) { my $params = $self->get_param_hash; if ($self->{'webapp'}) { foreach ($self->{'webapp'}->query->param) { $params->{$_} ||= $self->{'webapp'}->query->param($_); } } } my $component_handler = $self->{'component_handler_class'}->new( 'webapp' => $self->{'webapp'}, 'containing_template' => $self, ); my $params = $self->get_param_hash; $params->{$driver_config->{'embed_tag_name'}} = $component_handler; my $filename = $self->filename; my $string_ref = $self->string_ref; $string_ref or $filename or croak "TemplateToolkit: file or string must be specified"; $template->process(($string_ref || $filename), $params, \$output) || croak $template->error; return \$output; }
1;