| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Kelp::Template; |
|
2
|
|
|
|
|
|
|
|
|
3
|
39
|
|
|
39
|
|
847
|
use Kelp::Base; |
|
|
39
|
|
|
|
|
103
|
|
|
|
39
|
|
|
|
|
2240
|
|
|
4
|
39
|
|
|
39
|
|
21783
|
use Template::Tiny; |
|
|
39
|
|
|
|
|
56843
|
|
|
|
39
|
|
|
|
|
1549
|
|
|
5
|
39
|
|
|
39
|
|
1270
|
use Path::Tiny; |
|
|
39
|
|
|
|
|
18369
|
|
|
|
39
|
|
|
|
|
2541
|
|
|
6
|
39
|
|
|
39
|
|
260
|
use Carp; |
|
|
39
|
|
|
|
|
79
|
|
|
|
39
|
|
|
|
|
26860
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
attr paths => sub { [] }; |
|
9
|
|
|
|
|
|
|
attr encoding => 'UTF-8'; |
|
10
|
|
|
|
|
|
|
attr tt => sub { Template::Tiny->new }; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub process |
|
13
|
|
|
|
|
|
|
{ |
|
14
|
94
|
|
|
94
|
1
|
419
|
my ($self, $template, $vars) = @_; |
|
15
|
|
|
|
|
|
|
|
|
16
|
94
|
|
|
|
|
196
|
my $ref = ref $template; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# A GLOB or an IO object will be read and returned as a SCALAR template |
|
19
|
|
|
|
|
|
|
# No reference means a file name |
|
20
|
94
|
100
|
100
|
|
|
373
|
if (!$ref) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
21
|
73
|
|
|
|
|
212
|
$template = $self->_read_file($self->find_template($template)); |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
elsif ($ref =~ /^IO/ || $ref eq 'GLOB') { |
|
24
|
5
|
|
|
|
|
12
|
$template = $self->_read_file($template); |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
elsif ($ref ne 'SCALAR') { |
|
27
|
0
|
|
|
|
|
0
|
croak "Template reference must be SCALAR, GLOB or an IO object"; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
70
|
|
|
|
|
141
|
my $output; |
|
31
|
70
|
|
|
|
|
295
|
$self->tt->process($template, $vars, \$output); |
|
32
|
70
|
|
|
|
|
44717
|
return $output; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub find_template |
|
36
|
|
|
|
|
|
|
{ |
|
37
|
73
|
|
|
73
|
0
|
139
|
my ($self, $name) = @_; |
|
38
|
|
|
|
|
|
|
|
|
39
|
73
|
|
|
|
|
100
|
my $file; |
|
40
|
73
|
|
|
|
|
118
|
for my $p ('.', @{$self->paths}) { |
|
|
73
|
|
|
|
|
211
|
|
|
41
|
143
|
|
|
|
|
318
|
$file = "$p/$name"; |
|
42
|
143
|
100
|
|
|
|
4252
|
return $file if -e $file; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
24
|
|
|
|
|
80
|
return undef; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _read_file |
|
49
|
|
|
|
|
|
|
{ |
|
50
|
78
|
|
|
78
|
|
181
|
my ($self, $file) = @_; |
|
51
|
78
|
|
|
|
|
99
|
my $text; |
|
52
|
|
|
|
|
|
|
|
|
53
|
78
|
100
|
|
|
|
208
|
if (ref $file) { |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# read the entire file |
|
56
|
5
|
|
|
|
|
37
|
local $/ = undef; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# make sure to properly rewind the handle after we read from it |
|
59
|
5
|
|
|
|
|
13
|
my $pos = tell $file; |
|
60
|
5
|
|
|
|
|
120
|
$text = readline $file; |
|
61
|
5
|
|
|
|
|
44
|
seek $file, $pos, 0; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
else { |
|
64
|
73
|
|
|
|
|
322
|
$text = path($file)->slurp( |
|
65
|
|
|
|
|
|
|
{binmode => ':encoding(' . $self->encoding . ')'} |
|
66
|
|
|
|
|
|
|
); |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
54
|
|
|
|
|
25493
|
return \$text; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
__END__ |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=pod |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 NAME |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Kelp::Template - A very minimal template rendering engine for Kelp |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $t = Kelp::Template->new; |
|
85
|
|
|
|
|
|
|
say $t->process('file.tt', { bar => 'foo' }); |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This module provides basic template rendering using L<Template::Tiny>. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 paths |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
An arrayref of paths to use when looking for template files. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 encoding |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Specifies the text encoding of the template files. The default value is C<UTF-8>. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 METHODS |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 process( $template, \%vars ) |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Processes a template and returns the parsed text. The template may be a file name, |
|
106
|
|
|
|
|
|
|
a reference to a text, a GLOB or an IO object. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
say $t->process(\"Hello [% who %]", { who => 'you' }); |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
|
111
|
|
|
|
|
|
|
|