line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Provider::Mojo; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
277677
|
use strict; |
|
3
|
|
|
|
|
17
|
|
|
3
|
|
|
|
|
91
|
|
4
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
102
|
|
5
|
3
|
|
|
3
|
|
407
|
use parent 'Template::Provider'; |
|
3
|
|
|
|
|
259
|
|
|
3
|
|
|
|
|
21
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
1625
|
use Class::Method::Modifiers (); |
|
3
|
|
|
|
|
4519
|
|
|
3
|
|
|
|
|
67
|
|
8
|
3
|
|
|
3
|
|
20
|
use Mojo::Util; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
121
|
|
9
|
3
|
|
|
3
|
|
461
|
use Mojolicious::Renderer; |
|
3
|
|
|
|
|
54096
|
|
|
3
|
|
|
|
|
39
|
|
10
|
3
|
|
|
3
|
|
111
|
use Scalar::Util; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
103
|
|
11
|
3
|
|
|
3
|
|
19
|
use Template::Constants; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2041
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.005'; |
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
|
46315
|
my ($self, $name) = @_; |
23
|
45
|
|
|
|
|
93
|
my ($data, $error); |
24
|
|
|
|
|
|
|
|
25
|
45
|
100
|
|
|
|
120
|
if (ref $name) { |
26
|
|
|
|
|
|
|
# $name can be a reference to a scalar, GLOB or file handle |
27
|
20
|
|
|
|
|
108
|
($data, $error) = $self->_load($name); |
28
|
20
|
50
|
|
|
|
490
|
($data, $error) = $self->_compile($data) unless $error; |
29
|
20
|
50
|
|
|
|
102536
|
$data = $data->{data} unless $error; |
30
|
|
|
|
|
|
|
} else { |
31
|
|
|
|
|
|
|
# Use renderer to find template |
32
|
25
|
|
|
|
|
64
|
my $renderer = $self->_mojo_renderer; |
33
|
25
|
|
|
|
|
65
|
my $options = _template_options($renderer, $name); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Try template |
36
|
25
|
100
|
|
|
|
85
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
50
|
|
|
|
|
|
37
|
11
|
|
|
|
|
1156
|
($data, $error) = $self->_fetch($path); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Try DATA section |
41
|
|
|
|
|
|
|
elsif (defined(my $d = $renderer->get_data_template($options))) { |
42
|
14
|
|
|
|
|
4405
|
($data, $error) = $self->_load(\$d); |
43
|
14
|
50
|
|
|
|
302
|
($data, $error) = $self->_compile($data) unless $error; |
44
|
14
|
50
|
|
|
|
13491
|
$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
|
|
|
|
|
15630
|
return ($data, $error); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub load { |
55
|
9
|
|
|
9
|
1
|
2101
|
my ($self, $name) = @_; |
56
|
9
|
|
|
|
|
21
|
my ($data, $error); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Use renderer to find template |
59
|
9
|
|
|
|
|
20
|
my $renderer = $self->_mojo_renderer; |
60
|
9
|
|
|
|
|
25
|
my $options = _template_options($renderer, $name); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Try template |
63
|
9
|
100
|
|
|
|
37
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
50
|
|
|
|
|
|
64
|
3
|
|
|
|
|
290
|
($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
|
|
|
848
|
$d = Mojo::Util::encode $self->{ENCODING}, $d if $self->{UNICODE} and $self->{ENCODING}; |
71
|
6
|
|
|
|
|
67
|
($data, $error) = ($d, undef); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# No template |
75
|
0
|
|
|
|
|
0
|
else { return (undef, Template::Constants::STATUS_DECLINED) } |
76
|
|
|
|
|
|
|
|
77
|
9
|
50
|
|
|
|
387
|
if ($error) { |
78
|
0
|
0
|
|
|
|
0
|
return $self->{TOLERANT} ? (undef, Template::Constants::STATUS_DECLINED) |
79
|
|
|
|
|
|
|
: ($error, Template::Constants::STATUS_ERROR); |
80
|
|
|
|
|
|
|
} else { |
81
|
9
|
|
|
|
|
47
|
return ($data, Template::Constants::STATUS_OK); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
38
|
|
33
|
38
|
|
156
|
sub _mojo_renderer { shift->{MOJO_RENDERER} //= Mojolicious::Renderer->new } |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Split template name back into options |
88
|
|
|
|
|
|
|
sub _template_options { |
89
|
34
|
|
|
34
|
|
68
|
my ($renderer, $name) = @_; |
90
|
34
|
|
|
|
|
63
|
my $options = {}; |
91
|
34
|
50
|
|
|
|
193
|
if ($name =~ m/^(.+)\.(.+)\.(.+)\z/) { |
|
|
0
|
|
|
|
|
|
92
|
34
|
|
|
|
|
115
|
$options->{template} = $1; |
93
|
34
|
|
|
|
|
82
|
$options->{format} = $2; |
94
|
34
|
|
|
|
|
81
|
$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
|
|
|
|
|
70
|
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 |