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