line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TemplateToolkit; |
2
|
2
|
|
|
2
|
|
21935
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
14
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
360
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
119
|
|
5
|
2
|
|
|
2
|
|
11
|
use Mojo::Util qw(encode md5_sum); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
101
|
|
6
|
2
|
|
|
2
|
|
1077
|
use Template; |
|
2
|
|
|
|
|
37097
|
|
|
2
|
|
|
|
|
71
|
|
7
|
2
|
|
|
2
|
|
1038
|
use Template::Provider::Mojo; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1581
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.006'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
3
|
|
|
3
|
1
|
8178
|
my ($self, $app, $conf) = @_; |
13
|
|
|
|
|
|
|
|
14
|
3
|
|
100
|
|
|
21
|
my $tt_config = $conf->{template} || {}; |
15
|
3
|
|
|
|
|
17
|
$tt_config->{MOJO_RENDERER} = $app->renderer; |
16
|
3
|
|
|
|
|
20
|
push @{$tt_config->{LOAD_TEMPLATES}}, Template::Provider::Mojo->new($tt_config); |
|
3
|
|
|
|
|
26
|
|
17
|
3
|
|
|
|
|
110
|
my $tt = Template->new($tt_config); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->renderer->add_handler($conf->{name} || 'tt2' => sub { |
20
|
14
|
|
|
14
|
|
71849
|
my ($renderer, $c, $output, $options) = @_; |
21
|
|
|
|
|
|
|
|
22
|
14
|
|
|
|
|
34
|
my $inline = $options->{inline}; |
23
|
14
|
100
|
|
|
|
51
|
my $name = defined $inline ? md5_sum encode('UTF-8', $inline) : undef; |
24
|
14
|
50
|
66
|
|
|
214
|
return unless defined($name //= $renderer->template_name($options)); |
25
|
|
|
|
|
|
|
|
26
|
14
|
|
|
|
|
2381
|
my %params; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Helpers |
29
|
14
|
|
|
|
|
24
|
foreach my $method (grep { m/^\w+\z/ } keys %{$renderer->helpers}) { |
|
1036
|
|
|
|
|
2193
|
|
|
14
|
|
|
|
|
39
|
|
30
|
868
|
|
|
|
|
1537
|
my $sub = $renderer->helpers->{$method}; |
31
|
868
|
|
|
|
|
4534
|
$params{$method} = sub { carp "Calling helpers directly in templates is deprecated. Use c.$method"; $c->$sub(@_) }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Stash values |
35
|
14
|
|
|
|
|
44
|
$params{$_} = $c->stash->{$_} for grep { m/^\w+\z/ } keys %{$c->stash}; |
|
47
|
|
|
|
|
280
|
|
|
14
|
|
|
|
|
46
|
|
36
|
14
|
|
|
|
|
256
|
$params{self} = $params{c} = $c; |
37
|
14
|
|
|
|
|
52
|
$params{h} = $c->helpers; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Inline |
40
|
14
|
100
|
|
|
|
5641
|
if (defined $inline) { |
41
|
11
|
|
|
|
|
35
|
$c->app->log->debug(qq{Rendering inline template "$name"}); |
42
|
11
|
50
|
|
|
|
233
|
$tt->process(\$inline, \%params, $output) or die $tt->error, "\n"; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# File |
46
|
|
|
|
|
|
|
else { |
47
|
|
|
|
|
|
|
# Try template |
48
|
3
|
100
|
|
|
|
15
|
if (defined(my $path = $renderer->template_path($options))) { |
|
|
50
|
|
|
|
|
|
49
|
1
|
|
|
|
|
103
|
$c->app->log->debug(qq{Rendering template "$name"}); |
50
|
1
|
50
|
|
|
|
23
|
$tt->process($name, \%params, $output) or die $tt->error, "\n"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Try DATA section |
54
|
|
|
|
|
|
|
elsif (defined(my $d = $renderer->get_data_template($options))) { |
55
|
2
|
|
|
|
|
1046
|
$c->app->log->debug(qq{Rendering template "$name" from DATA section}); |
56
|
2
|
50
|
|
|
|
48
|
$tt->process(\$d, \%params, $output) or die $tt->error, "\n"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# No template |
60
|
0
|
|
|
|
|
|
else { $c->app->log->debug(qq{Template "$name" not found}) } |
61
|
|
|
|
|
|
|
} |
62
|
3
|
|
100
|
|
|
45129
|
}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 NAME |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Mojolicious::Plugin::TemplateToolkit - Template Toolkit renderer plugin for |
70
|
|
|
|
|
|
|
Mojolicious |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Mojolicious |
75
|
|
|
|
|
|
|
$app->plugin('TemplateToolkit'); |
76
|
|
|
|
|
|
|
$app->plugin(TemplateToolkit => {name => 'foo'}); |
77
|
|
|
|
|
|
|
$app->plugin(TemplateToolkit => {template => {INTERPOLATE => 1}}); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Mojolicious::Lite |
80
|
|
|
|
|
|
|
plugin 'TemplateToolkit'; |
81
|
|
|
|
|
|
|
plugin TemplateToolkit => {name => 'foo'}; |
82
|
|
|
|
|
|
|
plugin TemplateToolkit => {template => {INTERPOLATE => 1}}); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Set as default handler |
85
|
|
|
|
|
|
|
$app->renderer->default_handler('tt2'); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Render without setting as default handler |
88
|
|
|
|
|
|
|
$c->render(template => 'bar', handler => 'tt2'); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 DESCRIPTION |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
L is a renderer for C or |
93
|
|
|
|
|
|
|
C templates. See L and L for |
94
|
|
|
|
|
|
|
details on the C format, and L |
95
|
|
|
|
|
|
|
for general information on rendering in L. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Along with template files, inline and data section templates can be rendered in |
98
|
|
|
|
|
|
|
the standard Mojolicious fashion. Template files and data sections will be |
99
|
|
|
|
|
|
|
retrieved using L via L for |
100
|
|
|
|
|
|
|
both direct rendering and directives such as C. This means that |
101
|
|
|
|
|
|
|
instead of specifying L, |
102
|
|
|
|
|
|
|
you should set L to the appropriate paths. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$app->renderer->paths(['/path/to/templates']); |
105
|
|
|
|
|
|
|
push @{$app->renderer->paths}, '/path/to/more/templates'; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L stash values will be exposed directly as |
108
|
|
|
|
|
|
|
L in the templates, and the current |
109
|
|
|
|
|
|
|
controller object will be available as C or C, similar to |
110
|
|
|
|
|
|
|
L. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Helper methods can be called on the controller object as normal, as well as on |
113
|
|
|
|
|
|
|
a L available as C. The |
114
|
|
|
|
|
|
|
proxy object was previously needed for efficiency as the controller object used |
115
|
|
|
|
|
|
|
C to call helper methods, but since Mojolicious 8.04 the controller |
116
|
|
|
|
|
|
|
object uses a more efficient mechanism and the proxy object is no longer |
117
|
|
|
|
|
|
|
needed. See L and |
118
|
|
|
|
|
|
|
L for a list of all built-in helper methods. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Accessing helper methods directly as variables, rather than via the controller |
121
|
|
|
|
|
|
|
or proxy object, is deprecated and may be removed in a future release. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$c->stash(description => 'template engine'); |
124
|
|
|
|
|
|
|
$c->stash(engines => [qw(Template::Toolkit Text::Template)]); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
[% FOREACH engine IN engines %] |
127
|
|
|
|
|
|
|
[% engine %] is a [% description %]. |
128
|
|
|
|
|
|
|
[% END %] |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
[% c.link_to('Template Toolkit', 'http://www.template-toolkit.org') %] |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
[% c.param('foo') %] |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 OPTIONS |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L supports the following options. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 name |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Mojolicious::Lite |
142
|
|
|
|
|
|
|
plugin TemplateToolkit => {name => 'foo'}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Handler name, defaults to C. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 template |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Mojolicious::Lite |
149
|
|
|
|
|
|
|
plugin TemplateToolkit => {template => {INTERPOLATE => 1}}; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Configuration values passed to L object used to render templates. |
152
|
|
|
|
|
|
|
Note that L will use L |
153
|
|
|
|
|
|
|
to find templates, not L |
154
|
|
|
|
|
|
|
specified here. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 METHODS |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L inherits all methods from |
159
|
|
|
|
|
|
|
L and implements the following new ones. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 register |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
$plugin->register(Mojolicious->new); |
164
|
|
|
|
|
|
|
$plugin->register(Mojolicious->new, {name => 'foo'}); |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Register renderer in L application. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 BUGS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Report any issues on the public bugtracker. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Dan Book |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This software is Copyright (c) 2015 by Dan Book. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This is free software, licensed under: |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 SEE ALSO |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L, L, L |