line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Prancer::Template::TemplateToolkit; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
2764
|
use strict; |
|
4
|
|
|
|
|
22
|
|
|
4
|
|
|
|
|
116
|
|
4
|
4
|
|
|
4
|
|
10
|
use warnings FATAL => 'all'; |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
88
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
10
|
use Carp; |
|
4
|
|
|
|
|
2
|
|
|
4
|
|
|
|
|
149
|
|
7
|
4
|
|
|
4
|
|
15
|
use Cwd (); |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
64
|
|
8
|
4
|
|
|
4
|
|
15
|
use Try::Tiny; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
181
|
|
9
|
4
|
|
|
4
|
|
11
|
use Prancer qw(logger); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
1685
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
0
|
|
|
0
|
0
|
|
my ($class, $config) = @_; |
13
|
0
|
|
|
|
|
|
my $self = bless({}, $class); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
try { |
17
|
0
|
|
|
0
|
|
|
require Template; |
18
|
|
|
|
|
|
|
} catch { |
19
|
0
|
0
|
|
0
|
|
|
my $error = (defined($_) ? $_ : "unknown"); |
20
|
0
|
|
|
|
|
|
logger->fatal("could not load Template: ${error}"); |
21
|
0
|
|
|
|
|
|
croak; |
22
|
0
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
die "no template path is configured\n" unless defined($config->{'template_dir'}); |
25
|
0
|
|
|
|
|
|
my $include_path = Cwd::realpath($config->{'template_dir'}); |
26
|
0
|
0
|
|
|
|
|
die "${include_path} does not exist\n" unless (-e $include_path); |
27
|
0
|
0
|
|
|
|
|
die "${include_path} is not readable\n" unless (-r $include_path); |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
|
my $cache_path = (defined($config->{'cache_dir'}) ? Cwd::realpath($config->{'cache_dir'}) : undef); |
30
|
0
|
0
|
|
|
|
|
if (defined($cache_path)) { |
31
|
0
|
0
|
|
|
|
|
die "${cache_path} does not exist\n" unless (-e $cache_path); |
32
|
0
|
0
|
|
|
|
|
die "${cache_path} is not readable\n" unless (-r $cache_path); |
33
|
0
|
0
|
|
|
|
|
die "${cache_path} is not writable\n" unless (-w $cache_path); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
0
|
|
|
|
my $engine = Template->new({ |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
37
|
|
|
|
|
|
|
'INCLUDE_PATH' => $include_path, |
38
|
|
|
|
|
|
|
'ANYCASE' => 1, |
39
|
|
|
|
|
|
|
'START_TAG' => $config->{'start_tag'} || '<%', |
40
|
|
|
|
|
|
|
'END_TAG' => $config->{'end_tag'} || '%>', |
41
|
|
|
|
|
|
|
'ENCODING' => $config->{'encoding'} || 'utf8', |
42
|
|
|
|
|
|
|
'PRE_PROCESS' => $config->{'pre_process'}, |
43
|
|
|
|
|
|
|
'POST_PROCESS' => $config->{'post_process'}, |
44
|
|
|
|
|
|
|
'CACHE_SIZE' => $config->{'cache_size'}, |
45
|
|
|
|
|
|
|
'COMPILE_EXT' => 'ttc', |
46
|
|
|
|
|
|
|
'COMPILE_DIR' => $cache_path, |
47
|
|
|
|
|
|
|
}); |
48
|
0
|
|
|
|
|
|
$self->{'_engine'} = $engine; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
logger->info("initialized template engine with templates from ${include_path}"); |
51
|
0
|
0
|
|
|
|
|
logger->info("templates will be cached to ${cache_path}") if ($cache_path); |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
return $self; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub render { |
57
|
0
|
|
|
0
|
0
|
|
my ($self, $input, $vars) = @_; |
58
|
0
|
|
|
|
|
|
my $output = undef; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
try { |
61
|
0
|
0
|
|
0
|
|
|
$self->{'_engine'}->process($input, $vars, \$output) or croak $self->{'_engine'}->error(); |
62
|
|
|
|
|
|
|
} catch { |
63
|
0
|
0
|
|
0
|
|
|
my $error = (defined($_) ? $_ : "unknown"); |
64
|
0
|
|
|
|
|
|
logger->error("could not render template: ${error}"); |
65
|
0
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
return defined($output) ? $output : ''; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 NAME |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Prancer::Template::TemplateToolkit |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 SYNOPSIS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This template engine plugin requires L<Template>. If this plugin is configured |
79
|
|
|
|
|
|
|
but L<Template> is not found your application will not start. To use this |
80
|
|
|
|
|
|
|
template engine plugin, set your configuration to something like this: |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
template: |
83
|
|
|
|
|
|
|
driver: Prancer::Template::TemplateToolkit |
84
|
|
|
|
|
|
|
options: |
85
|
|
|
|
|
|
|
template_dir: /srv/www/site/templates |
86
|
|
|
|
|
|
|
encoding: utf8 |
87
|
|
|
|
|
|
|
start_tag: "<%" |
88
|
|
|
|
|
|
|
end_tag: "%>" |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Then your templates should be placed under C</srv/www/site/templates> as |
91
|
|
|
|
|
|
|
configured. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 OPTIONS |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=over 4 |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item template_dir |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
B<REQUIRED> Sets the directory to look for templates. If this path is not |
100
|
|
|
|
|
|
|
configured then your application will not start. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item start_tag |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Sets the start tag for your templates. The default is C<E<lt>%>. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item end_tag |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Sets the end tag for your templates. The default is C<%E<gt>>. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item encoding |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Sets the encoding of your templates. The default is C<utf8>. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item pre_process |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Names a template to add to the top of all templates. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item post_process |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Names a template to be added to the bottom of all templates. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item cache_size |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Sets the number of templates to cache. The default is cache all of them if the |
125
|
|
|
|
|
|
|
cache is enabled. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item cache_dir |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Sets the directory to cache templates. The default is to not cache templates. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=back |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|