File Coverage

blib/lib/CGI/Application/Plugin/AnyTemplate/Base.pm
Criterion Covered Total %
statement 75 79 94.9
branch 17 24 70.8
condition 14 23 60.8
subroutine 16 16 100.0
pod 11 11 100.0
total 133 153 86.9


line stmt bran cond sub pod time code
1              
2             package CGI::Application::Plugin::AnyTemplate::Base;
3              
4             =head1 NAME
5              
6             CGI::Application::Plugin::AnyTemplate::Base - Base class for templates
7              
8             =head1 DESCRIPTION
9              
10             This documentation is mainly for developers who want to write additional
11             Template drivers. For how to use the system, see the docs for
12             L
13              
14             =cut
15              
16 25     25   1304 use strict;
  25         51  
  25         881  
17 25     25   118 use Carp;
  25         53  
  25         1549  
18 25     25   125 use Scalar::Util qw(weaken);
  25         43  
  25         24632  
19              
20             sub _new {
21 111     111   189 my $proto = shift;
22 111   33     509 my $class = ref $proto || $proto;
23              
24 111         933 my %args = @_;
25              
26 111         217 my $self = {};
27              
28 111   50     5945 $self->{'driver_config'} = delete $args{'driver_config'} || {};
29 111   100     624 $self->{'native_config'} = delete $args{'native_config'} || {};
30 111   50     389 $self->{'include_paths'} = delete $args{'include_paths'} || [];
31 111         267 $self->{'filename'} = delete $args{'filename'};
32 111         331 $self->{'string_ref'} = delete $args{'string_ref'};
33 111         249 $self->{'callers_package'} = delete $args{'callers_package'};
34 111         249 $self->{'return_references'} = delete $args{'return_references'};
35 111         408 $self->{'conf_name'} = delete $args{'conf_name'};
36 111         269 $self->{'webapp'} = delete $args{'webapp'};
37              
38 111   100     631 $self->{'component_handler_class'} = delete $args{'component_handler_class'}
39             || 'CGI::Application::Plugin::AnyTemplate::ComponentHandler';
40              
41 111         494 bless $self, $class;
42              
43 111         679 weaken $self->{'webapp'};
44              
45 111         444 $self->initialize;
46              
47 109         30501 return $self;
48             }
49              
50             =head1 METHODS
51              
52             =over 4
53              
54             =item param
55              
56             The C method gets and sets values within the template.
57              
58             my $template = $self->template->load;
59              
60             my @param_names = $template->param();
61              
62             my $value = $template->param('name');
63              
64             $template->param('name' => 'value');
65             $template->param(
66             'name1' => 'value1',
67             'name2' => 'value2'
68             );
69              
70             It is designed to behave similarly to the C method in other modules like
71             C and C.
72              
73             =cut
74              
75             sub param {
76 255     255 1 1727 my $self = shift;
77              
78 255 100       746 if (@_) {
79 173         277 my $param;
80 173 100       603 if (ref $_[0] eq 'HASH') {
    100          
81 14         25 $param = shift;
82             }
83             elsif (@_ == 1) {
84 24         71 return $self->{'param'}{$_[0]};
85             }
86             else {
87 135         738 $param = { @_ };
88             }
89 149   100     698 $self->{'param'} ||= {};
90 149         1572 $self->{'param'}{$_} = $param->{$_} for keys %$param;
91             }
92             else {
93 82   100     269 $self->{'param'} ||= {};
94 82         129 return keys %{ $self->{'param'} };
  82         269  
95             }
96             }
97              
98             =item get_param_hash
99              
100             Returns the template variables as a hash of names and values.
101              
102             my %params = $template->get_param_hash;
103              
104             In a scalar context, returns a reference to the hash used
105             internally to contain the values:
106              
107             my $params_ref = $template->get_param_hash;
108              
109             =cut
110              
111             sub get_param_hash {
112 120     120 1 335 my $self = shift;
113 120   50     379 $self->{'param'} ||= {};
114 120 100       299 return %{ $self->{'param'} } if wantarray;
  20         155  
115 100         299 return $self->{'param'};
116             }
117              
118             =item clear_params
119              
120             Clears the values stored in the template:
121              
122             $template->param(
123             'name1' => 'value1',
124             'name1' => 'value2'
125             );
126             $template->clear_params;
127             $template->param(
128             'name_foo' => 'value_bar',
129             );
130              
131             # params are now:
132             'name_foo' => 'value_bar',
133              
134              
135             =cut
136              
137             sub clear_params {
138 27     27 1 139 my $self = shift;
139 27         91 $self->{'param'} = {};
140             }
141              
142             =item output
143              
144             Returns the template with all the values filled in.
145              
146             return $template->output();
147              
148             You can also supply names and values to the template at this stage:
149              
150             return $template->output('name' => 'value', 'name2' => 'value2');
151              
152             Before the template output is generated, the C<< template_pre_process >>
153             hook is called. Any callbacks that you register to this hook will be
154             called before each template is processed. Register a
155             C callback as follows:
156              
157             $self->add_callback('template_pre_process', \&my_tmpl_pre_process);
158              
159             Pre-process callbacks will be passed a reference to the C<$template>
160             object, and can can modify the parameters passed into the template by
161             using the C method:
162              
163             sub my_tmpl_pre_process {
164             my ($self, $template) = @_;
165              
166             # Change the internal template parameters by reference
167             my $params = $template->get_param_hash;
168              
169             foreach my $key (keys %$params) {
170             $params{$key} = to_piglatin($params{$key});
171             }
172              
173             # Can also set values using the param method
174             $template->param('foo', 'bar');
175              
176             }
177              
178              
179             After the template output is generated, the C hook is called.
180             You can register a C callback as follows:
181              
182             $self->add_callback('template_post_process', \&my_tmpl_post_process);
183              
184             Any callbacks that you register to this hook will be called after each
185             template is processed, and will be passed both a reference to the
186             template object and a reference to the output generated by the template.
187             This allows you to modify the output of the template:
188              
189             sub my_tmpl_post_process {
190             my ($self, $template, $output_ref) = @_;
191              
192             $$output_ref =~ s/foo/bar/;
193             }
194              
195              
196              
197              
198             When you call the C method, any components embedded in the
199             template are run. See C, below.
200              
201             =cut
202              
203             # calling forms:
204             # $template->output;
205             # $template->output('file.html', \%params);
206             # $template->output(\%params)
207             #
208             # $template->fill;
209             # $template->fill('file.html', \%params);
210             # $template->fill(\%params)
211              
212             sub output {
213 96     96 1 17660 my $self = shift;
214              
215 96         319 $self->param(@_);
216              
217 96         202 my $webapp = $self->{'webapp'};
218              
219 96 50 33     828 if ($webapp and $webapp->can('call_hook')) {
220 96         417 $webapp->call_hook('template_pre_process', $self);
221             }
222              
223 96         2604 my $output = $self->render_template;
224              
225 92 50 33     1000 if ($webapp and $webapp->can('call_hook')) {
226 92         154 my $output_param = $output;
227 92 50       277 $output_param = \$output_param unless ref $output_param;
228 92         405 $webapp->call_hook('template_post_process', $self, $output_param);
229             }
230 92 100       2252 if ($self->{'return_references'}) {
231 82 50       1092 return ref $output ? $output : \$output;
232             }
233             else {
234 10 50       55 return ref $output ? $$output : $output;
235             }
236             }
237              
238             =item filename
239              
240             If the template was loaded from a file, the C method returns the template filename.
241              
242             =cut
243              
244             sub filename {
245 93     93 1 163 my $self = shift;
246 93         306 return $self->{'filename'};
247             }
248              
249             =item string_ref
250              
251             If the template was loaded from a string, the C method returns a reference to the string.
252              
253             =cut
254              
255             sub string_ref {
256 93     93 1 167 my $self = shift;
257 93         352 return $self->{'string_ref'};
258             }
259              
260             =item object
261              
262             Returns a reference to the underlying template driver, e.g. the
263             C object or the C object.
264              
265             =back
266              
267             =cut
268              
269             sub object {
270 46     46 1 343 my $self = shift;
271 46         150 return $self->{'driver'};
272             }
273              
274             =head1 DOCS FOR TEMPLATE MODULE DEVELOPERS
275              
276             The following documentation is of interest primarly for developers who
277             wish to add support for a new type of Template system.
278              
279             =head2 METHODS FOR DEVELOPERS
280              
281             =over 4
282              
283             =item initialize
284              
285             This method is called by the controller at C to create the
286             driver-specific subclass of C
287              
288             This is a virtual method and must be defined in the subclass.
289              
290             The following paramters are passed to the driver and available as keys of the
291             driver's C<$self> object:
292              
293             'driver_config' => ... # hashref of driver-specific config
294             'native_config' => ... # hashref of native template system specific config
295             'include_paths' => ... # listref of template include paths
296             'filename' => ... # template filename
297             'webapp' => ... # reference to the current CGI::Application $self
298              
299             =cut
300              
301             sub initialize {
302 1     1 1 232 croak "Driver did not initialize its driver";
303             }
304              
305              
306             =item driver_config_keys
307              
308             When it creates the driver object,
309             C has to separate the
310             C from the C.
311              
312             C should return a list of parameters that are
313             specific to the driver_config and not the native template system
314             config.
315              
316              
317             For instance, the user can specify
318              
319             $self->template->config(
320             HTMLTemplate => {
321             embed_tag_name => 'embed',
322             global_vars => 1,
323             die_on_bad_params => 0,
324             cache => 1
325             },
326             );
327              
328             The parameters C, C, and C are all
329             specific to L. These are considered I parameters.
330              
331             But C configures the
332             C subclass. This
333             is considered a I parameter.
334              
335             Therefore C<'embed_tag_name'> should be included in the list of
336             params returned by C.
337              
338             Example C:
339              
340             sub driver_config_keys {
341             'template_extension',
342             'embed_tag_name'
343             }
344              
345             =cut
346              
347             sub driver_config_keys {
348 2     2 1 9 return;
349             }
350              
351             =item default_driver_config
352              
353             Should return a hash of default values for C.
354              
355             For instance:
356              
357             sub default_driver_config {
358             {
359             template_extension => '.foo',
360             embed_tag_name => 'embed',
361             };
362             }
363              
364              
365             =cut
366              
367             sub default_driver_config {
368 2     2 1 8 return;
369             }
370              
371             =item render_template
372              
373             This method must be overriden in a subclass. It has the responsibility
374             of filling the template in C<< $self->filename >> with the values in C<< $self->param >>
375             via the appropriate template system, and returning the output as either
376             a string or a reference to a string.
377              
378             It also must manage embedding nested components.
379              
380             =back
381              
382             =cut
383              
384             sub render_template {
385 1     1 1 169 croak "render_template virtual method";
386             }
387              
388             # Utility method for drivers to load their prerequsite modules
389              
390             sub _require_prerequisite_modules {
391 109     109   189 my ($class) = @_;
392              
393 109         140 my @missing_modules;
394              
395 109         410 foreach my $module ($class->required_modules) {
396 109         12623 eval "require $module";
397 109 50       733 if ($@) {
398 0         0 push @missing_modules, $module;
399             }
400             # $module->import;
401             }
402 109 50       503 if (@missing_modules) {
403 0           foreach my $module (@missing_modules) {
404 0           warn "$class: missing prerequisite module: $module\n";
405             }
406 0           die "Can't continue loading: $class\n";
407             }
408             }
409              
410              
411             =head1 AUTHOR
412              
413             Michael Graham, C<< >>
414              
415             =head1 BUGS
416              
417             Please report any bugs or feature requests to
418             C, or through the web interface at
419             L. I will be notified, and then you'll automatically
420             be notified of progress on your bug as I make changes.
421              
422             =head1 SOURCE
423              
424             The source code repository for this module can be found at http://github.com/mgraham/CAP-AnyTemplate/
425              
426             =head1 SEE ALSO
427              
428             CGI::Application::Plugin::AnyTemplate
429             CGI::Application::Plugin::AnyTemplate::ComponentHandler
430             CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate
431             CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExpr
432             CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggable
433             CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit
434             CGI::Application::Plugin::AnyTemplate::Driver::Petal
435              
436             CGI::Application
437              
438             Template::Toolkit
439             HTML::Template
440              
441             HTML::Template::Pluggable
442             HTML::Template::Plugin::Dot
443              
444             Petal
445              
446             CGI::Application::Plugin::TT
447              
448             =head1 COPYRIGHT & LICENSE
449              
450             Copyright 2005 Michael Graham, All Rights Reserved.
451              
452             This program is free software; you can redistribute it and/or modify it
453             under the same terms as Perl itself.
454              
455             =cut
456              
457             1; # End of CGI::Application::Plugin::AnyTemplate
458