line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Provider::Mojo; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
249206
|
use strict; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
88
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
79
|
|
5
|
3
|
|
|
3
|
|
413
|
use parent 'Template::Provider'; |
|
3
|
|
|
|
|
258
|
|
|
3
|
|
|
|
|
22
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
1610
|
use Class::Method::Modifiers (); |
|
3
|
|
|
|
|
4301
|
|
|
3
|
|
|
|
|
106
|
|
8
|
3
|
|
|
3
|
|
21
|
use Mojo::Util; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
127
|
|
9
|
3
|
|
|
3
|
|
458
|
use Mojolicious::Renderer; |
|
3
|
|
|
|
|
58718
|
|
|
3
|
|
|
|
|
41
|
|
10
|
3
|
|
|
3
|
|
97
|
use Scalar::Util; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
108
|
|
11
|
3
|
|
|
3
|
|
19
|
use Template::Constants; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1867
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.006'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Class::Method::Modifiers::after '_init' => sub { |
16
|
|
|
|
|
|
|
my ($self, $params) = @_; |
17
|
|
|
|
|
|
|
Scalar::Util::weaken($self->{MOJO_RENDERER} = delete $params->{MOJO_RENDERER}); |
18
|
|
|
|
|
|
|
$self->{ENCODING} //= $self->_mojo_renderer->encoding; |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub fetch { |
22
|
45
|
|
|
45
|
1
|
41417
|
my ($self, $name) = @_; |
23
|
45
|
|
|
|
|
77
|
my ($data, $error); |
24
|
|
|
|
|
|
|
|
25
|
45
|
100
|
|
|
|
102
|
if (ref $name) { |
26
|
|
|
|
|
|
|
# $name can be a reference to a scalar, GLOB or file handle |
27
|
20
|
|
|
|
|
131
|
($data, $error) = $self->_load($name); |
28
|
20
|
50
|
|
|
|
483
|
($data, $error) = $self->_compile($data) unless $error; |
29
|
20
|
50
|
|
|
|
97148
|
$data = $data->{data} unless $error; |
30
|
|
|
|
|
|
|
} else { |
31
|
|
|
|
|
|
|
# Use renderer to find template |
32
|
25
|
|
|
|
|
54
|
my $renderer = $self->_mojo_renderer; |
33
|
25
|
|
|
|
|
56
|
my $options = _template_options($renderer, $name); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Try template |
36
|
25
|
100
|
|
|
|
76
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
50
|
|
|
|
|
|
37
|
11
|
|
|
|
|
1093
|
($data, $error) = $self->_fetch($path); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Try DATA section |
41
|
|
|
|
|
|
|
elsif (defined(my $d = $renderer->get_data_template($options))) { |
42
|
14
|
|
|
|
|
4784
|
($data, $error) = $self->_load(\$d); |
43
|
14
|
50
|
|
|
|
297
|
($data, $error) = $self->_compile($data) unless $error; |
44
|
14
|
50
|
|
|
|
11791
|
$data = $data->{data} unless $error; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# No template |
48
|
0
|
|
|
|
|
0
|
else { ($data, $error) = (undef, Template::Constants::STATUS_DECLINED) } |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
45
|
|
|
|
|
14440
|
return ($data, $error); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub load { |
55
|
9
|
|
|
9
|
1
|
1752
|
my ($self, $name) = @_; |
56
|
9
|
|
|
|
|
15
|
my ($data, $error); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Use renderer to find template |
59
|
9
|
|
|
|
|
19
|
my $renderer = $self->_mojo_renderer; |
60
|
9
|
|
|
|
|
18
|
my $options = _template_options($renderer, $name); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Try template |
63
|
9
|
100
|
|
|
|
27
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
50
|
|
|
|
|
|
64
|
3
|
|
|
|
|
277
|
($data, $error) = $self->_template_content($path); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Try DATA section |
68
|
|
|
|
|
|
|
elsif (defined(my $d = $renderer->get_data_template($options))) { |
69
|
|
|
|
|
|
|
# Content is expected to be encoded |
70
|
6
|
50
|
33
|
|
|
773
|
$d = Mojo::Util::encode $self->{ENCODING}, $d if $self->{UNICODE} and $self->{ENCODING}; |
71
|
6
|
|
|
|
|
59
|
($data, $error) = ($d, undef); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# No template |
75
|
0
|
|
|
|
|
0
|
else { return (undef, Template::Constants::STATUS_DECLINED) } |
76
|
|
|
|
|
|
|
|
77
|
9
|
50
|
|
|
|
353
|
if ($error) { |
78
|
0
|
0
|
|
|
|
0
|
return $self->{TOLERANT} ? (undef, Template::Constants::STATUS_DECLINED) |
79
|
|
|
|
|
|
|
: ($error, Template::Constants::STATUS_ERROR); |
80
|
|
|
|
|
|
|
} else { |
81
|
9
|
|
|
|
|
39
|
return ($data, Template::Constants::STATUS_OK); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
38
|
|
33
|
38
|
|
137
|
sub _mojo_renderer { shift->{MOJO_RENDERER} //= Mojolicious::Renderer->new } |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Split template name back into options |
88
|
|
|
|
|
|
|
sub _template_options { |
89
|
34
|
|
|
34
|
|
58
|
my ($renderer, $name) = @_; |
90
|
34
|
|
|
|
|
66
|
my $options = {}; |
91
|
34
|
50
|
|
|
|
180
|
if ($name =~ m/^(.+)\.(.+)\.(.+)\z/) { |
|
|
0
|
|
|
|
|
|
92
|
34
|
|
|
|
|
96
|
$options->{template} = $1; |
93
|
34
|
|
|
|
|
82
|
$options->{format} = $2; |
94
|
34
|
|
|
|
|
79
|
$options->{handler} = $3; |
95
|
|
|
|
|
|
|
} elsif ($name =~ m/^(.+)\.(.+)\z/) { |
96
|
0
|
|
|
|
|
0
|
$options->{template} = $1; |
97
|
0
|
|
|
|
|
0
|
$options->{format} = $2; |
98
|
|
|
|
|
|
|
} else { |
99
|
0
|
|
|
|
|
0
|
$options->{template} = $name; |
100
|
0
|
|
|
|
|
0
|
$options->{format} = $renderer->default_format; |
101
|
0
|
|
|
|
|
0
|
$options->{handler} = $renderer->default_handler; |
102
|
|
|
|
|
|
|
} |
103
|
34
|
|
|
|
|
65
|
return $options; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 NAME |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Template::Provider::Mojo - Use Mojolicious to provide templates |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SYNOPSIS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $app = Mojolicious->new; |
115
|
|
|
|
|
|
|
$provider = Template::Provider::Mojo->new({MOJO_RENDERER => $app->renderer}); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
($template, $error) = $provider->fetch($name); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 DESCRIPTION |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
L is a L subclass that uses a |
122
|
|
|
|
|
|
|
L instance to resolve template names. This means that |
123
|
|
|
|
|
|
|
L will be searched for file-based templates, and |
124
|
|
|
|
|
|
|
L will be searched for DATA templates. The |
125
|
|
|
|
|
|
|
C configuration setting will be initialized to |
126
|
|
|
|
|
|
|
L if unset. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 METHODS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L inherits all methods from L and |
131
|
|
|
|
|
|
|
implements the following new ones. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 fetch |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Returns a compiled template for the name specified. See L |
136
|
|
|
|
|
|
|
for usage details. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 load |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Loads a template without parsing or compiling it. This is used by the |
141
|
|
|
|
|
|
|
L directive. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 BUGS |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Report any issues on the public bugtracker. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 AUTHOR |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Dan Book |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This software is Copyright (c) 2015 by Dan Book. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This is free software, licensed under: |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 SEE ALSO |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
L, L, L |