line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Kelp::Template; |
2
|
|
|
|
|
|
|
|
3
|
30
|
|
|
30
|
|
703
|
use Kelp::Base; |
|
30
|
|
|
|
|
65
|
|
|
30
|
|
|
|
|
171
|
|
4
|
30
|
|
|
30
|
|
17355
|
use Template::Tiny; |
|
30
|
|
|
|
|
41881
|
|
|
30
|
|
|
|
|
1076
|
|
5
|
30
|
|
|
30
|
|
1174
|
use Path::Tiny; |
|
30
|
|
|
|
|
13769
|
|
|
30
|
|
|
|
|
11890
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
attr paths => sub { [] }; |
8
|
|
|
|
|
|
|
attr encoding => 'utf8'; |
9
|
|
|
|
|
|
|
attr tt => sub { Template::Tiny->new }; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub process { |
12
|
44
|
|
|
44
|
1
|
376
|
my ( $self, $template, $vars ) = @_; |
13
|
|
|
|
|
|
|
|
14
|
44
|
|
|
|
|
91
|
my $ref = ref $template; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# A GLOB or an IO object will be read and returned as a SCALAR template |
17
|
|
|
|
|
|
|
# No reference means a file name |
18
|
44
|
100
|
100
|
|
|
378
|
if ( $ref =~ /^IO/ || $ref eq 'GLOB' || !$ref ) { |
|
|
50
|
100
|
|
|
|
|
19
|
38
|
100
|
|
|
|
120
|
if ( !$ref ) { |
20
|
36
|
|
|
|
|
82
|
for my $p ( '.', @{ $self->paths } ) { |
|
36
|
|
|
|
|
126
|
|
21
|
69
|
100
|
|
|
|
1532
|
if ( -e ( my $fullpath = "$p/$template" ) ) { |
22
|
21
|
|
|
|
|
76
|
$template = $fullpath; |
23
|
21
|
|
|
|
|
55
|
last; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
38
|
|
|
|
|
176
|
$template = $self->_read_file($template); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
elsif ( $ref ne 'SCALAR' ) { |
30
|
0
|
|
|
|
|
0
|
die "Template reference must be SCALAR, GLOB or an IO object"; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
29
|
|
|
|
|
66
|
my $output; |
34
|
29
|
|
|
|
|
126
|
$self->tt->process( $template, $vars, \$output ); |
35
|
29
|
|
|
|
|
7623
|
return $output; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _read_file { |
39
|
38
|
|
|
38
|
|
93
|
my ( $self, $file ) = @_; |
40
|
|
|
|
|
|
|
|
41
|
38
|
|
|
|
|
198
|
local $/ = undef; |
42
|
38
|
100
|
|
|
|
314
|
my $text = |
43
|
|
|
|
|
|
|
ref $file |
44
|
|
|
|
|
|
|
? <$file> |
45
|
|
|
|
|
|
|
: path($file)->slurp({ binmode => ':encoding(' . $self->encoding . ')' }) |
46
|
|
|
|
|
|
|
; |
47
|
|
|
|
|
|
|
|
48
|
23
|
|
|
|
|
32259
|
return \$text; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
__END__ |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=pod |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 NAME |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Kelp::Template - A very minimal template rendering engine for Kelp |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $t = Kelp::Template->new; |
64
|
|
|
|
|
|
|
say $t->process('file.tt', { bar => 'foo' }); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
This module provides basic template rendering using L<Template::Tiny>. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 paths |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
An arrayref of paths to use when looking for template files. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 encoding |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Specifies the text encoding of the template files. The default value is C<utf8>. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 METHODS |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 process( $template, \%vars ) |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Processes a template and returns the parsed text. The template may be a file name, |
85
|
|
|
|
|
|
|
a reference to a text, a GLOB or an IO object. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
say $t->process(\"Hello [% who %]", { who => 'you' }); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |