File Coverage

blib/lib/CGI/Application/Plugin/HTCompiled.pm
Criterion Covered Total %
statement 47 58 81.0
branch 14 26 53.8
condition 1 3 33.3
subroutine 9 11 81.8
pod 1 1 100.0
total 72 99 72.7


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::HTCompiled;
2              
3 3     3   40721 use CGI::Application 4.31;
  3         5198  
  3         66  
4              
5 3     3   38 use 5.008;
  3         6  
6              
7 3     3   11 use strict;
  3         7  
  3         43  
8 3     3   8 use warnings;
  3         3  
  3         65  
9 3     3   9 use Carp;
  3         3  
  3         163  
10 3     3   1103 use UNIVERSAL::isa qw/isa/;
  3         2179  
  3         10  
11              
12             =head1 NAME
13              
14             CGI::Application::Plugin::HTCompiled - Integrate with HTML::Template::Compiled
15              
16             =cut
17              
18             $CGI::Application::Plugin::HTCompiled::VERSION = '1.06';
19              
20             =head1 SYNOPSIS
21              
22             # In your CGI::Application-derived base class. . .
23             use base "CGI::Application";
24             use CGI::Application::Plugin::HTCompiled;
25              
26             # Later, in a run mode far, far away. . .
27             sub view
28             {
29             my $self = shift;
30             my $username = $self->query->param("user");
31             my $user = My::Users->retrieve($username);
32              
33             my $tmpl_view = $self->load_tmpl( "view_user.tmpl" );
34              
35             $tmpl_view->param( user => $user );
36              
37             return $tmpl_view->output();
38             }
39              
40             =head1 DESCRIPTION
41              
42             Allows you to use L as a seamless replacement
43             for L.
44              
45              
46             =head1 DEFAULT PARAMETERS
47              
48             By default, the HTCompiled plugin will automatically add a parameter 'c' to the template that
49             will return to your CGI::Application object $self. This allows you to access any
50             methods in your CGI::Application module that you could normally call on $self
51             from within your template. This allows for some powerful actions in your templates.
52             For example, your templates will be able to access query parameters, or if you use
53             the CGI::Application::Plugin::Session module, you can access session parameters.
54              
55             Reload this page
56              
57             With this extra flexibility comes some responsibilty as well. It could lead down a
58             dangerous path if you start making alterations to your object from within the template.
59             For example you could call c.header_add to add new outgoing headers, but that is something
60             that should be left in your code, not in your template. Try to limit yourself to
61             pulling in information into your templates (like the session example above does).
62              
63             =head2 Extending load_tmpl()
64              
65             There are times when the basic C functionality just isn't
66             enough. The easiest way to do this is by replacing or
67             extending the functionality of L's C method.
68             This is still possible using the plugin.
69              
70             The following code snippet illustrates one possible way of achieving this:
71              
72             sub load_tmpl
73             {
74             my ($self, $tmpl_file, @extra_params) = @_;
75              
76             push @extra_params, "cache", "1";
77             return $self->SUPER::load_tmpl($tmpl_file, @extra_params);
78             }
79              
80              
81              
82             =head1 FUNCTIONS
83              
84             This is documentation of how it is done internally. If you actually are looking
85             for how to use this module, see SYNOPSIS. There isn't anything else to do than
86             using this plugin.
87              
88             =head2 import()
89              
90             Will be called when your Module uses L. Registers
91             callbacks at the C and the C stages. This is how the plugin
92             mechanism works.
93              
94             =cut
95              
96             sub import {
97 3     3   1808 my $caller = scalar( caller );
98            
99             # -- determine if the module has been used as base class or as plugin.
100 3 100       62 return unless $caller->isa('CGI::Application');
101            
102 1         25 $caller->add_callback( 'init' => \&_add_init );
103 1         14 $caller->add_callback( 'load_tmpl' => \&_pass_in_self );
104 1         1228 goto &Exporter::import;
105             } # /import
106              
107              
108              
109              
110             =head2 _pass_in_self()
111              
112             Adds the parameter c each template that will be processed. See
113             DEFAULT PARAMETERS for more information.
114              
115             =cut
116              
117             sub _pass_in_self {
118 0     0   0 my ( $self, $one, $tmpl_params, $template_file ) = @_;
119            
120             # we won't warn, assuming that the user set param "c" intensionally
121             #warn("Template param 'c' will be overwritten.") if exists $tmpl_params->{c};
122 0         0 $tmpl_params->{c} = $self;
123             } # /_pass_in_self
124              
125              
126              
127              
128             =head2 _add_init()
129              
130             Set html_tmpl_class to L at the init stage. That way,
131             each time a template is loaded using load_tmpl, an instance of
132             HTML::Template::Compiled will be created instead of the defualt HTML::Template.
133             See the L manpage for more information.
134              
135             =cut
136              
137             sub _add_init {
138 0     0   0 my $self = shift;
139 0         0 $self->html_tmpl_class('HTML::Template::Compiled');
140             } # /_add_init
141              
142              
143              
144              
145             =head2 load_tmpl()
146              
147             This method exists to ensure backward compatibility only. It overrides
148             CGI::Application's load_tmpl() when this plugin is used the old way.
149             See BACKWARD COMPATIBILITY for more information and please just don't use it
150             that way anymore.
151              
152             For the most part, this is the exact C method from
153             L, except it uses L instead of L.
154              
155             See the L reference for more detailed information
156             on what parameters can be passed to C.
157              
158             =cut
159              
160             sub load_tmpl {
161 2     2 1 3208 my $self = shift;
162 2 50       10 return $self->SUPER::load_tmpl(@_) unless $self->isa('CGI::Application::Plugin::HTCompiled');
163            
164 2         57 my ($tmpl_file, @extra_params) = @_;
165              
166             # add tmpl_path to path array if one is set, otherwise add a path arg
167 2 50       6 if (my $tmpl_path = $self->tmpl_path) {
168 2 50       23 my @tmpl_paths = (ref $tmpl_path eq 'ARRAY') ? @$tmpl_path : $tmpl_path;
169 2         2 my $found = 0;
170 2         11 for( my $x = 0; $x < @extra_params; $x += 2 ) {
171 1 50 33     7 if ($extra_params[$x] eq 'path' and
172             ref $extra_params[$x+1] eq 'ARRAY') {
173 0         0 unshift @{$extra_params[$x+1]}, @tmpl_paths;
  0         0  
174 0         0 $found = 1;
175 0         0 last;
176             }
177             }
178 2 50       10 push(@extra_params, path => [ @tmpl_paths ]) unless $found;
179             }
180              
181             # Since we have method call access in the templates, add the CGI::App object
182             # as "c" by default.
183 2         5 my %tmpl_params = (c=>$self);
184 2         6 my %ht_params = @extra_params;
185 2 50       6 %ht_params = () unless keys %ht_params;
186              
187             # Define our extension if doesn't already exist;
188 2 50       11 $self->{__CURRENT_TMPL_EXTENSION} = '.html' unless defined $self->{__CURRENT_TMPL_EXTENSION};
189              
190             # Define a default templat name based on the current run mode
191 2 50       6 unless (defined $tmpl_file) {
192 0         0 $tmpl_file = $self->get_current_runmode . $self->{__CURRENT_TMPL_EXTENSION};
193             }
194              
195 2         8 $self->call_hook('load_tmpl', \%ht_params, \%tmpl_params, $tmpl_file);
196              
197 3     3   3004 use HTML::Template::Compiled;
  3         129595  
  3         16  
198             # let's check $tmpl_file and see what kind of parameter it is - we
199             # now support 3 options: scalar (filename), ref to scalar (the
200             # actual html/template content) and reference to FILEHANDLE
201 2         28 my $t = undef;
202              
203 2 50       16 if ( ref $tmpl_file eq 'SCALAR' ) {
    50          
204 0         0 $t = HTML::Template::Compiled->new( scalar_ref => $tmpl_file, %ht_params );
205             } elsif ( ref $tmpl_file eq 'GLOB' ) {
206 0         0 $t = HTML::Template::Compiled->new( filehandle => $tmpl_file, %ht_params );
207             } else {
208 2         14 $t = HTML::Template::Compiled->new( filename => $tmpl_file, %ht_params);
209             }
210              
211 2 50       2813 (defined $t) || croak "problem creating template object. Check args to new()";
212              
213 2 50       7 if (keys %tmpl_params) {
214 2         9 $t->param(%tmpl_params);
215             }
216              
217 2         60 return $t;
218             }
219              
220              
221              
222              
223              
224             =head1 BACKWARD COMPATIBILITY
225              
226             You can still use the old method using the module by inheriting from it.
227             This is not recommended, as it overrides L's C.
228              
229             # In your CGI::Application-derived base class. . .
230             use base ("CGI::Application::Plugin::HTCompiled", "CGI::Application");
231              
232             # Later, in a run mode far, far away. . .
233             sub view
234             {
235             my $self = shift;
236             my $username = $self->query->param("user");
237             my $user = My::Users->retrieve($username);
238              
239             my $tmpl_view = $self->load_tmpl( "view_user.tmpl" );
240              
241             $tmpl_view->param( user => $user );
242              
243             return $tmpl_view->output();
244             }
245              
246              
247             =head1 EXAMPLE
248              
249             Define your CGI::Application derived base class.
250            
251             package CGIApplicationDerivedBaseClass;
252            
253             use strict;
254             use warnings;
255            
256             use FindBin qw/$Bin/;
257             use lib $Bin . '/lib';
258            
259             use base qw/CGI::Application/;
260            
261             use CGI::Application::Plugin::HTCompiled;
262            
263             =head1 NAME
264            
265             CGIApplicationDerivedBaseClass - Perl extension for demonstrating
266             CGI::Application::Plugin::HTCompiled.
267            
268             =head1 SYNOPSIS
269            
270             use strict;
271             use warnings;
272            
273             my $app = CGIApplicationDerivedBaseClass->new();
274             $app->run();
275            
276             =head1 DESCRIPTION
277            
278             This demonstrates, how to use CGI::Application::Plugin::HTCompiled.
279            
280            
281             =head1 METHODS
282            
283             =head2 setup()
284            
285             Defined runmodes, etc.
286            
287             =cut
288            
289             sub setup {
290             my $self = shift;
291            
292             $self->start_mode('start');
293             $self->run_modes([qw/
294             start
295             /]);
296            
297             } # /setup
298            
299            
300            
301            
302             =head2 start()
303            
304             =cut
305            
306             sub start {
307             my $self = shift;
308            
309             my $tmpl_content = qq~
310            

Hi!

311            
312            

You are here: (this is HTML::Compiled magic)

313            

You are using CAP::HTC version

314             ~;
315            
316             my $t = $self->load_tmpl(\$tmpl_content);
317             $t->param(version => $CGI::Application::Plugin::HTCompiled::VERSION);
318             return $t->output();
319             } # /start
320            
321            
322            
323            
324             =head1 SEE ALSO
325            
326             CGI::Application, CGI::Application::Plugin::HTCompiled.
327            
328             =head1 AUTHOR
329            
330             Alexander Becler, Ec a p f a n < a t > g m x . d eE
331            
332             =head1 COPYRIGHT AND LICENSE
333            
334             Copyright (C) 2009 by Alexander Becker
335            
336             This library is free software; you can redistribute it and/or modify it under
337             the same terms as Perl itself, either Perl version 5.8.8 or, at your option,
338             any later version of Perl 5 you may have available.
339            
340             =cut
341            
342             1;
343            
344              
345             Create an instance and run.
346              
347             #!/usr/bin/perl
348            
349             use strict;
350             use warnings;
351             use CGIApplicationDerivedBaseClass;
352            
353             my $app = CGIApplicationDerivedBaseClass->new();
354             $app->run();
355              
356              
357              
358             =head1 AUTHOR
359              
360             Alexander Becker C<< >>,
361             Mark Stosberg C<< >>
362             ...but largely modeled on HTDot plugin by Jason A. Crome.
363              
364             =head1 BUGS
365              
366             Please report any bugs or feature requests to
367             C, or through the web interface at
368             L.
369             I will be notified, and then you'll automatically be notified of progress on
370             your bug as I make changes.
371              
372             =head1 ACKNOWLEDGEMENTS
373              
374             The usual crowd in #cgiapp on irc.perl.org
375              
376             =head1 SEE ALSO
377              
378             L, L, L,
379              
380             =head1 COPYRIGHT & LICENSE
381              
382             Copyright 2005 Mark Stosberg, all rights reserved.
383              
384             This program is free software; you can redistribute it and/or modify it
385             under the same terms as Perl itself.
386              
387             =cut
388              
389             1; # End of CGI::Application::Plugin::HTCompiled
390