blib/lib/HTML/KTemplate.pm

filter: Statementstmt 96.0% 295 / 307 Branchbran 81.9% 190 / 232 Conditioncond 63.7% 44 / 69 MC/DCmcdc 41.8% 18 / 43 Subroutinesub 90.9% 20 / 22 Podpod 100% 10 / 10 totaltotal 84.4% 577 / 683 CC 146 SCAR 30.7
CC
146
Cov
87%
_parse
44.8
_load
24.6
_loop
24.1
CRAP 21.6
↑ Summary
1  
2 #=======================================================================
3 #
4 # Copyright (c) 2002-2003 Kasper Dziurdz. All rights reserved.
5 #
6 # This module is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # Artistic License for more details.
13 #
14 # Please email me any comments, questions, suggestions or bug
15 # reports to: <kasper@repsak.de>
16 #
17 #=======================================================================
18  
19 package HTML::KTemplate;
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
25 $VAR_START_TAG $VAR_END_TAG
26 $BLOCK_START_TAG $BLOCK_END_TAG
27 $INCLUDE_START_TAG $INCLUDE_END_TAG
28 $ROOT $CHOMP $VERSION $CACHE
29 $FIRST $INNER $LAST
30 1 );
31  
32 1 $VERSION = '1.33';
33  
34 1 $VAR_START_TAG = '[%';
35 1 $VAR_END_TAG = '%]';
36  
37 1 $BLOCK_START_TAG = '<!--';
38 1 $BLOCK_END_TAG = '-->';
39  
40 1 $INCLUDE_START_TAG = '<!--';
41 1 $INCLUDE_END_TAG = '-->';
42  
43 1 $ROOT = undef;
44 1 $CHOMP = 1;
45 1 $CACHE = {};
46  
47 1 $FIRST = { 'FIRST' => 1, 'first' => 1 };
48 1 $INNER = { 'INNER' => 1, 'inner' => 1 };
49 1 $LAST = { 'LAST' => 1, 'last' => 1 };
50  
51  
52 sub TEXT () { 0 }
53 sub VAR () { 1 }
54 sub BLOCK () { 2 }
55 sub FILE () { 3 }
56 sub IF () { 4 }
57 sub ELSE () { 5 }
58 sub UNLESS () { 6 }
59 sub LOOP () { 7 }
60  
61 sub TYPE () { 0 }
62 sub IDENT () { 1 }
63 sub STACK () { 2 }
64  
65 sub NAME () { 0 }
66 sub PATH () { 1 }
67  
68  
69 36 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
70  
71 36 my $class = shift;
72 36 my $self = {
73 'vars' => [{}], # values for template vars
74 'loop' => [], # loop context variables
75 'block' => undef, # current block reference
76 'files' => [], # file paths for include
77 'output' => '', # template output
78 'config' => { # configuration
79 'cache' => 0,
80 'strict' => 0,
81 'no_includes' => 0,
82 'max_includes' => 15,
83 'loop_vars' => 0,
84 'blind_cache' => 0,
85 'include_vars' => 0,
86 'parse_vars' => 0,
87 },
88 };
89  
90 36 ▶ $self->{'config'}->{'root'} = shift if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1 35 36
91 36 ▶ croak('Odd number of option parameters') if @_ % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 != 0 0 36 36
92  
93 # load in all option parameters
94 36 ▶ $self->{'config'}->{$_} = shift while $_ = lc shift;
Branch: 1 branch
BranchTrueFalseTotal
$_ = lc shift() and do { $self->{"config"}{$_} = shift() } 8 36 44
95  
96 $self->{'config'}->{'root'} = $ROOT
97 36 ▶ unless exists $self->{'config'}->{'root'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"config"}{"root"} 34 2 36
98  
99 $self->{'config'}->{'cache'} = 1
100 36 ▶ if $self->{'config'}->{'blind_cache'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"blind_cache"} 0 36 36
101  
102 36 bless ($self, $class);
103 36 return $self;
104  
105 }
106  
107  
108 59 ▶sub assign {
Subroutine assign: called CC=6 SCAR=17.9
Pod documented
109  
110 59 my $self = shift;
111 59 my ($target, $block);
112  
113 # odd number of arguments: block
114 59 ▶ if (@_ % 2 != 0 && @_ >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0 and @_ >= 3) 1 58 59
Condition100%3/3
@_ % 2 != 0 and @_ >= 3
!ll&&!rl&&r
5531
MC/DC100%2/2
@_ % 2 != 0 and @_ >= 3
@_ % 2 != 0 @_ >= 3
Truth table100%3/3
A and B
A= @_ % 2 != 0B= @_ >= 3
ABresult
0X0
100
111
115 1 $self->block(shift);
116 1 ++$block;
117 }
118
119 # if a block reference is defined,
120 # assign the variables to the block
121 $target = defined $self->{'block'}
122 25 ? $self->{'block'}->[ $#{ $self->{'block'} } ]
123 59 ▶ : $self->{'vars'}->[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'block'} ? : 25 34 59
124  
125 59 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 3 56 59
126 # copy data for faster variable lookup
127 3 @{ $target }{ keys %{$_[0]} } = values %{$_[0]};
128 } else {
129 56 my %assign = @_;
130 56 @{ $target }{ keys %assign } = values %assign;
131 }
132
133 # remove block reference
134 59 ▶ $self->block() if $block;
Branch: 1 branch
BranchTrueFalseTotal
if $block 1 58 59
135  
136 59 return 1;
137  
138 }
139  
140  
141 46 ▶sub block {
Subroutine block: called CC=9 SCAR=22.0
Pod documented
142 # - creates a new loop in the defined block
143 # - sets a reference so all future variable values will
144 # be assigned there (until this method is called again)
145  
146 46 my $self = shift;
147 46 my (@ident, $root, $key, $last_key);
148
149 # no argument: undefine block reference
150 46 ▶ if (!defined $_[0] || !length $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0] and length $_[0]) 5 41 46
Condition66%2/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
5041
MC/DC50%1/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table66%2/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
151 5 $self->{'block'} = undef;
152 5 return 1;
153 }
154
155 41 ▶ push @ident, split /\./, shift while @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { push @ident, split(/\./, shift(), 0) } 42 41 83
156 41 $last_key = pop @ident;
157
158 41 $root = $self->{'vars'}->[0];
159
160 41 foreach $key (@ident) {
161
162 # hash reference: perfect!
163 40 ▶ if (ref $root->{$key} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $root->{$key} eq 'HASH') { } 9 31 40
elsif (ref $root->{$key} eq 'ARRAY' and ref $root->{$key}[$#{$$root{$key};}] eq 'HASH') { } 22 9 31
164 9 $root = $root->{$key};
165 }
166
167 # array reference: block continues in hash
168 # reference at the end of the array
169 ▶ elsif (ref $root->{$key} eq 'ARRAY'
Condition66%2/3
ref $root->{$key} eq "ARRAY" and ref $root->{$key}[$#{$$root{$key};}] eq "HASH"
!ll&&!rl&&r
9022
MC/DC50%1/2
ref $root->{$key} eq "ARRAY" and ref $root->{$key}[$#{$$root{$key};}] eq "HASH"
ref $root->{$key} eq "ARRAY" ref $root->{$key}[$#{$$root{$key};}] eq "HASH"
Truth table66%2/3
A and B
A= ref $root->{$key} eq "ARRAY"B= ref $root->{$key}[$#{$$root{$key};}] eq "HASH"
ABresult
0X0
100
111
170 22 && ref $root->{$key}->[ $#{ $root->{$key} } ] eq 'HASH' ) {
171 22 $root = $root->{$key}->[ $#{ $root->{$key} } ];
172 }
173
174 else { # create new hash reference
175 9 $root = $root->{$key} = {};
176 }
177
178 }
179
180 41 ▶ if (ref $root->{$last_key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $root->{$last_key} eq 'ARRAY') { } 26 15 41
181 # block exists: add new loop
182 26 push @{ $root->{$last_key} }, {};
183 } else {
184 # create new block
185 15 $root->{$last_key} = [{}];
186 }
187
188 41 $self->{'block'} = $root->{$last_key};
189
190 41 return 1;
191
192 }
193  
194  
195 39 ▶sub process {
Subroutine process: called CC=3 SCAR=11.1
Pod documented
196  
197 39 my $self = shift;
198  
199 39 foreach (@_) {
200 41 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 41 41
201 41 $self->_include($_);
202 }
203  
204 36 return 1;
205
206 }
207  
208  
209 69 ▶sub _include {
Subroutine _include: called CC=7 SCAR=19.5
210  
211 69 my $self = shift;
212 69 my $filename = shift;
213 69 my ($stack, $filepath);
214
215 # check whether includes are disabled
216 69 ▶ if ($self->{'config'}->{'no_includes'} && scalar @{ $self->{'files'} } != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"no_includes"} and scalar @{$self->{"files"};} != 0) 1 68 69
Condition100%3/3
$self->{"config"}{"no_includes"} and scalar @{$self->{"files"};} != 0
!ll&&!rl&&r
6711
MC/DC100%2/2
$self->{"config"}{"no_includes"} and scalar @{$self->{"files"};} != 0
$self->{"config"}{"no_includes"} scalar @{$self->{"files"};} != 0
Truth table100%3/3
A and B
A= $self->{"config"}{"no_includes"}B= scalar @{$self->{"files"};} != 0
ABresult
0X0
100
111
217 croak('Include blocks are disabled at ' . $self->{'files'}->[0]->[NAME])
218 1 ▶ if $self->{'config'}->{'strict'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"strict"} 1 0 1
219 0 return; # no strict
220 }
221
222 # check for recursive includes
223 croak('Recursive includes: maximum recursion depth of ' . $self->{'config'}->{'max_includes'} . ' files exceeded')
224 68 ▶ if scalar @{ $self->{'files'} } > $self->{'config'}->{'max_includes'};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"files"};} > $self->{"config"}{"max_includes"} 1 67 68
225  
226 67 ($stack, $filepath) = $self->_load($filename);
227
228 # add file path to use as include path
229 67 ▶ unshift @{ $self->{'files'} }, [ $filename, $filepath ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $filepath 62 5 67
230 if defined $filepath;
231
232 # create output
233 67 $self->_output($stack);
234
235 # delete file info if it was added
236 49 ▶ shift @{ $self->{'files'} } if defined $filepath;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filepath 44 5 49
237  
238 }
239  
240  
241 67 ▶sub _load {
Subroutine _load: called CC=16 SCAR=32.0
242 # - loads the template file from cache or hard drive
243 # - returns the parsed stack and the full template path
244  
245 67 my $self = shift;
246 67 my $filename = shift;
247 67 my ($filepath, $mtime, $filedata);
248
249 # slurp the file
250 67 local $/ = undef;
251
252 # when the passed argument is a reference to a scalar,
253 # array or file handle, load and use it as template
254  
255 67 ▶ if (ref $filename eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq "SCALAR") 2 65 67
256 # skip undef and do not change passed scalar
257 2 ▶ $filedata = defined $$filename ? $$filename : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $$filename ? : 2 0 2
258 2 return $self->_parse(\$filedata, '[scalar_ref]');
259 }
260  
261 65 ▶ if (ref $filename eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq "ARRAY") 1 64 65
262 1 $filedata = join("", @$filename);
263 1 return $self->_parse(\$filedata, '[array_ref]');
264 }
265  
266 64 ▶ if (ref $filename eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq "GLOB") 1 63 64
267 1 $filedata = readline($$filename);
268 1 ▶ $filedata = '' unless defined $filedata; # skip undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filedata 0 1 1
269 1 return $self->_parse(\$filedata, '[file_handle]');
270 }
271  
272 # file handle (no reference)
273 63 ▶ if (ref \$filename eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$filename eq "GLOB") 1 62 63
274 1 $filedata = readline($filename);
275 1 ▶ $filedata = '' unless defined $filedata; # skip undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filedata 0 1 1
276 1 return $self->_parse(\$filedata, '[file_handle]');
277 }
278  
279 62 ($filepath, $mtime) = $self->_find($filename);
280
281 62 ▶ croak("Can't open file $filename: file not found")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filepath 0 62 62
282 unless defined $filepath;
283
284 62 ▶ if ($self->{'config'}->{'cache'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"cache"}) 0 62 62
285 # load parsed template from cache
286 0 $filedata = $CACHE->{$filepath};
287
288 return ($filedata->[0], $filepath)
289 0 ▶ if $self->{'config'}->{'blind_cache'} && defined $filedata;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"blind_cache"} and defined $filedata 0 0 0
Condition0%0/3
$self->{"config"}{"blind_cache"} and defined $filedata
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"config"}{"blind_cache"} and defined $filedata
$self->{"config"}{"blind_cache"} defined $filedata
Truth table0%0/3
A and B
A= $self->{"config"}{"blind_cache"}B= defined $filedata
ABresult
0X0
100
111
290 0 ▶ return ($filedata->[0], $filepath)
Branch: 1 branch
BranchTrueFalseTotal
if defined $filedata and $filedata->[1] == $mtime 0 0 0
Condition0%0/3
defined $filedata and $filedata->[1] == $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filedata and $filedata->[1] == $mtime
defined $filedata $filedata->[1] == $mtime
Truth table0%0/3
A and B
A= defined $filedataB= $filedata->[1] == $mtime
ABresult
0X0
100
111
291 if defined $filedata && $filedata->[1] == $mtime;
292 }
293
294 62 ▶ open (TEMPLATE, '<' . $filepath) ||
Branch: 1 branch
BranchTrueFalseTotal
open TEMPLATE, '<' . $filepath or croak("Can't open file ${filename}: $!") 62 0 62
295 croak("Can't open file $filename: $!");
296 62 $filedata = <TEMPLATE>;
297 62 close TEMPLATE;
298
299 62 $filedata = $self->_parse(\$filedata, $filename);
300
301 # commit to cache
302 $CACHE->{$filepath} = [ $filedata, $mtime ]
303 62 ▶ if $self->{'config'}->{'cache'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"cache"} 0 62 62
304  
305 62 return ($filedata, $filepath);
306  
307 }
308  
309  
310 62 ▶sub _find {
Subroutine _find: called CC=13 SCAR=28.2
311 # - searches for the template file in the
312 # root path or from where it was included
313 # - returns a full path and the mtime or
314 # undef if the file cannot be found
315  
316 62 my $self = shift;
317 62 my $filename = shift;
318 62 my ($inclpath, $filepath);
319  
320 $filepath = defined $self->{'config'}->{'root'}
321 62 ▶ ? File::Spec->catfile($self->{'config'}->{'root'}, $filename)
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'config'}{'root'} ? : 3 59 62
322 : File::Spec->canonpath($filename);
323  
324 return $filepath if $self->{'config'}->{'blind_cache'}
325 62 ▶ && defined $CACHE->{$filepath};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath} 0 62 62
Condition33%1/3
$self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath}
!ll&&!rl&&r
6200
MC/DC0%0/2
$self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath}
$self->{"config"}{"blind_cache"} defined $HTML::KTemplate::CACHE->{$filepath}
Truth table33%1/3
A and B
A= $self->{"config"}{"blind_cache"}B= defined $HTML::KTemplate::CACHE->{$filepath}
ABresult
0X0
100
111
326 62 ▶ return ($filepath, (stat(_))[9]) if -e $filepath;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filepath 39 23 62
327  
328 # check path from where the file was included
329 23 ▶ if (defined $self->{'files'}->[0]->[PATH]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"files"}[0][1]) 23 0 23
330 23 $inclpath = $self->{'files'}->[0]->[PATH];
331 23 $inclpath = [ File::Spec->splitdir($inclpath) ];
332 23 $inclpath->[$#$inclpath] = $filename;
333 23 $filepath = File::Spec->catfile(@$inclpath);
334 23 $filepath = File::Spec->canonpath($filepath);
335
336 return $filepath if $self->{'config'}->{'blind_cache'}
337 23 ▶ && defined $CACHE->{$filepath};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath} 0 23 23
Condition33%1/3
$self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath}
!ll&&!rl&&r
2300
MC/DC0%0/2
$self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath}
$self->{"config"}{"blind_cache"} defined $HTML::KTemplate::CACHE->{$filepath}
Truth table33%1/3
A and B
A= $self->{"config"}{"blind_cache"}B= defined $HTML::KTemplate::CACHE->{$filepath}
ABresult
0X0
100
111
338 23 ▶ return ($filepath, (stat(_))[9]) if -e $filepath;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filepath 21 2 23
339
340 # check path from variable
341 2 ▶ if ($self->{'config'}->{'include_vars'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"include_vars"}) 2 0 2
342 2 $filepath = File::Spec->canonpath( $self->_get($filename) );
343 return $filepath if $self->{'config'}->{'blind_cache'}
344 2 ▶ && defined $CACHE->{$filepath};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath} 0 2 2
Condition33%1/3
$self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"config"}{"blind_cache"} and defined $HTML::KTemplate::CACHE->{$filepath}
$self->{"config"}{"blind_cache"} defined $HTML::KTemplate::CACHE->{$filepath}
Truth table33%1/3
A and B
A= $self->{"config"}{"blind_cache"}B= defined $HTML::KTemplate::CACHE->{$filepath}
ABresult
0X0
100
111
345 2 ▶ return ($filepath, (stat(_))[9]) if -e $filepath;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filepath 2 0 2
346 }
347
348 }
349
350 0 return undef;
351
352 }
353  
354  
355 67 ▶sub _parse {
Subroutine _parse: called CC=42 SCAR=38.0
356 # - parses the template data passed as a reference
357 # - returns the finished stack
358  
359 67 my $self = shift;
360 67 my $filedata = shift;
361 67 my $filename = shift;
362 67 my ($text, $tag, $type, $ident);
363 67 my ($regexp, $line, $block, $space);
364 67 my (@idents, @pstacks);
365  
366 67 $line = 1; # current line
367 67 @pstacks = ([]);
368
369 # block and include tags are the same by default.
370 # if that wasn't changed, use a faster regexp.
371
372 67 ▶ $regexp = $BLOCK_START_TAG eq $INCLUDE_START_TAG
Branch: 1 branch
BranchTrueFalseTotal
$HTML::KTemplate::BLOCK_START_TAG eq $HTML::KTemplate::INCLUDE_START_TAG && $HTML::KTemplate::BLOCK_END_TAG eq $HTML::KTemplate::INCLUDE_END_TAG ? : 63 4 67
Condition66%2/3
$HTML::KTemplate::BLOCK_START_TAG eq $HTML::KTemplate::INCLUDE_START_TAG and $HTML::KTemplate::BLOCK_END_TAG eq $HTML::KTemplate::INCLUDE_END_TAG
!ll&&!rl&&r
4063
MC/DC50%1/2
$HTML::KTemplate::BLOCK_START_TAG eq $HTML::KTemplate::INCLUDE_START_TAG and $HTML::KTemplate::BLOCK_END_TAG eq $HTML::KTemplate::INCLUDE_END_TAG
$HTML::KTemplate::BLOCK_START_TAG eq $HTML::KTemplate::INCLUDE_START_TAG $HTML::KTemplate::BLOCK_END_TAG eq $HTML::KTemplate::INCLUDE_END_TAG
Truth table66%2/3
A and B
A= $HTML::KTemplate::BLOCK_START_TAG eq $HTML::KTemplate::INCLUDE_START_TAGB= $HTML::KTemplate::BLOCK_END_TAG eq $HTML::KTemplate::INCLUDE_END_TAG
ABresult
0X0
100
111
373 && $BLOCK_END_TAG eq $INCLUDE_END_TAG
374
375 ? qr/^
376 (.*?)
377 (
378 \Q$VAR_START_TAG\E
379 \s*
380 ([\w.-]+)
381 \s*
382 \Q$VAR_END_TAG\E
383 |
384 \Q$BLOCK_START_TAG\E
385 \s*
386 (?:
387 (
388 [Bb][Ee][Gg][Ii][Nn]
389 |
390 [Ee][Nn][Dd]
391 |
392 [Ii][Ff]
393 |
394 [Ll][Oo][Oo][Pp]
395 |
396 [Ee][Ll][Ss][Ee]
397 |
398 [Uu][Nn][Ll][Ee][Ss][Ss]
399 )
400 (?: \s+ ([\w.-]+) )?
401 |
402 ([Ii][Nn][Cc][Ll][Uu][Dd][Ee])\s+
403 (?: "([^"]*?)" | '([^']*?)' | (\S*?) )
404 )
405 \s*
406 \Q$BLOCK_END_TAG\E
407 )
408 /sx
409
410 : qr/^
411 (.*?)
412 (
413 \Q$VAR_START_TAG\E
414 \s*
415 ([\w.-]+)
416 \s*
417 \Q$VAR_END_TAG\E
418 |
419 \Q$BLOCK_START_TAG\E
420 \s*
421 (
422 [Bb][Ee][Gg][Ii][Nn]
423 |
424 [Ee][Nn][Dd]
425 |
426 [Ii][Ff]
427 |
428 [Ll][Oo][Oo][Pp]
429 |
430 [Ee][Ll][Ss][Ee]
431 |
432 [Uu][Nn][Ll][Ee][Ss][Ss]
433 )
434 (?: \s+ ([\w.-]+) )?
435 \s*
436 \Q$BLOCK_END_TAG\E
437 |
438 \Q$INCLUDE_START_TAG\E
439 \s*
440 ([Ii][Nn][Cc][Ll][Uu][Dd][Ee])\s+
441 (?: "([^"]*?)" | '([^']*?)' | (\S*?) )
442 \s*
443 \Q$INCLUDE_END_TAG\E
444 )
445 /sx;
446
447 67 ▶ while ($$filedata =~ s/$regexp//sx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$filedata =~ s/$regexp//sx) 277 67 344
448  
449 277 $text = $1; # preceding text
450 277 $tag = $2; # whole tag (needed for line count)
451 277 ▶ $type = $4 || $6; # tag type (undef for var)
Condition100%3/3
$4 || $6
l!l&&r!l&&!r
1843360
MC/DC100%2/2
$4 || $6
$4 $6
Truth table100%3/3
A || B
A= $4B= $6
ABresult
1X1
011
000
452 277 ▶ $ident = defined $3 ? $3 : defined $5 ? $5 : defined $7 ? $7 :
Branch: 5 branches
BranchTrueFalseTotal
defined $3 ? : 60 217 277
defined $5 ? : 169 48 217
defined $7 ? : 4 44 48
defined $8 ? : 4 40 44
defined $9 ? : 25 15 40
453 defined $8 ? $8 : defined $9 ? $9 : undef;
454
455 # get line position
456 277 $line += ($text =~ tr/\n//);
457
458 277 ▶ if ($CHOMP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::KTemplate::CHOMP) 277 0 277
459 # delete newline after last block tag
460 277 ▶ $space ? $text =~ s/^[ \t]*\r?\n// : $text =~ s/^[ \t]*\r?\n/ / if $block;
Branch: 2 branches
BranchTrueFalseTotal
if $block 158 119 277
$space ? : 150 8 158
461
462 # check this tag is not a var or include
463 277 ▶ $block = $type && $type !~ /^[Ii]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type && !($type =~ /^[Ii]/) ? : 170 107 277
Condition100%3/3
$type and not $type =~ /^[Ii]/
!ll&&!rl&&r
6047170
MC/DC100%2/2
$type and not $type =~ /^[Ii]/
$type not $type =~ /^[Ii]/
Truth table100%3/3
A and not(B)
A= $typeB= not $type =~ /^[Ii]/
ABresult
0X0
100
111
464 277 $space = 0; # no space was added (default)
465
466 # remove newline preceding this block tag
467 277 ▶ $space = 1 if $block && $text =~ s/\r?\n[ \t]*\z/ /;
Branch: 1 branch
BranchTrueFalseTotal
if $block and $text =~ s/\r?\n[ \t]*\z/ / 162 115 277
Condition100%3/3
$block and $text =~ s/\r?\n[ \t]*\z/ /
!ll&&!rl&&r
1078162
MC/DC100%2/2
$block and $text =~ s/\r?\n[ \t]*\z/ /
$block $text =~ s/\r?\n[ \t]*\z/ /
Truth table100%3/3
A and B
A= $blockB= $text =~ s/\r?\n[ \t]*\z/ /
ABresult
0X0
100
111
468 }
469
470 # the first element of the @pstacks array contains a reference
471 # to the current parse stack where the template data is added.
472
473 277 ▶ push @{$pstacks[0]}, [ TEXT, $text ] if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 277 0 277
474  
475 277 ▶ if (!defined $type) {
Branch: 8 branches
BranchTrueFalseTotal
if (not defined $type) { } 60 217 277
elsif ($type =~ /^[Bb]/) { } 47 170 217
elsif ($type =~ /^[Ee][Nn]/) { } 86 84 170
elsif ($type =~ /^[Ii][Ff]/) { } 14 70 84
elsif ($type =~ /^[Uu]/) { } 14 56 70
elsif ($type =~ /^[Ee]/) { } 12 44 56
elsif ($type =~ /^[Ii]/) { } 33 11 44
elsif ($type =~ /^[Ll]/) { } 11 0 11
476
477 60 push @{$pstacks[0]}, [ VAR, $ident ];
478
479 } elsif ($type =~ /^[Bb]/) {
480
481 47 ▶ croak("Parse error: invalid param in block tag at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ident 0 47 47
482 unless length $ident;
483
484 # create a new parse stack were all data
485 # will be added until the block ends.
486 47 unshift @pstacks, [];
487
488 # create a reference to this new parse stack in the old one
489 # so the block data doesn't get lost after the block ends.
490 47 push @{$pstacks[1]}, [ BLOCK, $ident, $pstacks[0] ];
491
492 # add block type and ident for syntax checking
493 47 unshift @idents, [ 'BEGIN', $ident ];
494
495 } elsif ($type =~ /^[Ee][Nn]/) {
496
497 86 ▶ croak("Parse error: block closed but never opened at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
if scalar @idents == 0 0 86 86
498 if scalar @idents == 0;
499  
500 86 ▶ croak("Parse error: invalid param in block tag at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
if defined $ident and uc $ident eq "BEGIN" || uc $ident ne $idents[0][0] and $ident ne $idents[0][1] 0 86 86
Condition77%7/9
defined $ident and uc $ident eq "BEGIN" || uc $ident ne $idents[0][0] and $ident ne $idents[0][1]
!ll&&!rl&&r
10760
defined $ident and uc $ident eq "BEGIN" || uc $ident ne $idents[0][0]
!ll&&!rl&&r
8276
uc $ident eq "BEGIN" or uc $ident ne $idents[0][0]
l!l&&r!l&&!r
0762
MC/DC0%0/4
defined $ident and uc $ident eq "BEGIN" || uc $ident ne $idents[0][0] and $ident ne $idents[0][1]
defined $ident uc $ident eq "BEGIN" uc $ident ne $idents[0][0] $ident ne $idents[0][1]
Truth table50%3/6
A and B or C and D
A= defined $identB= uc $ident eq "BEGIN"C= uc $ident ne $idents[0][0]D= $ident ne $idents[0][1]
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
501 if defined $ident && (uc $ident eq 'BEGIN' || uc $ident ne $idents[0]->[TYPE])
502 && $ident ne $idents[0]->[IDENT];
503
504 86 shift @pstacks;
505 86 shift @idents;
506
507 } elsif ($type =~ /^[Ii][Ff]/) {
508
509 14 ▶ croak("Parse error: invalid param in if tag at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ident 0 14 14
510 unless length $ident;
511
512 14 unshift @pstacks, [];
513 14 push @{$pstacks[1]}, [ IF , $ident, $pstacks[0] ];
514 14 unshift @idents, [ 'IF', $ident ];
515
516 } elsif ($type =~ /^[Uu]/) {
517
518 14 ▶ croak("Parse error: invalid param in unless tag at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ident 0 14 14
519 unless length $ident;
520
521 14 unshift @pstacks, [];
522 14 push @{$pstacks[1]}, [ UNLESS , $ident, $pstacks[0] ];
523 14 unshift @idents, [ 'UNLESS', $ident ];
524
525 } elsif ($type =~ /^[Ee]/) {
526  
527 12 ▶ croak("Parse error: found else tag with no matching block at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
if scalar @idents == 0 0 12 12
528 if scalar @idents == 0;
529
530 12 ▶ croak("Parse error: invalid param in else tag at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
if defined $ident and $ident ne $idents[0][1] 0 12 12
Condition66%2/3
defined $ident and $ident ne $idents[0][1]
!ll&&!rl&&r
750
MC/DC0%0/2
defined $ident and $ident ne $idents[0][1]
defined $ident $ident ne $idents[0][1]
Truth table66%2/3
A and B
A= defined $identB= $ident ne $idents[0][1]
ABresult
0X0
100
111
531 if defined $ident && $ident ne $idents[0]->[IDENT];
532
533 12 shift @pstacks; # close current block
534 12 unshift @pstacks, []; # and create a new one.
535 12 push @{$pstacks[1]}, [ ELSE, undef, $pstacks[0] ];
536
537 } elsif ($type =~ /^[Ii]/) {
538
539 33 ▶ croak("Parse error: file to include not defined at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ident 0 33 33
540 unless length $ident;
541
542 33 push @{$pstacks[0]}, [ FILE, $ident ];
543
544 } elsif ($type =~ /^[Ll]/) {
545
546 11 ▶ croak("Parse error: invalid param in loop tag at $filename line $line")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ident 0 11 11
547 unless length $ident;
548  
549 11 unshift @pstacks, [];
550 11 push @{$pstacks[1]}, [ LOOP , $ident, $pstacks[0] ];
551 11 unshift @idents, [ 'LOOP', $ident ];
552  
553 }
554  
555 # tag might contain newline
556 277 $line += ($tag =~ tr/\n//);
557  
558 }
559  
560 # chomp and add remaining text not recognized by the regexp
561 67 ▶ $$filedata =~ s/^[ \t]*\n// if $CHOMP && $block;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::KTemplate::CHOMP and $block 12 55 67
Condition66%2/3
$HTML::KTemplate::CHOMP and $block
!ll&&!rl&&r
05512
MC/DC50%1/2
$HTML::KTemplate::CHOMP and $block
$HTML::KTemplate::CHOMP $block
Truth table66%2/3
A and B
A= $HTML::KTemplate::CHOMPB= $block
ABresult
0X0
100
111
562 67 push @{$pstacks[0]}, [ TEXT, $$filedata ];
563  
564 67 ▶ croak("Parse error: block not closed at $filename")
Branch: 1 branch
BranchTrueFalseTotal
if @idents > 0 0 67 67
565 if @idents > 0;
566  
567 67 return $pstacks[0];
568  
569 }
570  
571  
572 186 ▶sub _output {
Subroutine _output: called CC=10 SCAR=23.0
573  
574 186 my $self = shift;
575 186 my $stack = shift;
576 186 my ($line, $looped);
577
578 186 foreach $line (@$stack) { # create template output
579 $line->[TYPE] == VAR ? $self->{'output'} .= $self->_value( $line->[IDENT] ) :
580 802 ▶ $line->[TYPE] == TEXT ? $self->{'output'} .= $line->[IDENT] :
Branch: 8 branches
BranchTrueFalseTotal
$line->[0] == 1 ? : 92 710 802
$line->[0] == 0 ? : 479 231 710
$line->[0] == 3 ? : 28 203 231
$line->[0] == 2 ? : 135 68 203
$line->[0] == 4 ? : 13 55 68
$line->[0] == 7 ? : 29 26 55
$line->[0] == 6 ? : 14 12 26
$line->[0] == 5 ? : 12 0 12
581 $line->[TYPE] == FILE ? $self->_include( $line->[IDENT] ) :
582 $line->[TYPE] == BLOCK ? $looped = $self->_loop( $line->[IDENT], $line->[STACK], BLOCK ) :
583 $line->[TYPE] == IF ? $looped = $self->_loop( $line->[IDENT], $line->[STACK], IF ) :
584 $line->[TYPE] == LOOP ? $looped = $self->_loop( $line->[IDENT], $line->[STACK], LOOP ) :
585 $line->[TYPE] == UNLESS ? $looped = $self->_loop( $line->[IDENT], $line->[STACK], UNLESS ) :
586 $line->[TYPE] == ELSE ? $looped = $self->_loop( $looped, $line->[STACK], ELSE ) : next;
587 }
588  
589 }
590  
591  
592 95 ▶sub _value {
Subroutine _value: called CC=8 SCAR=21.0
593  
594 95 my $self = shift;
595 95 my $ident = shift;
596 95 my $value = $self->_get($ident);
597
598 95 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 7 88 95
599 croak("No value found for variable $ident at " . $self->{'files'}->[0]->[NAME])
600 7 ▶ if $self->{'config'}->{'strict'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"strict"} 1 6 7
601 6 return ''; # no strict
602 }
603  
604 # if the value is a code reference the code
605 # is called and the output is returned
606
607 88 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 1 87 88
608 1 ▶ $value = &{$value} if ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "CODE" 1 0 1
609 1 ▶ return '' if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 1 1
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
610 }
611  
612 88 ▶ if ($self->{'config'}->{'parse_vars'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"parse_vars"}) 5 83 88
613 5 $value =~ s/ # replace template vars
614 \Q$VAR_START_TAG\E
615 \s*([\w.-]+)\s*
616 \Q$VAR_END_TAG\E
617 3 / $self->_value($1) /xge;
618 }
619  
620 88 return $value;
621  
622 }
623  
624  
625 203 ▶sub _loop {
Subroutine _loop: called CC=24 SCAR=31.8
626  
627 203 my $self = shift;
628 203 my $ident = shift;
629 203 my $stack = shift;
630 203 my $mode = shift;
631 203 my ($data, $vars, $skip);
632 203 my $loop_vars = 0;
633 203 my $loop_count = 0;
634  
635 203 ▶ if ($mode == BLOCK) {
Branch: 5 branches
BranchTrueFalseTotal
if ($mode == 2) { } 135 68 203
elsif ($mode == 7) { } 29 39 68
elsif ($mode == 4) { } 13 26 39
elsif ($mode == 6) { } 14 12 26
elsif ($mode == 5) { } 12 0 12
636
637 135 $data = $self->_get($ident);
638 135 ▶ return 0 unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 77 58 135
639
640 # no array reference: check the Boolean
641 # context to loop once or skip the block
642 58 ▶ unless (ref $data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 25 33 58
643 33 ▶ $data ? $data = [1] : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 29 4 33
644 # if statement: no loop vars
645 } else {
646 25 ▶ return 0 unless @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 2 23 25
647 23 $loop_vars = $self->{'config'}->{'loop_vars'};
648 }
649
650 } elsif ($mode == LOOP) {
651  
652 29 $data = $self->_get($ident);
653 29 ▶ return 0 unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 16 13 29
654 13 ▶ return 0 unless ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 6 7 13
655 7 ▶ return 0 unless @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 1 6 7
656 6 $loop_vars = $self->{'config'}->{'loop_vars'};
657  
658 } elsif ($mode == IF) {
659
660 13 $data = $self->_get($ident);
661 13 ▶ return 0 unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 12 13
662 12 ▶ $data ? $data = [1] : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 6 6 12
663
664 } elsif ($mode == UNLESS) {
665
666 14 $data = $self->_get($ident);
667 14 ▶ return 0 if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 9 5 14
668 5 $data = [1];
669
670 } elsif ($mode == ELSE) {
671
672 12 ▶ return 0 if $ident;
Branch: 1 branch
BranchTrueFalseTotal
if $ident 0 12 12
673 12 $data = [1];
674
675 }
676
677 81 foreach $vars (@$data) {
678
679 ref $vars eq 'HASH' # add current loop variables
680 119 ▶ ? (unshift @{ $self->{'vars'} }, $vars)
Branch: 1 branch
BranchTrueFalseTotal
ref $vars eq 'HASH' ? : 53 66 119
681 : ($skip = 1);
682  
683 119 ▶ if ($loop_vars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_vars) { } 15 104 119
684 15 ++$loop_count;
685
686 # add loop context variables
687 0 ▶ @$data == 1 ? unshift @{ $self->{'loop'} }, { %$FIRST, %$LAST } :
Branch: 1 branch
BranchTrueFalseTotal
$loop_count == 1 ? : 6 9 15
688 6 ▶ $loop_count == 1 ? unshift @{ $self->{'loop'} }, $FIRST :
Branch: 1 branch
BranchTrueFalseTotal
$loop_count == @$data ? : 6 3 9
689 6 $loop_count == @$data ? unshift @{ $self->{'loop'} }, $LAST :
690 15 ▶ unshift @{ $self->{'loop'} }, $INNER;
Branch: 1 branch
BranchTrueFalseTotal
@$data == 1 ? : 0 15 15
691
692 # create output
693 15 $self->_output($stack);
694  
695 # delete loop context variables
696 15 shift @{ $self->{'loop'} };
697
698 } else {
699
700 # create output
701 104 $self->_output($stack);
702
703 }
704  
705 !$skip # delete current loop variables
706 119 ▶ ? (shift @{ $self->{'vars'} })
Branch: 1 branch
BranchTrueFalseTotal
!$skip ? : 53 66 119
707 : ($skip = 0);
708 }
709  
710 81 return 1;
711 }
712  
713  
714 288 ▶sub _get {
Subroutine _get: called CC=11 SCAR=24.0
715 # - returns the variable value from the variable
716 # hash (considering the temporary loop variables)
717  
718 288 my $self = shift;
719 288 my (@ident, $root, $last_key, $skip);
720
721 288 @ident = split /\./, $_[0];
722 288 $last_key = pop @ident;
723
724 # check for loop context variables
725 return $self->{'loop'}->[0]->{$last_key} if $self->{'config'}->{'loop_vars'}
726 288 ▶ && @ident == 0 && exists $self->{'loop'}->[0]->{$last_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"loop_vars"} and @ident == 0 and exists $self->{"loop"}[0]{$last_key} 15 273 288
Condition83%5/6
$self->{"config"}{"loop_vars"} and @ident == 0 and exists $self->{"loop"}[0]{$last_key}
!ll&&!rl&&r
2373615
$self->{"config"}{"loop_vars"} and @ident == 0
!ll&&!rl&&r
237051
MC/DC66%2/3
$self->{"config"}{"loop_vars"} and @ident == 0 and exists $self->{"loop"}[0]{$last_key}
$self->{"config"}{"loop_vars"} @ident == 0 exists $self->{"loop"}[0]{$last_key}
Truth table75%3/4
A and B and C
A= $self->{"config"}{"loop_vars"}B= @ident == 0C= exists $self->{"loop"}[0]{$last_key}
ABCresult
0XX0
10X0
1100
1111
727
728 # loop values are prepended to the front of the
729 # var array so start with them first
730
731 273 foreach my $hash (@{ $self->{'vars'} }) {
732  
733 # speed up normal variable lookup
734 return $hash->{$last_key} if @ident == 0
735 427 ▶ && exists $hash->{$last_key};
Branch: 1 branch
BranchTrueFalseTotal
if @ident == 0 and exists $hash->{$last_key} 160 267 427
Condition100%3/3
@ident == 0 and exists $hash->{$last_key}
!ll&&!rl&&r
22245160
MC/DC100%2/2
@ident == 0 and exists $hash->{$last_key}
@ident == 0 exists $hash->{$last_key}
Truth table100%3/3
A and B
A= @ident == 0B= exists $hash->{$last_key}
ABresult
0X0
100
111
736  
737 267 $root = $hash; # do not change the hash
738  
739 267 foreach my $key (@ident) {
740
741 30 ▶ if (ref $root eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $root eq 'HASH') { } 28 2 30
742 # go down the hash structure
743 28 $root = $root->{$key};
744 }
745
746 else {
747 # nothing found
748 2 $skip = 1; last;
749 }
750
751 }
752
753 267 ▶ unless ($skip) { # return if found something
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) { } 2 265 267
754 265 ▶ return $root->{$last_key} if exists $root->{$last_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $root->{$last_key} 16 249 265
755 }
756
757 else { # try again
758 2 $skip = 0;
759 }
760  
761 }
762  
763 97 return undef;
764  
765 }
766  
767  
768 0 ▶sub print {
Subroutine print: not called CC=3 SCAR=24.8
Pod documented
769  
770 0 my $self = shift;
771 0 my $fh = shift;
772  
773 ref $fh eq 'GLOB' || ref \$fh eq 'GLOB'
774 ? CORE::print $fh $self->{'output'}
775 0 ▶ : CORE::print $self->{'output'};
Branch: 1 branch
BranchTrueFalseTotal
ref $fh eq 'GLOB' || ref \$fh eq 'GLOB' ? : 0 0 0
Condition0%0/3
ref $fh eq "GLOB" or ref \$fh eq "GLOB"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $fh eq "GLOB" or ref \$fh eq "GLOB"
ref $fh eq "GLOB" ref \$fh eq "GLOB"
Truth table0%0/3
A or B
A= ref $fh eq "GLOB"B= ref \$fh eq "GLOB"
ABresult
1X1
011
000
776  
777 0 return 1;
778  
779 }
780  
781  
782 33 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
783 33 my $self = shift;
784 33 my $temp = $self->{'output'};
785 33 return \$temp;
786 }
787  
788  
789 1 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
790 1 my $self = shift;
791 1 $self->clear_vars();
792 1 $self->clear_out();
793 1 return 1;
794 }
795  
796  
797 3 ▶sub clear_vars {
Subroutine clear_vars: called CC=1 SCAR=0.0
Pod documented
798 3 my $self = shift;
799 3 $self->{'vars'} = [{}];
800 3 $self->block();
801 3 return 1;
802 }
803  
804  
805 3 ▶sub clear_out {
Subroutine clear_out: called CC=1 SCAR=0.0
Pod documented
806 3 my $self = shift;
807 3 $self->{'output'} = '';
808 3 return 1;
809 }
810  
811  
812 0 ▶sub clear_cache {
Subroutine clear_cache: not called CC=1 SCAR=6.9
Pod documented
813 0 $CACHE = {};
814 0 return 1;
815 }
816  
817  
818 1 1;
819  
820  
821  
822 =head1 NAME
823  
824 HTML::KTemplate - Perl module to process HTML templates.
825  
826  
827 =head1 SYNOPSIS
828  
829 B<CGI-Script:>
830  
831 #!/usr/bin/perl -w
832 use HTML::KTemplate;
833
834 $tpl = HTML::KTemplate->new('path/to/templates');
835
836 $tpl->assign( TITLE => 'Template Test Page' );
837 $tpl->assign( TEXT => 'Some welcome text ...' );
838
839 foreach (1 .. 3) {
840
841 $tpl->assign( LOOP,
842 TEXT => 'Just a test ...',
843 );
844
845 }
846
847 $tpl->process('template.tpl');
848
849 $tpl->print();
850  
851 B<Template:>
852  
853 <html>
854 <head><title>[% TITLE %]</title>
855 <body>
856
857 Hello! [% TEXT %]<p>
858
859 <!-- BEGIN LOOP -->
860
861 [% TEXT %]<br>
862
863 <!-- END LOOP -->
864
865 </body>
866 </html>
867  
868  
869 B<Output:>
870  
871 Hello! Some welcome text ...
872
873 Just a test ...
874 Just a test ...
875 Just a test ...
876  
877  
878 =head1 MOTIVATION
879  
880 Although there are many different template modules at CPAN, I couldn't find any that would meet my expectations. So I created this one with following features:
881  
882 =over 4
883  
884 =item *
885 Template syntax can consist only of variables and blocks.
886  
887 =item *
888 Support for multidimensional data structures.
889  
890 =item *
891 Everything is very simple and very fast.
892  
893 =item *
894 Still there are many advanced options available.
895  
896 =back
897  
898 Please email me any comments, suggestions or bug reports to <kasper@repsak.de>.
899  
900  
901 =head1 VARIABLES
902  
903 By default, template variables are embedded within C<[% %]> and may contain any alphanumeric characters including the underscore and the hyphen. The values for the variables are assigned with C<assign()>, passed as a hash or a hash reference.
904  
905 %hash = (
906 VARIABLE => 'Value',
907 );
908
909 $tpl->assign( %hash );
910 $tpl->assign(\%hash );
911 $tpl->assign( VARIABLE => 'Value' );
912  
913 To access a multidimensional hash data structure, the variable names are separated by a dot. In the following example, two values for the variables C<[% USER.NAME %]> and C<[% USER.EMAIL %]> are assigned:
914  
915 $tpl->assign(
916
917 USER => {
918 NAME => 'Kasper Dziurdz', # [% USER.NAME %]
919 EMAIL => 'kasper@repsak.de', # [% USER.EMAIL %]
920 },
921
922 );
923  
924 If the value of a variable is a reference to a subroutine, the subroutine is called and the returned string is included in the output. This is the only way to execute Perl code in a template.
925  
926 $tpl->assign(
927
928 BENCHMARK => sub {
929 # get benchmark data
930 return 'created in 0.01 seconds';
931 }
932
933 );
934  
935  
936 =head1 BLOCKS
937  
938 Blocks allow you to create loops and iterate over a part of a template or to write simple if-statements. A block begins with C<< <!-- BEGIN BLOCKNAME --> >> and ends with C<< <!-- END BLOCKNAME --> >>. This is an example of creating a block with the C<block()> method:
939  
940 $tpl->assign( HEADER => 'Some numbers:' );
941
942 @block_values = ('One', 'Two', 'Three', 'Four');
943
944 foreach (@block_values) {
945
946 $tpl->block('LOOP_NUMBERS');
947 $tpl->assign( NUMBER => $_ );
948 $tpl->assign( SOMETHING => '' );
949
950 }
951
952 $tpl->block(); # leave block
953
954 $tpl->assign( FOOTER => '...in words.' );
955  
956 Each time C<block()> is called it creates a new loop in the selected block. All variable values passed to C<assign()> are assigned only to this loop until a new loop is created or C<block()> is called without any arguments to assign global variables again. This is a template for the script above:
957  
958 [% HEADER %]
959
960 <!-- BEGIN LOOP_NUMBERS -->
961
962 [% NUMBER %]
963
964 <!-- END LOOP_NUMBERS -->
965
966 [% FOOTER %]
967  
968 Global variables (or outer block variables) are also available inside a block. However, if there is a block variable with the same name, the block variable is preferred.
969  
970 Because a block is a normal variable with an array reference, blocks can also be created without the C<block()> method:
971  
972 $tpl->assign(
973 HEADER => 'Some numbers:',
974 LOOP_NUMBERS =>
975 [
976 { NUMBER => 'One' },
977 { NUMBER => 'Two' },
978 { NUMBER => 'Three' },
979 { NUMBER => 'Four' },
980 ],
981 FOOTER => '...in words.',
982 );
983  
984 Loops within loops work as you would expect. To create a nested loop with C<block()>, you have to pass all block names separate as a list or joined with a dot, for example as C<BLOCK_1.BLOCK_2>. This way, a new loop for C<BLOCK_2> is created in the last loop of C<BLOCK_1>. The variable values are assigned with C<assign()>.
985  
986 foreach (@block_one) {
987
988 $tpl->block('BLOCK_1');
989 $tpl->assign(VAR => $_);
990
991 foreach (@block_two) {
992
993 $tpl->block('BLOCK_1', 'BLOCK_2');
994 $tpl->assign(VAR => $_);
995
996 }
997 }
998
999 $tpl->block(); # leave block
1000  
1001 The template would look like this:
1002  
1003 <!-- BEGIN BLOCK_1 -->
1004
1005 <!-- BEGIN BLOCK_2 -->
1006
1007 <!-- END BLOCK_2 -->
1008
1009 <!-- END BLOCK_1 -->
1010  
1011 B<Important: You have to call C<block()> without any arguments to assign global variables again.>
1012  
1013 It is also possible to create a loop with the C<assign()> method by passing the block name as the first argument (or all block names joined with a dot to create nested loops). The variables can be assigned only once and not as a hash reference but there is no need to use the C<block()> method.
1014  
1015 $tpl->assign( BLOCK, # assign to this block
1016 VARIABLE_1 => 'Block ...',
1017 VARIABLE_2 => 'Block ...',
1018 );
1019
1020 $tpl->assign( # assign global again
1021 VARIABLE_3 => 'Global ...'.
1022 );
1023  
1024 Blocks can even be used to create if-statements. Simply assign a variable with a true or false value. Based on that, the block is skipped or included in the output.
1025  
1026 $tpl->assign( SHOW_INFO => 1 ); # show block SHOW_INFO
1027 $tpl->assign( SHOW_LOGIN => 0 ); # skip block SHOW_LOGIN
1028  
1029 For a better control of the loop output, three special loop variables can be made available inside a loop: C<FIRST>, C<INNER> and C<LAST>. This variables are disabled by default (see L<OPTIONS|"Loop Vars"> section how to enable them).
1030  
1031 <!-- BEGIN LOOP -->
1032
1033
1034 <!-- BEGIN FIRST -->
1035 First loop pass
1036 <!-- END FIRST -->
1037
1038
1039 <!-- BEGIN INNER -->
1040 Neither first nor last
1041 <!-- END INNER -->
1042
1043
1044 <!-- BEGIN LAST -->
1045 Last loop pass
1046 <!-- END LAST -->
1047
1048
1049 <!-- END LOOP -->
1050  
1051  
1052 =head1 INCLUDES
1053  
1054 Includes are used to process and include the output of another template file directly into the current template in place of the include tag. All variables and blocks assigned to the current template are also available inside the included template.
1055  
1056 <!-- INCLUDE file.tpl -->
1057
1058 <!-- INCLUDE "file.tpl" -->
1059
1060 <!-- INCLUDE 'file.tpl' -->
1061  
1062 If the template can't be found under the specified file path (considering the root path), the path to the enclosing file is tried. See L<OPTIONS|"No Includes"> section how to disable includes or change the limit for recursive includes.
1063  
1064 It is possible to include template files defined by a variable when the option for including variables is enabled (it is disabled by default).
1065  
1066 <!-- INCLUDE VARIABLE -->
1067  
1068  
1069 =head1 ADVANCED
1070  
1071 Although it is possible to create loops and if statements with the block tag, sometimes the template syntax might get too confusing or not allow to write the wanted conditions in an easy way. For this reason if, unless, else and loop tags are available.
1072  
1073 <!-- IF VARIABLE -->
1074
1075 <!-- END VARIABLE -->
1076
1077
1078 <!-- UNLESS VARIABLE -->
1079
1080 <!-- END VARIABLE -->
1081
1082
1083 <!-- LOOP ARRAY -->
1084
1085 <!-- END ARRAY -->
1086
1087
1088 <!-- IF VARIABLE -->
1089
1090 <!-- ELSE VARIABLE -->
1091
1092 <!-- END VARIABLE -->
1093  
1094 The else tag can be used with all statements, even with loops. For an even cleaner template syntax, the else and the end tag can be written without the variable name.
1095  
1096 <!-- BEGIN ARRAY -->
1097
1098 <!-- END -->
1099
1100
1101 <!-- IF VARIABLE -->
1102
1103 <!-- ELSE -->
1104
1105 <!-- END -->
1106  
1107 The following syntax is also allowed but will not work with the block tag:
1108  
1109 <!-- IF VARIABLE -->
1110
1111 <!-- ELSE -->
1112
1113 <!-- END IF -->
1114
1115
1116 <!-- LOOP ARRAY -->
1117
1118 <!-- END LOOP -->
1119  
1120  
1121 =head1 METHODS
1122  
1123 =head2 new()
1124  
1125 Creates a new template object.
1126  
1127 $tpl = HTML::KTemplate->new();
1128
1129 $tpl = HTML::KTemplate->new('/path/to/templates');
1130
1131 $tpl = HTML::KTemplate->new(
1132 root => '/path/to/templates',
1133 cache => 0,
1134 strict => 0,
1135 no_includes => 0,
1136 max_includes => 15,
1137 loop_vars => 0,
1138 blind_cache => 0,
1139 include_vars => 0,
1140 parse_vars => 0,
1141 );
1142  
1143  
1144 =head2 assign()
1145  
1146 Assigns values for the variables used in the template.
1147  
1148 %hash = (
1149 VARIABLE => 'Value',
1150 );
1151
1152 $tpl->assign( %hash );
1153 $tpl->assign(\%hash );
1154 $tpl->assign( VARIABLE => 'Value' );
1155
1156 $tpl->assign( BLOCK,
1157 VARIABLE => 'Value',
1158 VARIABLE => 'Value',
1159 );
1160  
1161 =head2 block()
1162  
1163 See the description of L<BLOCKS|"BLOCKS">.
1164  
1165 $tpl->block('BLOCK_1');
1166
1167 $tpl->block('BLOCK_1','BLOCK_2');
1168 $tpl->block('BLOCK_1.BLOCK_2');
1169
1170 $tpl->block(); # leave block
1171  
1172 =head2 process()
1173  
1174 The C<process()> method is called to process the template files passed as arguments. It loads each template file, parses it and adds it to the template output. It is also possible to pass a reference to a scalar, array or file handle to initialize the template from memory. The use of the template output is determined by the C<print()> or the C<fetch()> method.
1175  
1176 $tpl->process('header.tpl', 'footer.tpl');
1177
1178 $tpl->process('header.tpl');
1179 $tpl->process('footer.tpl');
1180
1181 $tpl->process(\$scalar);
1182 $tpl->process(\@array);
1183 $tpl->process(\*FH);
1184  
1185 =head2 print()
1186  
1187 Prints the output data to C<STDOUT>. If a file handle reference is passed, it is used instead of the standard output.
1188  
1189 $tpl->print();
1190
1191 $tpl->print(\*FILE);
1192  
1193 =head2 fetch()
1194  
1195 Returns a scalar reference to the output data.
1196  
1197 $output_ref = $tpl->fetch();
1198
1199 print FILE $$output_ref;
1200  
1201 =head2 clear()
1202  
1203 Clears all variable values and other data being held in memory (except cache data).
1204  
1205 $tpl->clear();
1206  
1207 Equivalent to:
1208  
1209 $tpl->clear_vars();
1210 $tpl->clear_out();
1211  
1212 =head2 clear_vars()
1213  
1214 Clears all assigned variable values.
1215  
1216 $tpl->clear_vars();
1217  
1218 =head2 clear_out()
1219  
1220 Clears all output data created by C<process()>.
1221  
1222 $tpl->clear_out();
1223  
1224 =head2 clear_cache()
1225  
1226 Empties all cache data.
1227  
1228 $tpl->clear_cache();
1229  
1230  
1231 =head1 OPTIONS
1232  
1233 =head2 Variable Tag
1234  
1235 $HTML::KTemplate::VAR_START_TAG = '[%';
1236 $HTML::KTemplate::VAR_END_TAG = '%]';
1237  
1238 =head2 Block Tag
1239  
1240 $HTML::KTemplate::BLOCK_START_TAG = '<!--';
1241 $HTML::KTemplate::BLOCK_END_TAG = '-->';
1242  
1243 =head2 Include Tag
1244  
1245 $HTML::KTemplate::INCLUDE_START_TAG = '<!--';
1246 $HTML::KTemplate::INCLUDE_END_TAG = '-->';
1247  
1248 =head2 Root
1249  
1250 $HTML::KTemplate::ROOT = undef; # default
1251 $HTML::KTemplate::ROOT = '/path/to/templates';
1252
1253 $tpl = HTML::KTemplate->new( '/path/to/templates' );
1254 $tpl = HTML::KTemplate->new( root => '/path/to/templates' );
1255  
1256 =head2 No Includes
1257  
1258 Set this option to 1 to disable includes. The include tags will be skipped unless the strict option is set to 1.
1259  
1260 $tpl = HTML::KTemplate->new( no_includes => 0 ); # default
1261 $tpl = HTML::KTemplate->new( no_includes => 1 );
1262  
1263 =head2 Max Includes
1264  
1265 Allows to set the maximum depth that includes can reach. An error is raised when this depth is exceeded.
1266  
1267 $tpl = HTML::KTemplate->new( max_includes => 15 ); # default
1268  
1269 =head2 Include Vars
1270  
1271 Allows to include template files defined by a variable (see the description of L<INCLUDES|"INCLUDES"> for more information).
1272  
1273 $tpl = HTML::KTemplate->new( include_vars => 0 ); # default
1274 $tpl = HTML::KTemplate->new( include_vars => 1 );
1275  
1276 =head2 Cache
1277  
1278 Caching option for a persistent environment like mod_perl. Parsed templates will be cached in memory based on their file path and modification date. Use C<clear_cache()> to empty cache.
1279  
1280 $tpl = HTML::KTemplate->new( cache => 0 ); # default
1281 $tpl = HTML::KTemplate->new( cache => 1 );
1282  
1283 =head2 Blind Cache
1284  
1285 Behaves as the normal caching option but does not check the modification date to see if the template has changed. This might result in some speed improvement over normal caching.
1286  
1287 $tpl = HTML::KTemplate->new( blind_cache => 0 ); # default
1288 $tpl = HTML::KTemplate->new( blind_cache => 1 );
1289  
1290 =head2 Loop Vars
1291  
1292 Set this option to 1 to enable the loop variables C<FIRST>, C<INNER> and C<LAST>.
1293  
1294 $tpl = HTML::KTemplate->new( loop_vars => 0 ); # default
1295 $tpl = HTML::KTemplate->new( loop_vars => 1 );
1296  
1297 The default loop variables can be changed in the following way:
1298  
1299 $HTML::KTemplate::FIRST = { 'FIRST' => 1, 'first' => 1 };
1300 $HTML::KTemplate::INNER = { 'INNER' => 1, 'inner' => 1 };
1301 $HTML::KTemplate::LAST = { 'LAST' => 1, 'last' => 1 };
1302  
1303 =head2 Parse Vars
1304  
1305 Set this option to 1 to parse variables. That way all template variables inside of a variable will be replaced with their assigned values.
1306  
1307 $tpl = HTML::KTemplate->new( parse_vars => 0 ); # default
1308 $tpl = HTML::KTemplate->new( parse_vars => 1 );
1309  
1310  
1311 =head2 Strict
1312  
1313 Set this option to 1 to raise errors on not defined variables and include tags when disabled.
1314  
1315 $tpl = HTML::KTemplate->new( strict => 0 ); # default
1316 $tpl = HTML::KTemplate->new( strict => 1 );
1317  
1318 =head2 Chomp
1319  
1320 Removes the newline before and after a block tag.
1321  
1322 $HTML::KTemplate::CHOMP = 1; # default
1323 $HTML::KTemplate::CHOMP = 0;
1324  
1325  
1326 =head1 MAILING LIST
1327  
1328 If you want to get email when a new version of HTML::KTemplate is released, join the announcements mailing list:
1329  
1330 http://lists.sourceforge.net/lists/listinfo/html-ktemplate-announce
1331  
1332 A mailing list for discussing HTML::KTemplate is available at <html-ktemplate-users@lists.sourceforge.net>. To join, visit:
1333  
1334 http://lists.sourceforge.net/lists/listinfo/html-ktemplate-users
1335  
1336 You can also email me questions, comments, suggestions or bug reports directly to <kasper@repsak.de>.
1337  
1338  
1339 =head1 WEBSITE
1340  
1341 More information about HTML::KTemplate can be found at:
1342  
1343 http://html-ktemplate.sourceforge.net/
1344  
1345  
1346 =head1 COPYRIGHT
1347  
1348 Copyright (c) 2002-2003 Kasper Dziurdz. All rights reserved.
1349
1350 This module is free software; you can redistribute it and/or
1351 modify it under the same terms as Perl itself.
1352
1353 This program is distributed in the hope that it will be useful,
1354 but WITHOUT ANY WARRANTY; without even the implied warranty of
1355 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1356 Artistic License for more details.
1357  
1358 =head1 AUTHOR
1359  
1360 Kasper Dziurdz <kasper@repsak.de>
1361  
1362 =cut
1363  
↑ Summary