package CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit; =head1 NAME CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit - Template::Toolkit plugin to AnyTemplate =head1 DESCRIPTION This is a driver for L, which provides the implementation details specific to rendering templates via the L templating system. All C drivers are designed to be used the same way. For general usage instructions, see the documentation of L. =head1 EMBEDDED COMPONENT SYNTAX (Template::Toolkit) The L 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") %] =head1 TT OBJECT CACHING (singleton support) =head2 Introduction In a persistent environment, rather than creating a L object each time you fill a template, it is much more efficient to load a single L 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 L options (such as C, etc.). By default, when the C driver creates a L object, it caches it. From that point on, whenever the same application needs a L object, the driver uses the cached object rather than creating a new one. =head2 Multiple Applications in a Shared Persistent Environment An attempt is made to prevent different applications from sharing the same TT object. Internally, the TT 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: $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 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 module hierarchy looks like this: CGI::Application My::Project My::Webapp In this hierarchy, it makes sense to store the cached TT object in C. To make this happen, either call C<< $self->template->config >> from within C, or explicitly name the C when you call C<< $self->template->config >>. =head2 Disabling TT Object Caching You can disable L object caching entirely by providing a false value to the C driver config parameter: $self->template->config( type => 'TemplateToolkit', TemplateToolkit => { object_caching => 0, }, ); =head2 TT Object Caching and Include Paths The C driver config parameter is not cached; it is set every time you call C<< $self->template->load >>. So you can safely used cached TT objects even if the applications sharing the TT object need different C. =cut 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'); =head1 CONFIGURATION The L driver accepts the following config parameters: =over 4 =item embed_tag_name The name of the tag used for embedding components. Defaults to C. =item template_extension If C is true, then L will append the value of C to C. By default the C is C<.xhtml>. =item emulate_associate_query B If this config parameter is true, then L will copy all of the webapp's query params into the template. This is similar to what would happen if you used L's C feature with the webapp's query object: my $driver = HTML::Template->new( associate => $self->query, ); By default C is false. =item object_caching Whether or not to cache the L object in a persistent environment By default, C is enabled. See L<"TT OBJECT CACHING (singleton support)">, above. =item storage_class What class to use as the storage key when object caching is enabled. By default, C defaults to the package containing the subroutine that called C<< $self->template->config >>. See L<"TT OBJECT CACHING (singleton support)">, above. =back All other configuration parameters are passed on unchanged to L. =cut 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, ); } =head2 required_modules The C function returns the modules required for this driver to operate. In this case: C