line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TemplatePerlish; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
11511
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
69
|
|
4
|
3
|
|
|
3
|
|
9
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
65
|
|
5
|
3
|
|
|
3
|
|
1325
|
use English qw< -no_match_vars >; |
|
3
|
|
|
|
|
2460
|
|
|
3
|
|
|
|
|
14
|
|
6
|
|
|
|
|
|
|
{ our $VERSION = '0.001001'; } |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1367
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
6494
|
|
|
3
|
|
|
|
|
16
|
|
9
|
3
|
|
|
3
|
|
1515
|
use Mojo::Util qw(encode md5_sum); |
|
3
|
|
|
|
|
30961
|
|
|
3
|
|
|
|
|
159
|
|
10
|
3
|
|
|
3
|
|
1360
|
use Template::Perlish; |
|
3
|
|
|
|
|
7579
|
|
|
3
|
|
|
|
|
12
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _resolve { |
13
|
12
|
|
|
12
|
|
13
|
my ($renderer, $options, $binmode) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
return (undef, inline => $options->{inline}) |
16
|
12
|
100
|
|
|
|
36
|
if defined $options->{inline}; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
6
|
my $name = $renderer->template_name($options); |
19
|
2
|
100
|
|
|
|
1043
|
if (defined(my $path = $renderer->template_path($options))) { |
20
|
1
|
|
|
|
|
43
|
return ($name, path => _slurp($path, $binmode)); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
1
|
50
|
|
|
|
70
|
if (defined(my $tmpl = $renderer->get_data_template($options))) { |
24
|
1
|
|
|
|
|
21
|
return ($name, data => $tmpl); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
0
|
return $name; |
28
|
|
|
|
|
|
|
} ## end sub _resolve |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _slurp { |
31
|
1
|
|
|
1
|
|
2
|
my ($path, $binmode) = @_; |
32
|
1
|
50
|
|
|
|
32
|
open my $fh, '<', $path |
33
|
|
|
|
|
|
|
or die "Error opening '$path': $OS_ERROR"; |
34
|
1
|
|
|
|
|
7
|
binmode $fh, $binmode; |
35
|
1
|
|
|
|
|
38
|
local $/; |
36
|
1
|
|
|
|
|
20
|
return scalar <$fh>; |
37
|
|
|
|
|
|
|
} ## end sub _slurp |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub register { |
40
|
3
|
|
|
3
|
1
|
1831
|
my ($self, $app, $conf) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# this is how we're going to slurp templates |
43
|
3
|
|
50
|
|
|
16
|
my $binmode = delete($conf->{read_binmode}) || ':encoding(UTF-8)'; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# caching of templates can be disabled if needed |
46
|
3
|
50
|
|
|
|
11
|
my $cache_for = exists($conf->{cache}) ? $conf->{cache} : 1; |
47
|
3
|
50
|
33
|
|
|
17
|
$cache_for = {} if $cache_for && ref($cache_for) ne 'HASH'; |
48
|
|
|
|
|
|
|
|
49
|
3
|
|
100
|
|
|
12
|
my $name = $conf->{name} || 'tp'; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
100
|
|
|
16
|
my $tp = Template::Perlish->new($conf->{template_perlish} || {}); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$app->renderer->add_handler( |
54
|
|
|
|
|
|
|
$name => sub { |
55
|
12
|
|
|
12
|
|
23253
|
my ($renderer, $c, $output, $options) = @_; |
56
|
12
|
|
|
|
|
22
|
my $logger = $c->app()->log(); |
57
|
|
|
|
|
|
|
|
58
|
12
|
|
|
|
|
58
|
my ($name, $type, $template) = |
59
|
|
|
|
|
|
|
_resolve($renderer, $options, $binmode); |
60
|
|
|
|
|
|
|
|
61
|
12
|
50
|
|
|
|
46
|
if (!defined $type) { |
62
|
0
|
0
|
|
|
|
0
|
$name = '(unknown name)' unless defined $name; |
63
|
0
|
|
|
|
|
0
|
$logger->debug("Template '$name' not found"); |
64
|
0
|
|
|
|
|
0
|
return undef; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
12
|
|
|
|
|
8
|
my $sub; |
68
|
12
|
50
|
|
|
|
22
|
if ($cache_for) { |
69
|
12
|
|
|
|
|
22
|
my $key = md5_sum(encode('UTF-8', $template)); |
70
|
12
|
|
66
|
|
|
281
|
$sub = $cache_for->{$key} ||= $tp->compile_as_sub($template); |
71
|
12
|
100
|
|
|
|
6798
|
$name = $key unless defined $name; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
else { |
74
|
0
|
|
|
|
|
0
|
$sub = $tp->compile_as_sub($template); |
75
|
0
|
0
|
|
|
|
0
|
$name = md5_sum(encode('UTF-8', $template)) |
76
|
|
|
|
|
|
|
unless defined $name; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
12
|
|
|
|
|
32
|
my $helpers = $renderer->helpers(); |
80
|
|
|
|
|
|
|
my %helpers_params = map { |
81
|
12
|
|
|
|
|
146
|
my $method = $helpers->{$_}; |
|
744
|
|
|
|
|
532
|
|
82
|
744
|
|
|
|
|
1338
|
$_ => sub { $c->$method(@_) }; |
|
2
|
|
|
|
|
522
|
|
83
|
|
|
|
|
|
|
} keys %$helpers; |
84
|
|
|
|
|
|
|
my %params = ( |
85
|
|
|
|
|
|
|
%helpers_params, |
86
|
12
|
|
|
|
|
108
|
%{$c->stash()}, |
|
12
|
|
|
|
|
35
|
|
87
|
|
|
|
|
|
|
self => $c, |
88
|
|
|
|
|
|
|
c => $c, |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
12
|
|
|
|
|
249
|
$logger->debug("Rendering $type template '$name'"); |
92
|
12
|
|
|
|
|
476
|
$$output = $sub->(\%params); |
93
|
|
|
|
|
|
|
|
94
|
12
|
|
|
|
|
6557
|
return 1; |
95
|
|
|
|
|
|
|
} |
96
|
3
|
|
|
|
|
69
|
); |
97
|
|
|
|
|
|
|
} ## end sub register |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
__END__ |