line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MVC::Neaf::View::TT; |
2
|
|
|
|
|
|
|
|
3
|
23
|
|
|
23
|
|
145628
|
use 5.006; |
|
23
|
|
|
|
|
150
|
|
4
|
23
|
|
|
23
|
|
154
|
use strict; |
|
23
|
|
|
|
|
51
|
|
|
23
|
|
|
|
|
537
|
|
5
|
23
|
|
|
23
|
|
119
|
use warnings; |
|
23
|
|
|
|
|
48
|
|
|
23
|
|
|
|
|
1412
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.2901'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
MVC::Neaf::View::TT - Template toolkit-based view module for Neaf. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSYS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Somewhere in your app |
16
|
|
|
|
|
|
|
return { |
17
|
|
|
|
|
|
|
-view => 'TT', |
18
|
|
|
|
|
|
|
-template => 'foo.tt', |
19
|
|
|
|
|
|
|
title => 'Page title', |
20
|
|
|
|
|
|
|
.... |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
# Neaf knows it's time to render foo.tt with returned data as stash |
23
|
|
|
|
|
|
|
# and return result to user |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# What actually happens |
26
|
|
|
|
|
|
|
my $view = MVC::Neaf::View::TT->new; |
27
|
|
|
|
|
|
|
my $content = $view->render( { ... } ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# And if in foo.tt |
30
|
|
|
|
|
|
|
[% title %] |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Then in $content it becomes |
33
|
|
|
|
|
|
|
Page title |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This module is one of core rendering engines of L |
38
|
|
|
|
|
|
|
known under C alias. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
See also C. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 METHODS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=cut |
45
|
|
|
|
|
|
|
|
46
|
23
|
|
|
23
|
|
149
|
use Carp; |
|
23
|
|
|
|
|
45
|
|
|
23
|
|
|
|
|
1537
|
|
47
|
23
|
|
|
23
|
|
12590
|
use Template; |
|
23
|
|
|
|
|
509398
|
|
|
23
|
|
|
|
|
761
|
|
48
|
23
|
|
|
23
|
|
189
|
use Template::Provider; |
|
23
|
|
|
|
|
53
|
|
|
23
|
|
|
|
|
969
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
our @CARP_NOT = qw(MVC::Neaf::View MVC::Neaf MVC::Neaf::Request); |
51
|
|
|
|
|
|
|
|
52
|
23
|
|
|
23
|
|
988
|
use parent qw(MVC::Neaf::View); |
|
23
|
|
|
|
|
609
|
|
|
23
|
|
|
|
|
162
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 new( %options ) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
%options may include: |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item * template - default template to use. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item * preserve_dash - don't strip dashed options. Useful for debugging. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item * preload => { name => 'in-memory template' } - preload some templates. |
65
|
|
|
|
|
|
|
See C below. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item * INCLUDE_PATH => [path, ...] - will be calculated relative |
68
|
|
|
|
|
|
|
to the calling file, if not starts with a '/'. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=back |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Also any UPPERCASE OPTIONS will be forwarded to the backend |
73
|
|
|
|
|
|
|
(i.e. Template object) w/o changes. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Any extra options except those above will cause an exception. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my %new_opt; |
80
|
|
|
|
|
|
|
$new_opt{$_}++ for qw( template preserve_dash engine preload preload_auto neaf_base_dir ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my @opt_provider = qw( |
83
|
|
|
|
|
|
|
INCLUDE_PATH ABSOLUTE RELATIVE DEFAULT ENCODING CACHE_SIZE STAT_TTL |
84
|
|
|
|
|
|
|
COMPILE_EXT COMPILE_DIR TOLERANT PARSER DEBUG EVAL_PERL |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub new { |
88
|
17
|
|
|
17
|
1
|
678
|
my ($class, %opt) = @_; |
89
|
|
|
|
|
|
|
|
90
|
17
|
|
|
|
|
51
|
my %tt_opt; |
91
|
|
|
|
|
|
|
$tt_opt{$_} = delete $opt{$_} |
92
|
17
|
|
|
|
|
133
|
for grep { /^[A-Z]/ } keys %opt; |
|
22
|
|
|
|
|
124
|
|
93
|
17
|
|
|
|
|
62
|
my @extra = grep { !$new_opt{$_} } keys %opt; |
|
15
|
|
|
|
|
55
|
|
94
|
17
|
50
|
|
|
|
75
|
croak( "$class->new: Unknown options @extra" ) |
95
|
|
|
|
|
|
|
if @extra; |
96
|
|
|
|
|
|
|
|
97
|
17
|
|
|
|
|
59
|
my $engine = delete $opt{engine}; |
98
|
17
|
|
|
|
|
43
|
my $preload = delete $opt{preload}; |
99
|
17
|
|
|
|
|
209
|
my $self = $class->SUPER::new(%opt); |
100
|
|
|
|
|
|
|
|
101
|
17
|
|
33
|
|
|
178
|
$self->{engine} ||= do { |
102
|
|
|
|
|
|
|
# enforce non-absolute paths to be calculated relative to caller file |
103
|
17
|
|
100
|
|
|
221
|
$tt_opt{INCLUDE_PATH} = $self->dir( $tt_opt{INCLUDE_PATH} || [] ); |
104
|
|
|
|
|
|
|
|
105
|
17
|
|
|
|
|
50
|
my %prov_opt; |
106
|
17
|
|
|
|
|
51
|
foreach( @opt_provider) { |
107
|
|
|
|
|
|
|
$prov_opt{$_} = $tt_opt{$_} |
108
|
221
|
100
|
|
|
|
495
|
if defined $tt_opt{$_}; |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $prov = delete $tt_opt{LOAD_TEMPLATES} || [ |
112
|
17
|
|
50
|
|
|
289
|
Template::Provider->new(\%prov_opt) |
113
|
|
|
|
|
|
|
]; |
114
|
17
|
|
|
|
|
1841
|
$self->{engine_preload} = Template::Provider->new({ |
115
|
|
|
|
|
|
|
%prov_opt, |
116
|
|
|
|
|
|
|
CACHE_SIZE => undef, |
117
|
|
|
|
|
|
|
STAT_TTL => 4_000_000_000, |
118
|
|
|
|
|
|
|
}); |
119
|
|
|
|
|
|
|
# shallow copy (not unshift) to avoid spoiling original values |
120
|
17
|
|
|
|
|
1207
|
$prov = [ $self->{engine_preload}, @$prov ]; |
121
|
|
|
|
|
|
|
|
122
|
17
|
|
|
|
|
186
|
Template->new (%tt_opt, LOAD_TEMPLATES => $prov); |
123
|
|
|
|
|
|
|
}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# TODO 0.40 automagically preload from the calling file's DATA section |
127
|
17
|
100
|
|
|
|
353689
|
if ( ref $preload eq 'HASH' ) { |
|
|
50
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$self->preload( $_ => $preload->{$_} ) |
129
|
1
|
|
|
|
|
10
|
for keys %$preload; |
130
|
|
|
|
|
|
|
} elsif ($preload) { |
131
|
0
|
|
0
|
|
|
0
|
$self->_croak("preload must be a hash, not ".(ref $preload || "a scalar") ); |
132
|
|
|
|
|
|
|
}; |
133
|
17
|
|
|
|
|
101
|
return $self; |
134
|
|
|
|
|
|
|
}; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 render( \%data ) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns a pair of values: ($content, $content_type). |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Content-type defaults to text/html. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
The template is determined from (1) -template in data (2) template in new(). |
143
|
|
|
|
|
|
|
If neither is present, empty string and "text/plain" are returned. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub render { |
148
|
21
|
|
|
21
|
1
|
7434
|
my ($self, $data) = @_; |
149
|
|
|
|
|
|
|
|
150
|
21
|
|
66
|
|
|
94
|
my $template = $data->{-template} || $self->{template}; |
151
|
|
|
|
|
|
|
|
152
|
21
|
100
|
|
|
|
64
|
if (!defined $template) { |
153
|
1
|
|
|
|
|
255
|
croak __PACKAGE__.": -template option is required"; |
154
|
|
|
|
|
|
|
}; |
155
|
|
|
|
|
|
|
|
156
|
20
|
|
|
|
|
38
|
my $out; |
157
|
|
|
|
|
|
|
$self->{engine}->process( $template, $data, \$out ) |
158
|
20
|
100
|
|
|
|
120
|
or $self->_croak( $self->{engine}->error ); |
159
|
|
|
|
|
|
|
|
160
|
16
|
100
|
|
|
|
237627
|
return wantarray ? ($out, "text/html") : $out; |
161
|
|
|
|
|
|
|
}; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 preload ( name => "[% in_memory_template %]", ... ) |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Store precompiled templates under given names. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Returns self, dies on error. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub preload { |
172
|
6
|
|
|
6
|
1
|
3137
|
my ($self, %tpls) = @_; |
173
|
|
|
|
|
|
|
|
174
|
6
|
|
|
|
|
47
|
foreach (keys %tpls) { |
175
|
9
|
100
|
|
|
|
281
|
my $compiled = eval { $self->{engine}->template( \$tpls{$_} ) } |
|
9
|
|
|
|
|
64
|
|
176
|
|
|
|
|
|
|
or $self->_croak( "$_: $@" ); |
177
|
|
|
|
|
|
|
|
178
|
8
|
|
|
|
|
151267
|
$self->{engine_preload}->store( $_, $compiled ); |
179
|
|
|
|
|
|
|
}; |
180
|
|
|
|
|
|
|
|
181
|
5
|
|
|
|
|
523
|
return $self; |
182
|
|
|
|
|
|
|
}; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _croak { |
185
|
5
|
|
|
5
|
|
59717
|
my ($self, @msg) = @_; |
186
|
|
|
|
|
|
|
|
187
|
5
|
|
|
|
|
55
|
my $where = [caller(1)]->[3]; |
188
|
5
|
|
|
|
|
99
|
$where =~ s/.*:://; |
189
|
|
|
|
|
|
|
|
190
|
5
|
|
33
|
|
|
268
|
croak join "", (ref $self || $self), '->', $where, "(): ", @msg; |
191
|
|
|
|
|
|
|
}; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 SEE ALSO |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
L - the template toolkit used as backend. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
This module is part of L suite. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Copyright 2016-2023 Konstantin S. Uvarin C. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
204
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
205
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
See L for more information. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
1; |