line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TemplatePerlish; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
10076
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
47
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
42
|
|
5
|
2
|
|
|
2
|
|
978
|
use English qw< -no_match_vars >; |
|
2
|
|
|
|
|
1111
|
|
|
2
|
|
|
|
|
10
|
|
6
|
|
|
|
|
|
|
{ our $VERSION = '0.001'; } |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
719
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
8
|
|
9
|
2
|
|
|
2
|
|
268
|
use Mojo::Util qw(encode md5_sum); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
87
|
|
10
|
2
|
|
|
2
|
|
885
|
use Template::Perlish; |
|
2
|
|
|
|
|
5229
|
|
|
2
|
|
|
|
|
8
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _resolve { |
13
|
12
|
|
|
12
|
|
14
|
my ($renderer, $options, $binmode) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
return (undef, inline => $options->{inline}) |
16
|
12
|
100
|
|
|
|
35
|
if defined $options->{inline}; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
6
|
my $name = $renderer->template_name($options); |
19
|
2
|
100
|
|
|
|
1039
|
if (defined(my $path = $renderer->template_path($options))) { |
20
|
1
|
|
|
|
|
42
|
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
|
|
|
|
34
|
open my $fh, '<', $path |
33
|
|
|
|
|
|
|
or die "Error opening '$path': $OS_ERROR"; |
34
|
1
|
|
|
|
|
9
|
binmode $fh, $binmode; |
35
|
1
|
|
|
|
|
40
|
local $/; |
36
|
1
|
|
|
|
|
22
|
return scalar <$fh>; |
37
|
|
|
|
|
|
|
} ## end sub _slurp |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub register { |
40
|
3
|
|
|
3
|
1
|
1845
|
my ($self, $app, $conf) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# this is how we're going to slurp templates |
43
|
3
|
|
50
|
|
|
18
|
my $binmode = delete($conf->{read_binmode}) || ':encoding(UTF-8)'; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# caching of templates can be disabled if needed |
46
|
3
|
50
|
|
|
|
6
|
my $cache_for = exists($conf->{cache}) ? $conf->{cache} : 1; |
47
|
3
|
50
|
33
|
|
|
20
|
$cache_for = {} if $cache_for && ref($cache_for) ne 'HASH'; |
48
|
|
|
|
|
|
|
|
49
|
3
|
|
100
|
|
|
10
|
my $name = $conf->{name} || 'tp'; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
100
|
|
|
15
|
my $tp = Template::Perlish->new($conf->{template_perlish} || {}); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$app->renderer->add_handler( |
54
|
|
|
|
|
|
|
$name => sub { |
55
|
12
|
|
|
12
|
|
23586
|
my ($renderer, $c, $output, $options) = @_; |
56
|
12
|
|
|
|
|
22
|
my $logger = $c->app()->log(); |
57
|
|
|
|
|
|
|
|
58
|
12
|
|
|
|
|
59
|
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
|
|
|
|
|
7
|
my $sub; |
68
|
12
|
50
|
|
|
|
26
|
if ($cache_for) { |
69
|
12
|
|
|
|
|
20
|
my $key = md5_sum(encode('UTF-8', $template)); |
70
|
12
|
|
66
|
|
|
292
|
$sub = $cache_for->{$key} ||= $tp->compile_as_sub($template); |
71
|
12
|
100
|
|
|
|
7131
|
$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
|
|
|
|
|
35
|
my $helpers = $renderer->helpers(); |
80
|
|
|
|
|
|
|
my %helpers_params = map { |
81
|
12
|
|
|
|
|
190
|
my $method = $helpers->{$_}; |
|
744
|
|
|
|
|
492
|
|
82
|
744
|
|
|
|
|
1150
|
$_ => sub { $c->$method(@_) }; |
|
2
|
|
|
|
|
387
|
|
83
|
|
|
|
|
|
|
} keys %$helpers; |
84
|
|
|
|
|
|
|
my %params = ( |
85
|
|
|
|
|
|
|
%helpers_params, |
86
|
12
|
|
|
|
|
104
|
%{$c->stash()}, |
|
12
|
|
|
|
|
30
|
|
87
|
|
|
|
|
|
|
self => $c, |
88
|
|
|
|
|
|
|
c => $c, |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
12
|
|
|
|
|
234
|
$logger->debug("Rendering $type template '$name'"); |
92
|
12
|
|
|
|
|
465
|
$$output = $sub->(\%params); |
93
|
|
|
|
|
|
|
|
94
|
12
|
|
|
|
|
6208
|
return 1; |
95
|
|
|
|
|
|
|
} |
96
|
3
|
|
|
|
|
81
|
); |
97
|
|
|
|
|
|
|
} ## end sub register |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
__END__ |