File Coverage

blib/lib/CGI/Application/Plugin/AnyTemplate/Driver/TemplateToolkit.pm
Criterion Covered Total %
statement 56 56 100.0
branch 16 18 88.8
condition 11 15 73.3
subroutine 10 10 100.0
pod 5 5 100.0
total 98 104 94.2


line stmt bran cond sub pod time code
1              
2             package CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit;
3              
4             =head1 NAME
5              
6             CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit - Template::Toolkit plugin to AnyTemplate
7              
8             =head1 DESCRIPTION
9              
10             This is a driver for L, which
11             provides the implementation details specific to rendering templates via
12             the L templating system.
13              
14             All C drivers are designed to be used the same way. For
15             general usage instructions, see the documentation of
16             L.
17              
18             =head1 EMBEDDED COMPONENT SYNTAX (Template::Toolkit)
19              
20             The L syntax for embedding components is:
21              
22             [% CGIAPP.embed("some_run_mode", param1, param2, 'literal string3') %]
23              
24             This can be overridden by the following configuration variables:
25              
26             embed_tag_name # default 'CGIAPP'
27              
28             For instance by setting the following values in your configuration file:
29              
30             embed_tag_name 'MYAPP'
31              
32             Then the embedded component tag will look like:
33              
34             [% MYAPP.embed("some_run_mode") %]
35              
36              
37             =head1 TT OBJECT CACHING (singleton support)
38              
39             =head2 Introduction
40              
41             In a persistent environment, rather than creating a L
42             object each time you fill a template, it is much more efficient to load
43             a single L object and use this object to render all
44             of your templates.
45              
46             However, in a persistent environment, you may have several different
47             applications running, and they all might need to set different
48             L options (such as C, etc.).
49              
50             By default, when the C driver creates a
51             L object, it caches it. From that point on, whenever
52             the same application needs a L object, the driver
53             uses the cached object rather than creating a new one.
54              
55             =head2 Multiple Applications in a Shared Persistent Environment
56              
57             An attempt is made to prevent different applications from
58             sharing the same TT object.
59              
60             Internally, the TT objects are stored in a private hash keyed by the web
61             application's class name.
62              
63             You can explicitly specify the class name when you call C:
64              
65             $self->template->config(
66             type => 'TemplateToolkit',
67             TemplateToolkit => {
68             storage_class => 'My::Project',
69             },
70             );
71              
72             If you don't specify the class name, then the package containing the subroutine
73             that called C is used. For instance:
74              
75             package My::Project;
76             sub setup {
77             my $self = shift;
78             $self->template->config( # My::Project is used to store
79             type => 'TemplateToolkit', # cached TT object
80             );
81             }
82              
83             A typical C module hierarchy looks like this:
84              
85             CGI::Application
86             My::Project
87             My::Webapp
88              
89             In this hierarchy, it makes sense to store the cached TT object in
90             C. To make this happen, either call C<< $self->template->config >>
91             from within C, or explicitly name the C when you call
92             C<< $self->template->config >>.
93              
94             =head2 Disabling TT Object Caching
95              
96             You can disable L object caching entirely by
97             providing a false value to the C driver config
98             parameter:
99              
100             $self->template->config(
101             type => 'TemplateToolkit',
102             TemplateToolkit => {
103             object_caching => 0,
104             },
105             );
106              
107             =head2 TT Object Caching and Include Paths
108              
109             The C driver config parameter is not cached; it is set
110             every time you call C<< $self->template->load >>. So you can safely used
111             cached TT objects even if the applications sharing the TT object need
112             different C.
113              
114             =cut
115              
116 22     22   71307 use strict;
  22         133  
  22         827  
117 22     22   119 use Carp;
  22         46  
  22         1580  
118              
119 22     22   1950 use CGI::Application::Plugin::AnyTemplate::ComponentHandler;
  22         44  
  22         497  
120              
121 22     22   2091 use CGI::Application::Plugin::AnyTemplate::Base;
  22         42  
  22         578  
122 22     22   111 use vars qw(@ISA);
  22         42  
  22         14052  
123             @ISA = ('CGI::Application::Plugin::AnyTemplate::Base');
124              
125             =head1 CONFIGURATION
126              
127             The L driver
128             accepts the following config parameters:
129              
130             =over 4
131              
132             =item embed_tag_name
133              
134             The name of the tag used for embedding components. Defaults to
135             C.
136              
137             =item template_extension
138              
139             If C is true, then
140             L will append the value of
141             C to C. By default
142             the C is C<.xhtml>.
143              
144             =item emulate_associate_query
145              
146             B
147              
148             If this config parameter is true, then L
149             will copy all of the webapp's query params into the template.
150              
151             This is similar to what would happen if you used L's
152             C feature with the webapp's query object:
153              
154             my $driver = HTML::Template->new(
155             associate => $self->query,
156             );
157              
158             By default C is false.
159              
160             =item object_caching
161              
162             Whether or not to cache the L object in a persistent environment
163              
164             By default, C is enabled.
165              
166             See L<"TT OBJECT CACHING (singleton support)">, above.
167              
168             =item storage_class
169              
170             What class to use as the storage key when object caching is enabled.
171              
172             By default, C defaults to the package containing the
173             subroutine that called C<< $self->template->config >>.
174              
175             See L<"TT OBJECT CACHING (singleton support)">, above.
176              
177              
178             =back
179              
180             All other configuration parameters are passed on unchanged to L.
181              
182             =head1 CONFIGURING UTF-8 TEMPLATES
183              
184             C does NOT support L's C option at runtime:
185              
186             # not possible with AnyTemplate
187             $tt->process($infile, $vars, $outfile, { binmode => 1 })
188             || die $tt->error(), "\n";
189            
190             # not possible with AnyTemplate
191             $tt->process($infile, $vars, $outfile, binmode => 1)
192             || die $tt->error(), "\n";
193            
194             # not possible with AnyTemplate
195             $tt->process($infile, $vars, $outfile, binmode => ':utf8')
196             || die $tt->error(), "\n";
197              
198             Instead, use the C option in the initial config:
199            
200             $self->template->config(
201             default_type => 'TemplateToolkit',
202             TemplateToolkit => {
203             ENCODING => 'UTF-8'
204             }
205             );
206              
207             If you have a mix of encodings in your templates, use a separate
208             C configuration for each encoding:
209              
210             $self->template('ascii')->config(
211             default_type => 'TemplateToolkit',
212             );
213             $self->template('utf-8')->config(
214             default_type => 'TemplateToolkit',
215             TemplateToolkit => {
216             ENCODING => 'UTF-8'
217             }
218             );
219              
220             =cut
221              
222             sub driver_config_keys {
223 81     81 1 396 qw/
224             storage_class
225             object_caching
226             cache_storage_keys
227             embed_tag_name
228             template_extension
229             emulate_associate_query
230             /;
231             }
232              
233             sub default_driver_config {
234             (
235 81     81 1 616 object_caching => 1,
236             template_extension => '.tmpl',
237             embed_tag_name => 'CGIAPP',
238             emulate_associate_query => 0,
239             );
240             }
241              
242             =head2 required_modules
243              
244             The C function returns the modules required for this driver
245             to operate. In this case: C