File Coverage

blib/lib/CGI/Application/Plugin/AnyTemplate/Driver/HTMLTemplate.pm
Criterion Covered Total %
statement 61 61 100.0
branch 13 16 81.2
condition 3 6 50.0
subroutine 11 11 100.0
pod 6 6 100.0
total 94 100 94.0


line stmt bran cond sub pod time code
1              
2             package CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate;
3              
4             =head1 NAME
5              
6             CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate - HTML::Template driver 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 (HTML::Template)
19              
20             =head2 Syntax
21              
22             The L syntax for embedding components is:
23              
24            
25              
26             I<(Support for parameter passing is limited. See the note on paramters below.)>
27              
28             This can be overridden by the following configuration variables:
29              
30             embed_tag_name # default 'cgiapp_embed'
31              
32             For instance by setting the following value in your configuration file:
33              
34             embed_tag_name '***component***'
35              
36             Then the embedded component tag will look like:
37              
38            
39              
40             =head2 Parameters
41              
42             Since L doesn't support parameter passing in the
43             template, the C driver emulates this behaviour.
44              
45             The parameter list passed to the embed subroutine is parsed before
46             the template is parsed. Literal strings (strings enclosed in single or
47             double quotes) are passed verbatim to the target run mode. Params not
48             enclosed in quotes are looked up in C<< $self->param >>; the resulting
49             literal or looked up values are passed to the target run mode. Finally,
50             the return value of the run mode (its output) is passed as a parameter
51             value to the template.
52              
53             Note that the param lookup scheme is somewhat simplistic. For instance,
54             it does not respect the scope of loops or conditional constructs within
55             the template.
56              
57             For proper parameter handling using L-style templates,
58             use either the
59             L
60             or the L
61             driver instead.
62              
63              
64             =cut
65              
66              
67 21     21   24409 use strict;
  21         49  
  21         951  
68 21     21   125 use Carp;
  21         42  
  21         2006  
69              
70 21     21   13884 use CGI::Application::Plugin::AnyTemplate::ComponentHandler;
  21         54  
  21         632  
71              
72 21     21   15064 use CGI::Application::Plugin::AnyTemplate::Base;
  21         48  
  21         671  
73 21     21   122 use vars qw(@ISA);
  21         37  
  21         18644  
74             @ISA = ('CGI::Application::Plugin::AnyTemplate::Base');
75              
76             =head1 CONFIGURATION
77              
78             The L driver
79             accepts the following config parameters:
80              
81             =over 4
82              
83             =item embed_tag_name
84              
85             The name of the tag used for embedding components. Defaults to
86             C.
87              
88             =item template_extension
89              
90             If C is true, then
91             L will append the value of
92             C to C. By default
93             the C is C<.html>.
94              
95             =item associate_query
96              
97             B
98              
99             If this config parameter is true, then
100             L will
101             copy all of the webapp's query params into the template using
102             L's C mechanism:
103              
104             my $driver = HTML::Template->new(
105             associate => $self->query,
106             );
107              
108             By default C is false.
109              
110             If you provide an C config parameter of your own, that will
111             disable the C functionality.
112              
113             =back
114              
115             All other configuration parameters are passed on unchanged to L.
116              
117             =cut
118              
119             sub driver_config_keys {
120 74     74 1 269 qw/
121             embed_tag_name
122             template_extension
123             associate_query
124             /;
125             }
126              
127             sub default_driver_config {
128             (
129 74     74 1 486 template_extension => '.html',
130             embed_tag_name => 'CGIAPP_embed',
131             associate_query => 0,
132             );
133             }
134              
135             =head2 required_modules
136              
137             The C function returns the modules required for this driver
138             to operate. In this case: L.
139              
140             =cut
141              
142             sub required_modules {
143 64     64 1 474 return qw(
144             HTML::Template
145             );
146             }
147              
148              
149             =head1 DRIVER METHODS
150              
151             =over 4
152              
153             =item initialize
154              
155             Initializes the C driver. See the docs for
156             C for details.
157              
158             =cut
159              
160             # create the HTML::Template object,
161             # using:
162             # $self->{'driver_config'} # config info
163             # $self->{'include_paths'} # the paths to search for the template file
164             # $self->filename # the template file
165             # $self->string_ref # ...or the template string
166             # $self->{'webapp'}->query # for HTML::Template's 'associate' method,
167             # # so that the query params are included
168             # # in the template output
169             sub initialize {
170 43     43 1 72 my $self = shift;
171              
172 43         243 $self->_require_prerequisite_modules;
173              
174 43         266 my $string_ref = $self->string_ref;
175 43         239 my $filename = $self->filename;
176              
177 43 50 66     251 $string_ref or $filename or croak "HTML::Template: file or string must be specified";
178              
179 43 50       173 my $query = $self->{'webapp'}->query or croak "HTML::Template webapp query not found";
180              
181 43         196 my %params = (
182 43         450 %{ $self->{'native_config'} },
183             path => $self->{'include_paths'},
184             );
185              
186 43 100       132 if ($filename) {
187 39         86 $params{'filename'} = $filename;
188             }
189 43 100       104 if ($string_ref) {
190 4         9 $params{'scalarref'} = $string_ref;
191             }
192              
193 43 100       142 if ($self->{'driver_config'}{'associate_query'}) {
194 1   33     4 $params{'associate'} ||= $query; # allow user to override associate with their own
195             }
196              
197 43         291 $self->{'driver'} = HTML::Template->new(%params);
198              
199              
200             }
201              
202             # If we have already called output, then any stored params have already
203             # been stored in the driver. So when the user calls clear_params on the
204             # AT object, we have to call clear_params on the driver as well.
205              
206             sub clear_params {
207 13     13 1 66 my $self = shift;
208              
209 13 50       50 if ($self->{'driver'}) {
210 13         56 $self->{'driver'}->clear_params;
211             }
212 13         416 $self->SUPER::clear_params;
213             }
214              
215             =item render_template
216              
217             Fills the C object with C<< $self->param >>
218             replacing any magic C<*embed*> tags with the content generated by the
219             appropriate runmodes.
220              
221             Returns the output of the filled template as a string reference.
222              
223             See the docs for C for details.
224              
225             =back
226              
227             =cut
228              
229             sub render_template {
230 45     45 1 78 my $self = shift;
231              
232 45         98 my $driver_config = $self->{'driver_config'};
233 45         228 my $tmpl_vars = $self->get_param_hash;
234              
235             # pull in any included templates by calling them as run modes
236              
237 45         199 my $tag_match = '^'
238             . quotemeta(
239             $driver_config->{'embed_tag_name'}
240             )
241             . '\s*'
242             . '\((.*?)\)?' # optional params
243             . '\s*'
244             . '$';
245              
246 45         600 $tag_match = qr/$tag_match/i;
247              
248 45         363 my $component_handler = $self->{'component_handler_class'}->new(
249             'webapp' => $self->{'webapp'},
250             'containing_template' => $self,
251             );
252              
253             # fill the template
254 45         90 my $template = $self->{'driver'};
255              
256             # fill the CGIAPP_embed(foo,...) tags
257 45         187 foreach my $tag ($template->query) {
258             # print STDERR "tag: $tag ($tag_match)\n";
259 231 100       1472 if ($tag =~ $tag_match) {
260             # print STDERR "tag: $tag ($tag_match) MATCHED\n";
261 11         29 my $params = $1;
262              
263 11         72 my @params = split /\s*,\s*/, $params;
264 11         18 my @prepped_params;
265              
266 11         17 foreach my $param (@params) {
267             # print STDERR "param: $param\n";
268 35 100       162 if ($param =~ /^('|")?(.*?)\1$/) {
269 23         45 $param = $2; # remove quotes
270             # print STDERR "param-de-quoted: $param\n";
271 23         115 push @prepped_params, $param;
272             }
273             else {
274 12         20 $param = $tmpl_vars->{$param};
275             # print STDERR "param-looked-up: $param\n";
276 12         23 push @prepped_params, $param;
277             }
278             }
279 11         20 my $run_mode = shift @prepped_params;
280             # print STDERR "rm: $run_mode (@prepped_params)\n";
281              
282 11         17 $self->param($tag => ${ $component_handler->embed_direct($run_mode, @prepped_params) });
  11         51  
283             }
284             }
285              
286 43         191 $template->param($tmpl_vars);
287 43         3768 my $output = $template->output;
288 43         4319 return \$output;
289             }
290              
291             =head1 SEE ALSO
292              
293             CGI::Application::Plugin::AnyTemplate
294             CGI::Application::Plugin::AnyTemplate::Base
295             CGI::Application::Plugin::AnyTemplate::ComponentHandler
296             CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExpr
297             CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggable
298             CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit
299             CGI::Application::Plugin::AnyTemplate::Driver::Petal
300              
301             CGI::Application
302              
303             Template::Toolkit
304             HTML::Template
305              
306             HTML::Template::Pluggable
307             HTML::Template::Plugin::Dot
308              
309             Petal
310              
311             Exporter::Renaming
312              
313             CGI::Application::Plugin::TT
314              
315              
316             =head1 AUTHOR
317              
318             Michael Graham, C<< >>
319              
320             =head1 COPYRIGHT & LICENSE
321              
322             Copyright 2005 Michael Graham, All Rights Reserved.
323              
324             This program is free software; you can redistribute it and/or modify it
325             under the same terms as Perl itself.
326              
327             =cut
328              
329             1;
330