line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TemplatePerlish; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
12173
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
71
|
|
4
|
3
|
|
|
3
|
|
8
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
65
|
|
5
|
3
|
|
|
3
|
|
1348
|
use English qw< -no_match_vars >; |
|
3
|
|
|
|
|
2469
|
|
|
3
|
|
|
|
|
13
|
|
6
|
|
|
|
|
|
|
{ our $VERSION = '0.002'; } |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1389
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
6917
|
|
|
3
|
|
|
|
|
28
|
|
9
|
3
|
|
|
3
|
|
1496
|
use Mojo::Util qw(encode md5_sum); |
|
3
|
|
|
|
|
30920
|
|
|
3
|
|
|
|
|
163
|
|
10
|
3
|
|
|
3
|
|
1308
|
use Template::Perlish; |
|
3
|
|
|
|
|
7643
|
|
|
3
|
|
|
|
|
12
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _resolve { |
13
|
12
|
|
|
12
|
|
14
|
my ($renderer, $options, $binmode) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
return (undef, inline => $options->{inline}) |
16
|
12
|
100
|
|
|
|
42
|
if defined $options->{inline}; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
6
|
my $name = $renderer->template_name($options); |
19
|
2
|
100
|
|
|
|
1051
|
if (defined(my $path = $renderer->template_path($options))) { |
20
|
1
|
|
|
|
|
41
|
return ($name, path => _slurp($path, $binmode)); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
1
|
50
|
|
|
|
73
|
if (defined(my $tmpl = $renderer->get_data_template($options))) { |
24
|
1
|
|
|
|
|
20
|
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
|
|
|
|
28
|
open my $fh, '<', $path |
33
|
|
|
|
|
|
|
or die "Error opening '$path': $OS_ERROR"; |
34
|
1
|
|
|
|
|
7
|
binmode $fh, $binmode; |
35
|
1
|
|
|
|
|
35
|
local $/; |
36
|
1
|
|
|
|
|
23
|
return scalar <$fh>; |
37
|
|
|
|
|
|
|
} ## end sub _slurp |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub register { |
40
|
3
|
|
|
3
|
1
|
2000
|
my ($self, $app, $conf) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# this is how we're going to slurp templates |
43
|
3
|
|
50
|
|
|
19
|
my $binmode = delete($conf->{read_binmode}) || ':encoding(UTF-8)'; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# caching of templates can be disabled if needed |
46
|
3
|
50
|
|
|
|
12
|
my $cache_for = exists($conf->{cache}) ? $conf->{cache} : 1; |
47
|
3
|
50
|
33
|
|
|
21
|
$cache_for = {} if $cache_for && ref($cache_for) ne 'HASH'; |
48
|
|
|
|
|
|
|
|
49
|
3
|
|
100
|
|
|
11
|
my $name = $conf->{name} || 'tp'; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
100
|
|
|
19
|
my $tp = Template::Perlish->new($conf->{template_perlish} || {}); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$app->renderer->add_handler( |
54
|
|
|
|
|
|
|
$name => sub { |
55
|
12
|
|
|
12
|
|
23235
|
my ($renderer, $c, $output, $options) = @_; |
56
|
12
|
|
|
|
|
21
|
my $logger = $c->app()->log(); |
57
|
|
|
|
|
|
|
|
58
|
12
|
|
|
|
|
65
|
my ($name, $type, $template) = |
59
|
|
|
|
|
|
|
_resolve($renderer, $options, $binmode); |
60
|
|
|
|
|
|
|
|
61
|
12
|
50
|
|
|
|
44
|
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
|
|
|
|
|
13
|
my $sub; |
68
|
12
|
50
|
|
|
|
22
|
if ($cache_for) { |
69
|
12
|
|
|
|
|
26
|
my $key = md5_sum(encode('UTF-8', $template)); |
70
|
12
|
|
66
|
|
|
330
|
$sub = $cache_for->{$key} ||= $tp->compile_as_sub($template); |
71
|
12
|
100
|
|
|
|
7085
|
$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
|
|
|
|
|
36
|
my $helpers = $renderer->helpers(); |
80
|
|
|
|
|
|
|
my %helpers_params = map { |
81
|
12
|
|
|
|
|
154
|
my $method = $helpers->{$_}; |
|
744
|
|
|
|
|
521
|
|
82
|
744
|
|
|
|
|
1209
|
$_ => sub { $c->$method(@_) }; |
|
2
|
|
|
|
|
399
|
|
83
|
|
|
|
|
|
|
} keys %$helpers; |
84
|
|
|
|
|
|
|
my %params = ( |
85
|
|
|
|
|
|
|
%helpers_params, |
86
|
12
|
|
|
|
|
128
|
%{$c->stash()}, |
|
12
|
|
|
|
|
36
|
|
87
|
|
|
|
|
|
|
self => $c, |
88
|
|
|
|
|
|
|
c => $c, |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
12
|
|
|
|
|
247
|
$logger->debug("Rendering $type template '$name'"); |
92
|
12
|
|
|
|
|
510
|
$$output = $sub->(\%params); |
93
|
|
|
|
|
|
|
|
94
|
12
|
|
|
|
|
6398
|
return 1; |
95
|
|
|
|
|
|
|
} |
96
|
3
|
|
|
|
|
78
|
); |
97
|
|
|
|
|
|
|
} ## end sub register |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
__END__ |