| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Template::Provider::Mojo; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
227671
|
use strict; |
|
|
3
|
|
|
|
|
13
|
|
|
|
3
|
|
|
|
|
88
|
|
|
4
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
65
|
|
|
5
|
3
|
|
|
3
|
|
346
|
use parent 'Template::Provider'; |
|
|
3
|
|
|
|
|
212
|
|
|
|
3
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
1373
|
use Class::Method::Modifiers (); |
|
|
3
|
|
|
|
|
3636
|
|
|
|
3
|
|
|
|
|
54
|
|
|
8
|
3
|
|
|
3
|
|
17
|
use Mojo::Util; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
96
|
|
|
9
|
3
|
|
|
3
|
|
404
|
use Mojolicious::Renderer; |
|
|
3
|
|
|
|
|
44421
|
|
|
|
3
|
|
|
|
|
28
|
|
|
10
|
3
|
|
|
3
|
|
109
|
use Scalar::Util; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
93
|
|
|
11
|
3
|
|
|
3
|
|
15
|
use Template::Constants; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
1919
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.004'; |
|
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
|
37702
|
my ($self, $name) = @_; |
|
23
|
45
|
|
|
|
|
70
|
my ($data, $error); |
|
24
|
|
|
|
|
|
|
|
|
25
|
45
|
100
|
|
|
|
93
|
if (ref $name) { |
|
26
|
|
|
|
|
|
|
# $name can be a reference to a scalar, GLOB or file handle |
|
27
|
20
|
|
|
|
|
84
|
($data, $error) = $self->_load($name); |
|
28
|
20
|
50
|
|
|
|
372
|
($data, $error) = $self->_compile($data) unless $error; |
|
29
|
20
|
50
|
|
|
|
82894
|
$data = $data->{data} unless $error; |
|
30
|
|
|
|
|
|
|
} else { |
|
31
|
|
|
|
|
|
|
# Use renderer to find template |
|
32
|
25
|
|
|
|
|
51
|
my $renderer = $self->_mojo_renderer; |
|
33
|
25
|
|
|
|
|
46
|
my $options = _template_options($renderer, $name); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Try template |
|
36
|
25
|
100
|
|
|
|
72
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
|
50
|
|
|
|
|
|
|
37
|
11
|
|
|
|
|
895
|
($data, $error) = $self->_fetch($path); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Try DATA section |
|
41
|
|
|
|
|
|
|
elsif (defined(my $d = $renderer->get_data_template($options))) { |
|
42
|
14
|
|
|
|
|
3463
|
($data, $error) = $self->_load(\$d); |
|
43
|
14
|
50
|
|
|
|
251
|
($data, $error) = $self->_compile($data) unless $error; |
|
44
|
14
|
50
|
|
|
|
11172
|
$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
|
|
|
|
|
12516
|
return ($data, $error); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub load { |
|
55
|
9
|
|
|
9
|
1
|
1684
|
my ($self, $name) = @_; |
|
56
|
9
|
|
|
|
|
15
|
my ($data, $error); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Use renderer to find template |
|
59
|
9
|
|
|
|
|
17
|
my $renderer = $self->_mojo_renderer; |
|
60
|
9
|
|
|
|
|
17
|
my $options = _template_options($renderer, $name); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Try template |
|
63
|
9
|
100
|
|
|
|
24
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
|
50
|
|
|
|
|
|
|
64
|
3
|
|
|
|
|
237
|
($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
|
|
|
650
|
$d = Mojo::Util::encode $self->{ENCODING}, $d if $self->{UNICODE} and $self->{ENCODING}; |
|
71
|
6
|
|
|
|
|
52
|
($data, $error) = ($d, undef); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# No template |
|
75
|
0
|
|
|
|
|
0
|
else { return (undef, Template::Constants::STATUS_DECLINED) } |
|
76
|
|
|
|
|
|
|
|
|
77
|
9
|
50
|
|
|
|
307
|
if ($error) { |
|
78
|
0
|
0
|
|
|
|
0
|
return $self->{TOLERANT} ? (undef, Template::Constants::STATUS_DECLINED) |
|
79
|
|
|
|
|
|
|
: ($error, Template::Constants::STATUS_ERROR); |
|
80
|
|
|
|
|
|
|
} else { |
|
81
|
9
|
|
|
|
|
38
|
return ($data, Template::Constants::STATUS_OK); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
38
|
|
33
|
38
|
|
124
|
sub _mojo_renderer { shift->{MOJO_RENDERER} //= Mojolicious::Renderer->new } |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Split template name back into options |
|
88
|
|
|
|
|
|
|
sub _template_options { |
|
89
|
34
|
|
|
34
|
|
59
|
my ($renderer, $name) = @_; |
|
90
|
34
|
|
|
|
|
45
|
my $options = {}; |
|
91
|
34
|
50
|
|
|
|
164
|
if ($name =~ m/^(.+)\.(.+)\.(.+)\z/) { |
|
|
|
0
|
|
|
|
|
|
|
92
|
34
|
|
|
|
|
94
|
$options->{template} = $1; |
|
93
|
34
|
|
|
|
|
64
|
$options->{format} = $2; |
|
94
|
34
|
|
|
|
|
66
|
$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
|
|
|
|
|
57
|
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 |