blib/lib/CGI/Application/Plugin/TT.pm

filter: Statementstmt 91.3% 190 / 208 Branchbran 75.0% 72 / 96 Conditioncond 61.7% 29 / 47 MC/DCmcdc 22.2% 6 / 27 Subroutinesub 86.2% 25 / 29 Podpod 100% 9 / 9 totaltotal 79.5% 331 / 416 CC 70 SCAR 26.6
CC
70
Cov
83%
tt_config
36.0
tt_process
14.5
_get_object_or_options
8.2
CRAP 14.3
↑ Summary Build »
1 package CGI::Application::Plugin::TT;
2  
3 10 ▶use Template 2.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use CGI::Application 4.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use vars qw($VERSION @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 $VERSION = '1.05';
13  
14 10 require Exporter;
15  
16 10 @EXPORT = qw(
17 tt_obj
18 tt_config
19 tt_params
20 tt_clear_params
21 tt_process
22 tt_include_path
23 tt_template_name
24 );
25 sub import {
26 9 ▶ my $pkg = shift;
Subroutine import: called CC=4 SCAR=13.9
27 9 my $callpkg = caller;
28 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 foreach my $sym (@EXPORT) {
30 63 *{"${callpkg}::$sym"} = \&{$sym};
31 }
32 9 ▶ $callpkg->tt_config(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 8 9
33 9 ▶ if ($callpkg->isa('CGI::Application')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callpkg->isa('CGI::Application')) { } 9 0 9
34 9 $callpkg->new_hook('tt_pre_process');
35 9 $callpkg->new_hook('tt_post_process');
36 } else {
37 0 warn "Calling package is not a CGI::Application module so not installing tt_pre_process and tt_post_process hooks. If you are using \@ISA instead of 'use base', make sure it is in a BEGIN { } block, and make sure these statements appear before the plugin is loaded";
38 }
39  
40 }
41  
42 ##############################################
43 ###
44 ### tt_obj
45 ###
46 ##############################################
47 #
48 # Get a Template Toolkit object. The same object
49 # will be returned everytime this method is called
50 # during a request cycle.
51 #
52 sub tt_obj {
53 29 ▶ my $self = shift;
Subroutine tt_obj: called CC=6 SCAR=20.2
Pod documented
54  
55 29 my ($tt, $options, $frompkg) = _get_object_or_options($self);
56  
57 29 ▶ if (!$tt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tt) 18 11 29
58 18 my $tt_options = $options->{TEMPLATE_OPTIONS};
59 18 ▶ if (keys %{$options->{TEMPLATE_OPTIONS}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$options{'TEMPLATE_OPTIONS'};}) { } 18 0 18
60 18 ▶ $tt = Template->new( $options->{TEMPLATE_OPTIONS} ) || carp "Can't load Template";
Condition33%1/3
'Template'->new($options->{'TEMPLATE_OPTIONS'}) || carp(q[Can't load Template])
l!l&&r!l&&!r
1800
MC/DC0%0/2
'Template'->new($options->{'TEMPLATE_OPTIONS'}) || carp(q[Can't load Template])
'Template'->new($options->{'TEMPLATE_OPTIONS'}) carp(q[Can't load Template])
Truth table33%1/3
A || B
A= 'Template'->new($options->{'TEMPLATE_OPTIONS'})B= carp(q[Can't load Template])
ABresult
1X1
011
000
61 } else {
62 0 ▶ $tt = Template->new || carp "Can't load Template";
Condition0%0/3
'Template'->new || carp(q[Can't load Template])
l!l&&r!l&&!r
000
MC/DC0%0/2
'Template'->new || carp(q[Can't load Template])
'Template'->new carp(q[Can't load Template])
Truth table0%0/3
A || B
A= 'Template'->newB= carp(q[Can't load Template])
ABresult
1X1
011
000
63 }
64 18 ▶ _set_object($frompkg||$self, $tt);
Condition66%2/3
$frompkg || $self
l!l&&r!l&&!r
2160
MC/DC0%0/2
$frompkg || $self
$frompkg $self
Truth table66%2/3
A || B
A= $frompkgB= $self
ABresult
1X1
011
000
65 }
66 29 return $tt;
67 }
68  
69 ##############################################
70 ###
71 ### tt_config
72 ###
73 ##############################################
74 #
75 # Configure the Template Toolkit object
76 #
77 sub tt_config {
78 18 ▶ my $self = shift;
Subroutine tt_config: called CC=27 SCAR=35.8
Pod documented
79 18 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 16 2 18
80  
81 18 my $tt_config;
82 18 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 16 2 18
83 16 ▶ die "Calling tt_config after the tt object has already been created" if @_ && defined $self->{__TT};
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $self->{"__TT"} 0 16 16
Condition33%1/3
@_ and defined $self->{"__TT"}
!ll&&!rl&&r
0160
MC/DC0%0/2
@_ and defined $self->{"__TT"}
@_ defined $self->{"__TT"}
Truth table33%1/3
A and B
A= @_B= defined $self->{"__TT"}
ABresult
0X0
100
111
84 16 ▶ $tt_config = $self->{__TT_CONFIG} ||= {};
Condition50%1/2
$self->{"__TT_CONFIG"} ||= {}
l!l
016
MC/DC0%0/1
$self->{"__TT_CONFIG"} ||= {}
$self->{"__TT_CONFIG"}
Truth table50%1/2
A
A= $self->{"__TT_CONFIG"}
Aresult
11
00
85 } else {
86 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶ ${$class.'::__TT_CONFIG'} ||= {};
Condition50%1/2
${$class . '::__TT_CONFIG';} ||= {}
l!l
02
MC/DC0%0/1
${$class . '::__TT_CONFIG';} ||= {}
${$class . '::__TT_CONFIG';}
Truth table50%1/2
A
A= ${$class . '::__TT_CONFIG';}
Aresult
11
00
88 2 $tt_config = ${$class.'::__TT_CONFIG'};
89 }
90  
91 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 18 0 18
92 18 my $props;
93 18 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 18 18
94 0 my $rthash = %{$_[0]};
95 0 $props = CGI::Application->_cap_hash($_[0]);
96 } else {
97 18 $props = CGI::Application->_cap_hash({ @_ });
98 }
99  
100 18 my %options;
101 # Check for TEMPLATE_OPTIONS
102 18 ▶ if ($props->{TEMPLATE_OPTIONS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"TEMPLATE_OPTIONS"}) 18 0 18
103 carp "tt_config error: parameter TEMPLATE_OPTIONS is not a hash reference"
104 18 ▶ if Scalar::Util::reftype($props->{TEMPLATE_OPTIONS}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($props->{"TEMPLATE_OPTIONS"}) ne "HASH" 0 18 18
105 18 $tt_config->{TEMPLATE_OPTIONS} = delete $props->{TEMPLATE_OPTIONS};
106 }
107  
108 # Check for TEMPLATE_NAME_GENERATOR
109 18 ▶ if ($props->{TEMPLATE_NAME_GENERATOR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"TEMPLATE_NAME_GENERATOR"}) 1 17 18
110 carp "tt_config error: parameter TEMPLATE_NAME_GENERATOR is not a subroutine reference"
111 1 ▶ if Scalar::Util::reftype($props->{TEMPLATE_NAME_GENERATOR}) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($props->{"TEMPLATE_NAME_GENERATOR"}) ne "CODE" 0 1 1
112 1 $tt_config->{TEMPLATE_NAME_GENERATOR} = delete $props->{TEMPLATE_NAME_GENERATOR};
113 }
114  
115 # Check for TEMPLATE_PRECOMPILE_FILETEST
116 18 ▶ if ($props->{TEMPLATE_PRECOMPILE_FILETEST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) 6 12 18
117 carp "tt_config error: parameter TEMPLATE_PRECOMPILE_FILETEST is not a subroutine reference or regexp or string"
118 if defined Scalar::Util::reftype($props->{TEMPLATE_PRECOMPILE_FILETEST})
119 && Scalar::Util::reftype($props->{TEMPLATE_PRECOMPILE_FILETEST}) ne 'CODE'
120 6 ▶ && overload::StrVal($props->{TEMPLATE_PRECOMPILE_FILETEST}) !~ /^Regexp=/;
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) and builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) ne "CODE" and not overload::StrVal($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) =~ /^Regexp=/ 0 6 6
Condition83%5/6
defined builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) and builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) ne "CODE" and not overload::StrVal($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) =~ /^Regexp=/
!ll&&!rl&&r
420
defined builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) and builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) ne "CODE"
!ll&&!rl&&r
222
MC/DC0%0/3
defined builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) and builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) ne "CODE" and not overload::StrVal($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) =~ /^Regexp=/
defined builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) ne "CODE" not overload::StrVal($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) =~ /^Regexp=/
Truth table75%3/4
A and B and not(C)
A= defined builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"})B= builtin::reftype($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) ne "CODE"C= not overload::StrVal($props->{"TEMPLATE_PRECOMPILE_FILETEST"}) =~ /^Regexp=/
ABCresult
0XX0
10X0
1100
1111
121 6 $tt_config->{TEMPLATE_PRECOMPILE_FILETEST} = delete $props->{TEMPLATE_PRECOMPILE_FILETEST};
122 }
123  
124 # This property must be tested last, since it creates the TT object in order to
125 # preload all the templates.
126 #
127 # Check for TEMPLATE_PRECOMPILE_DIR
128 18 ▶ if( $props->{TEMPLATE_PRECOMPILE_DIR} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"TEMPLATE_PRECOMPILE_DIR"}) 6 12 18
129 6 my $type = Scalar::Util::reftype($props->{TEMPLATE_PRECOMPILE_DIR});
130 6 ▶ carp "tt_config error: parameter TEMPLATE_PRECOMPILE_DIR must be a SCALAR or an ARRAY ref"
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $type or $type eq "ARRAY" 0 6 6
Condition33%1/3
not defined $type or $type eq "ARRAY"
l!l&&r!l&&!r
600
MC/DC0%0/2
not defined $type or $type eq "ARRAY"
not defined $type $type eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not defined $typeB= $type eq "ARRAY"
ABresult
1X1
011
000
131 unless( !defined($type) or $type eq 'ARRAY' );
132  
133 # now look at each file and
134 0 my @dirs = ($type && $type eq 'ARRAY') ? @{$props->{TEMPLATE_PRECOMPILE_DIR}}
135 6 ▶ : ($props->{TEMPLATE_PRECOMPILE_DIR});
Branch: 1 branch
BranchTrueFalseTotal
$type && $type eq 'ARRAY' ? : 0 6 6
Condition33%1/3
$type and $type eq "ARRAY"
!ll&&!rl&&r
600
MC/DC0%0/2
$type and $type eq "ARRAY"
$type $type eq "ARRAY"
Truth table33%1/3
A and B
A= $typeB= $type eq "ARRAY"
ABresult
0X0
100
111
136 6 delete $props->{TEMPLATE_PRECOMPILE_DIR};
137 6 my $tt = $self->tt_obj;
138 6 my $junk = '';
139 6 ▶ my $filetester = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
140 6 ▶ if ($tt_config->{TEMPLATE_PRECOMPILE_FILETEST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt_config->{"TEMPLATE_PRECOMPILE_FILETEST"}) 6 0 6
141 6 ▶ if (! defined Scalar::Util::reftype($tt_config->{TEMPLATE_PRECOMPILE_FILETEST})) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined builtin::reftype($tt_config->{'TEMPLATE_PRECOMPILE_FILETEST'})) { } 2 4 6
elsif (builtin::reftype($tt_config->{'TEMPLATE_PRECOMPILE_FILETEST'}) eq 'CODE') { } 2 2 4
elsif (overload::StrVal($tt_config->{'TEMPLATE_PRECOMPILE_FILETEST'}) =~ /^Regexp=/) { } 2 0 2
142 2 ▶ $filetester = sub { $_[0] =~ /\.$tt_config->{TEMPLATE_PRECOMPILE_FILETEST}$/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
143 } elsif (Scalar::Util::reftype($tt_config->{TEMPLATE_PRECOMPILE_FILETEST}) eq 'CODE') {
144 2 $filetester = $tt_config->{TEMPLATE_PRECOMPILE_FILETEST};
145 } elsif (overload::StrVal($tt_config->{TEMPLATE_PRECOMPILE_FILETEST}) =~ /^Regexp=/) {
146 2 ▶ $filetester = sub { $_[0] =~ $tt_config->{TEMPLATE_PRECOMPILE_FILETEST} };
Subroutine __ANON__: called CC=1 SCAR=0.0
147 }
148 }
149 6 require File::Find;
150 File::Find::find(
151 sub {
152 12 ▶ my $file = $File::Find::name;
Subroutine __ANON__: called CC=3 SCAR=11.0
153 12 ▶ return unless $filetester->($file);
Branch: 1 branch
BranchTrueFalseTotal
unless &$filetester($file) 9 3 12
154 3 ▶ if( !-d $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $file) 3 0 3
155 3 $tt->process( $file, {}, \$junk );
156 }
157 },
158 6 map { File::Spec->rel2abs($_) } @dirs,
159 );
160
161 }
162
163 # If there are still entries left in $props then they are invalid
164 18 ▶ carp "Invalid option(s) (".join(', ', keys %$props).") passed to tt_config" if %$props;
Branch: 1 branch
BranchTrueFalseTotal
if %$props 0 18 18
165 }
166  
167 18 $tt_config;
168 }
169  
170 ##############################################
171 ###
172 ### tt_params
173 ###
174 ##############################################
175 #
176 # Set some parameters that will be added to
177 # any template object we process in this
178 # request cycle.
179 #
180 sub tt_params {
181 29 ▶ my $self = shift;
Subroutine tt_params: called CC=5 SCAR=16.1
Pod documented
182 29 my @data = @_;
183  
184 # Define the params stash if it doesn't exist
185 29 ▶ $self->{__TT_PARAMS} ||= {};
Condition100%2/2
$self->{"__TT_PARAMS"} ||= {}
l!l
1613
MC/DC100%1/1
$self->{"__TT_PARAMS"} ||= {}
$self->{"__TT_PARAMS"}
Truth table100%2/2
A
A= $self->{"__TT_PARAMS"}
Aresult
11
00
186  
187 29 ▶ if (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 16 13 29
188 16 my $params = $self->{__TT_PARAMS};
189 16 my $newparams = {};
190 16 ▶ if (ref $data[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data[0] eq 'HASH') { } 8 8 16
elsif (@data % 2 == 0) { } 8 0 8
191 # hashref
192 8 %$newparams = %{ $data[0] };
193 } elsif ( (@data % 2) == 0 ) {
194 8 %$newparams = @data;
195 } else {
196 0 carp "tt_params requires a hash or hashref!";
197 }
198  
199 # merge the new values into our stash of parameters
200 16 @$params{keys %$newparams} = values %$newparams;
201 }
202  
203 29 return $self->{__TT_PARAMS};
204 }
205  
206 ##############################################
207 ###
208 ### tt_clear_params
209 ###
210 ##############################################
211 #
212 # Clear any template parameters that may have
213 # been set during this request cycle.
214 #
215 sub tt_clear_params {
216 0 ▶ my $self = shift;
Subroutine tt_clear_params: not called CC=1 SCAR=6.9
Pod documented
217  
218 0 my $params = $self->{__TT_PARAMS};
219 0 $self->{__TT_PARAMS} = {};
220  
221 0 return $params;
222 }
223  
224 ##############################################
225 ###
226 ### tt_pre_process
227 ###
228 ##############################################
229 #
230 # Sample method that is called just before
231 # a Template is processed.
232 # Useful for setting global template params.
233 # It is passed the template filename and the hashref
234 # of template data
235 #
236 sub tt_pre_process {
237 0 ▶ my $self = shift;
Subroutine tt_pre_process: not called CC=1 SCAR=6.9
Pod documented
238 0 my $file = shift;
239 0 my $vars = shift;
240  
241 # Do your pre-processing here
242 }
243  
244 ##############################################
245 ###
246 ### tt_post_process
247 ###
248 ##############################################
249 #
250 # Sample method that is called just after
251 # a Template is processed.
252 # Useful for post processing the HTML.
253 # It is passed a scalar reference to the HTML code.
254 #
255 # Note: This could also be accomplished using the
256 # cgiapp_postrun method, except that this
257 # method is called after every template is
258 # processed (you could process multiple
259 # templates in one request), whereas
260 # cgiapp_postrun is only called once after
261 # the runmode has completed.
262 #
263 sub tt_post_process {
264 0 ▶ my $self = shift;
Subroutine tt_post_process: not called CC=1 SCAR=6.9
Pod documented
265 0 my $htmlref = shift;
266  
267 # Do your post-processing here
268 }
269  
270 ##############################################
271 ###
272 ### tt_process
273 ###
274 ##############################################
275 #
276 # Process a Template Toolkit template and return
277 # the resulting html as a scalar ref
278 #
279 sub tt_process {
280 13 ▶ my $self = shift;
Subroutine tt_process: called CC=14 SCAR=26.7
Pod documented
281 13 my $file = shift;
282 13 my $vars = shift;
283 13 my $html = '';
284  
285 13 ▶ my $can_call_hook = UNIVERSAL::can($self, 'call_hook') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($self, 'call_hook') ? : 13 0 13
286  
287 13 ▶ if (! defined($vars) && (Scalar::Util::reftype($file)||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $vars and (builtin::reftype($file) || "") eq "HASH") 4 9 13
Condition100%5/5
not defined $vars and (builtin::reftype($file) || "") eq "HASH"
!ll&&!rl&&r
724
builtin::reftype($file) || ''
l!l
42
MC/DC100%2/2
not defined $vars and (builtin::reftype($file) || "") eq "HASH"
not defined $vars builtin::reftype($file)
Truth table100%3/3
not(A) and B
A= not defined $varsB= builtin::reftype($file)
ABresult
0X0
100
111
288 4 $vars = $file;
289 4 $file = undef;
290 }
291 13 ▶ $file ||= $self->tt_template_name(1);
Condition66%2/3
$file ||= $self->tt_template_name(1)
l!l&&r!l&&!r
850
MC/DC0%0/2
$file ||= $self->tt_template_name(1)
$file $self->tt_template_name(1)
Truth table66%2/3
A ||= B
A= $fileB= $self->tt_template_name(1)
ABresult
1X1
011
000
292 13 ▶ $vars ||= {};
Condition100%2/2
$vars ||= {}
l!l
112
MC/DC100%1/1
$vars ||= {}
$vars
Truth table100%2/2
A
A= $vars
Aresult
11
00
293 13 my $template_name = $file;
294  
295 # Call the load_tmpl hook that is part of CGI::Application
296 13 ▶ $self->call_hook(
Branch: 1 branch
BranchTrueFalseTotal
if $can_call_hook 13 0 13
297 'load_tmpl',
298 {}, # template options are ignored
299 $vars,
300 $file,
301 ) if $can_call_hook;
302  
303 # Call tt_pre_process hook
304 13 ▶ $self->tt_pre_process($file, $vars) if $self->can('tt_pre_process');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("tt_pre_process") 7 6 13
305 13 ▶ $self->call_hook('tt_pre_process', $file, $vars) if $can_call_hook;
Branch: 1 branch
BranchTrueFalseTotal
if $can_call_hook 13 0 13
306  
307 # Include any parameters that may have been
308 # set with tt_params
309 13 my %params = ( %{ $self->tt_params() }, %$vars );
310  
311 # Add c => $self in as a param for convenient access to sessions and such
312 13 ▶ $params{c} ||= $self;
Condition33%1/3
$params{"c"} ||= $self
l!l&&r!l&&!r
0130
MC/DC0%0/2
$params{"c"} ||= $self
$params{"c"} $self
Truth table33%1/3
A ||= B
A= $params{"c"}B= $self
ABresult
1X1
011
000
313  
314 13 ▶ $self->tt_obj->process($file, \%params, \$html) || croak $self->tt_obj->error();
Branch: 1 branch
BranchTrueFalseTotal
$self->tt_obj->process($file, \%params, \$html) or croak($self->tt_obj->error) 1 12 13
315  
316 # Call tt_post_process hook
317 12 ▶ $self->tt_post_process(\$html) if $self->can('tt_post_process');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("tt_post_process") 7 5 12
318 12 ▶ $self->call_hook('tt_post_process', \$html) if $can_call_hook;
Branch: 1 branch
BranchTrueFalseTotal
if $can_call_hook 12 0 12
319  
320 12 _tt_add_devpopup_info($self, $template_name, \%params);
321  
322 12 return \$html;
323 }
324  
325 ##############################################
326 ###
327 ### tt_include_path
328 ###
329 ##############################################
330 #
331 # Change the include path after the template object
332 # has already been created
333 #
334 sub tt_include_path {
335 3 ▶ my $self = shift;
Subroutine tt_include_path: called CC=3 SCAR=11.0
Pod documented
336  
337 3 ▶ return $self->tt_obj->context->load_templates->[0]->include_path unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
338 2 ▶ $self->tt_obj->context->load_templates->[0]->include_path(ref($_[0]) ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
339  
340 2 return;
341 }
342  
343 ##############################################
344 ###
345 ### tt_template_name
346 ###
347 ##############################################
348 #
349 # Auto-generate the filename of a template based on
350 # the current module, and the name of the
351 # function that called us.
352 #
353 sub tt_template_name {
354 9 ▶ my $self = shift;
Subroutine tt_template_name: called CC=2 SCAR=6.9
Pod documented
355  
356 9 my ($tt, $options, $frompkg) = _get_object_or_options($self);
357  
358 9 ▶ my $func = $options->{TEMPLATE_NAME_GENERATOR} || \&__tt_template_name;
Condition100%2/2
$options->{'TEMPLATE_NAME_GENERATOR'} || \&CGI::Application::Plugin::TT::__tt_template_name
l!l
27
MC/DC100%1/1
$options->{'TEMPLATE_NAME_GENERATOR'} || \&CGI::Application::Plugin::TT::__tt_template_name
$options->{'TEMPLATE_NAME_GENERATOR'}
Truth table100%2/2
A
A= $options->{'TEMPLATE_NAME_GENERATOR'}
Aresult
11
00
359 9 return $self->$func(@_);
360 }
361  
362 ##############################################
363 ###
364 ### __tt_template_name
365 ###
366 ##############################################
367 #
368 # Generate the filename of a template based on
369 # the current module, and the name of the
370 # function that called us.
371 #
372 # example:
373 # module $self is blessed into: My::Module
374 # function name that called us: my_function
375 #
376 # generates: My/Module/my_function.tmpl
377 #
378 sub __tt_template_name {
379 7 ▶ my $self = shift;
Subroutine __tt_template_name: called CC=2 SCAR=6.9
380 7 ▶ my $uplevel = shift || 0;
Condition100%2/2
shift() || 0
l!l
61
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
381  
382 # the directory is based on the object's package name
383 7 my $dir = File::Spec->catdir(split(/::/, ref($self)));
384  
385 # the filename is the method name of the caller plus
386 # whatever offset the user asked for
387 7 (caller(2+$uplevel))[3] =~ /([^:]+)$/;
388 7 my $name = $1;
389  
390 7 return File::Spec->catfile($dir, $name.'.tmpl');
391 }
392  
393 ##
394 ## Private methods
395 ##
396 sub _set_object {
397 18 ▶ my $self = shift;
Subroutine _set_object: called CC=3 SCAR=11.0
398 18 my $tt = shift;
399 18 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 16 2 18
400  
401 18 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 16 2 18
402 16 $self->{__TT_OBJECT} = $tt;
403 } else {
404 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
405 2 ${$class.'::__TT_OBJECT'} = $tt;
406 }
407 }
408  
409 sub _get_object_or_options {
410 38 ▶ my $self = shift;
Subroutine _get_object_or_options: called CC=8 SCAR=21.0
411 38 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 38 0 38
412  
413 # Handle the simple case by looking in the object first
414 38 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 38 0 38
415 38 ▶ return ($self->{__TT_OBJECT}, $self->{__TT_CONFIG}) if $self->{__TT_OBJECT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__TT_OBJECT"} 7 31 38
416 31 ▶ return (undef, $self->{__TT_CONFIG}) if $self->{__TT_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__TT_CONFIG"} 23 8 31
417 }
418  
419 # See if we can find them in the class hierarchy
420 # We look at each of the modules in the @ISA tree, and
421 # their parents as well until we find either a tt
422 # object or a set of configuration parameters
423 8 require Class::ISA;
424 8 foreach my $super ($class, Class::ISA::super_path($class)) {
425 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
426 8 ▶ return (${$super.'::__TT_OBJECT'}, ${$super.'::__TT_CONFIG'}, $super) if ${$super.'::__TT_OBJECT'};
Branch: 1 branch
BranchTrueFalseTotal
if ${$super . '::__TT_OBJECT';} 6 2 8
427 2 ▶ return (undef, ${$super.'::__TT_CONFIG'}, $super) if ${$super.'::__TT_CONFIG'};
Branch: 1 branch
BranchTrueFalseTotal
if ${$super . '::__TT_CONFIG';} 2 0 2
428 }
429 0 return;
430 }
431  
432 ##############################################
433 ###
434 ### _tt_add_devpopup_info
435 ###
436 ##############################################
437 #
438 # This method will look to see if the devpopup
439 # plugin is being used, and will display all the
440 # parameters that were passed to the template.
441 #
442 sub _tt_add_devpopup_info {
443 12 ▶ my $self = shift;
Subroutine _tt_add_devpopup_info: called CC=4 SCAR=13.9
444 12 my $name = shift;
445 12 my $params = shift;
446  
447 12 ▶ return unless UNIVERSAL::can($self, 'devpopup');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($self, "devpopup") 11 1 12
448  
449 1 my %params = %$params;
450 1 foreach my $key (keys %params) {
451 3 ▶ if (my $class = Scalar::Util::blessed($params{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($params{$key})) 1 2 3
452 1 $params{$key} = "Object:$class";
453 }
454 }
455  
456 1 require Data::Dumper;
457 1 my $dumper = Data::Dumper->new([\%params]);
458 1 $dumper->Varname('Params');
459 1 $dumper->Indent(2);
460 1 my $dump = $dumper->Dump();
461  
462 # Entity encode the output since it will be displayed on a webpage and we
463 # want all HTML content rendered as text (borrowed from HTML::Entities)
464 1 $dump =~ s/([^\n\r\t !\#\$%\(-;=?-~])/sprintf "&#x%X;", ord($1)/ge;
465  
466 1 $self->devpopup->add_report(
467 title => "TT params for $name",
468 summary => "All template parameters passed to template $name",
469 report => qq{<div style="font-size: 80%"><pre>$dump</pre></div>},
470 );
471  
472 1 return;
473 }
474  
475  
476 10 1;
477 __END__
↑ Summary Build »