| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package CGI::Application::Plugin::AnyTemplate; |
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate - Use any templating system from within CGI::Application using a unified interface |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 VERSION |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Version 0.18 |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.18'; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
In your CGI::Application-based webapp: |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use base 'CGI::Application'; |
|
21
|
|
|
|
|
|
|
use CGI::Application::Plugin::AnyTemplate; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub cgiapp_init { |
|
24
|
|
|
|
|
|
|
my $self = shift; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Set template options |
|
27
|
|
|
|
|
|
|
$self->template->config( |
|
28
|
|
|
|
|
|
|
default_type => 'TemplateToolkit', |
|
29
|
|
|
|
|
|
|
); |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Later on, in a runmode: |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub my_runmode { |
|
36
|
|
|
|
|
|
|
my $self = shift; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my %template_params = ( |
|
39
|
|
|
|
|
|
|
name => 'Winston Churchill', |
|
40
|
|
|
|
|
|
|
age => 7, |
|
41
|
|
|
|
|
|
|
); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$self->template->fill('some_template', \%template_params); |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head2 Template-Independence |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
C allows you to use any |
|
51
|
|
|
|
|
|
|
supported Perl templating system using a single consistent interface. |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Currently supported templating systems include L, |
|
54
|
|
|
|
|
|
|
L, L, |
|
55
|
|
|
|
|
|
|
L and L. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
You can access any of these templating systems using the same interface. |
|
58
|
|
|
|
|
|
|
In this way, you can use the same code and switch templating systems on |
|
59
|
|
|
|
|
|
|
the fly. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This approach has many uses. For instance, it can be useful in |
|
62
|
|
|
|
|
|
|
migrating your application from one templating system to another. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 Embedded Components |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
In addition to template abstraction, C also provides a |
|
67
|
|
|
|
|
|
|
I. For instance, you might include a |
|
68
|
|
|
|
|
|
|
I component at the top of every page and a I |
|
69
|
|
|
|
|
|
|
at the bottom of every page. |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
These components are actually full L run modes, and |
|
72
|
|
|
|
|
|
|
can do anything normal run mode can do, including processing form |
|
73
|
|
|
|
|
|
|
parameters and filling in their own templates. See |
|
74
|
|
|
|
|
|
|
below under L<"EMBEDDED COMPONENTS"> for details. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 Multiple Named Template Configurations |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
You can set up multiple named template configurations and select between |
|
79
|
|
|
|
|
|
|
them at run time. |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub cgiapp_init { |
|
82
|
|
|
|
|
|
|
my $self = shift; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Can't use Template::Toolkit any more - |
|
85
|
|
|
|
|
|
|
# The boss wants everything has to be XML, |
|
86
|
|
|
|
|
|
|
# so we switch to Petal |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Set old-style template options (legacy scripts) |
|
89
|
|
|
|
|
|
|
$self->template('oldstyle')->config( |
|
90
|
|
|
|
|
|
|
default_type => 'TemplateToolkit', |
|
91
|
|
|
|
|
|
|
TemplateToolkit => { |
|
92
|
|
|
|
|
|
|
POST_CHOMP => 1, |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
); |
|
95
|
|
|
|
|
|
|
# Set new-style template options as default |
|
96
|
|
|
|
|
|
|
$self->template->config( |
|
97
|
|
|
|
|
|
|
default_type => 'Petal', |
|
98
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
99
|
|
|
|
|
|
|
); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub old_style_runmode { |
|
103
|
|
|
|
|
|
|
my $self = shift; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# ... |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# use TemplateToolkit to fill template edit_user.tmpl |
|
108
|
|
|
|
|
|
|
$self->template('oldstyle')->fill('edit_user', \%params); |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub new_style_runmode { |
|
113
|
|
|
|
|
|
|
my $self = shift; |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# ... |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# use Petal to fill template edit_user.xhml |
|
118
|
|
|
|
|
|
|
$self->template->fill('edit_user.xhtml', \%params); |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 Flexible Syntax |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
The syntax is pretty flexible. Pick a style that's most comfortable for |
|
125
|
|
|
|
|
|
|
you. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head3 CGI::Application::Plugin::TT style syntax |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$self->template->process('edit_user', \%params); |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
or (with slightly less typing): |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$self->template->fill('edit_user', \%params); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head3 CGI::Application load_tmpl style syntax |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $template = $self->template->load('edit_user'); |
|
138
|
|
|
|
|
|
|
$template->param('foo' => 'bar'); |
|
139
|
|
|
|
|
|
|
$template->output; |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head3 Verbose syntax (for complete control) |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $template = $self->template('named_config')->load( |
|
144
|
|
|
|
|
|
|
file => 'edit_user' |
|
145
|
|
|
|
|
|
|
type => 'TemplateToolkit' |
|
146
|
|
|
|
|
|
|
add_include_paths => '.', |
|
147
|
|
|
|
|
|
|
); |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$template->param('foo' => 'bar'); |
|
150
|
|
|
|
|
|
|
$template->output; |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
See also below under L<"CHANGING THE NAME OF THE 'template' METHOD">. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
|
155
|
|
|
|
|
|
|
|
|
156
|
26
|
|
|
26
|
|
953785
|
use strict; |
|
|
26
|
|
|
|
|
72
|
|
|
|
26
|
|
|
|
|
997
|
|
|
157
|
26
|
|
|
26
|
|
1314
|
use CGI::Application; |
|
|
26
|
|
|
|
|
8863
|
|
|
|
26
|
|
|
|
|
492
|
|
|
158
|
26
|
|
|
26
|
|
153
|
use Carp; |
|
|
26
|
|
|
|
|
57
|
|
|
|
26
|
|
|
|
|
2135
|
|
|
159
|
26
|
|
|
26
|
|
155
|
use Scalar::Util qw(weaken); |
|
|
26
|
|
|
|
|
45
|
|
|
|
26
|
|
|
|
|
3799
|
|
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
if ( ! eval { require Clone } ) { |
|
162
|
|
|
|
|
|
|
if ( eval { require Clone::PP } ) { |
|
163
|
26
|
|
|
26
|
|
132
|
no strict 'refs'; |
|
|
26
|
|
|
|
|
53
|
|
|
|
26
|
|
|
|
|
2262
|
|
|
164
|
|
|
|
|
|
|
*Clone::clone = *Clone::PP::clone; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
else { |
|
167
|
|
|
|
|
|
|
die "Neiter Clone nor Clone::PP found - $@\n"; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
26
|
|
|
26
|
|
173
|
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $CAPAT_Namespace); |
|
|
26
|
|
|
|
|
47
|
|
|
|
26
|
|
|
|
|
69853
|
|
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
@ISA = ('Exporter'); |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
@ISA = 'Exporter'; |
|
176
|
|
|
|
|
|
|
@EXPORT = qw(template); |
|
177
|
|
|
|
|
|
|
@EXPORT_OK = qw(load_tmpl); |
|
178
|
|
|
|
|
|
|
%EXPORT_TAGS = (load_tmpl => ['load_tmpl', @EXPORT]); |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
$CAPAT_Namespace = '__ANY_TEMPLATE'; |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
if (CGI::Application->can('new_hook')) { |
|
183
|
|
|
|
|
|
|
CGI::Application->new_hook('template_pre_process'); |
|
184
|
|
|
|
|
|
|
CGI::Application->new_hook('template_post_process'); |
|
185
|
|
|
|
|
|
|
CGI::Application->new_hook('load_tmpl'); |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub _new { |
|
189
|
82
|
|
|
82
|
|
154
|
my $proto = shift; |
|
190
|
82
|
|
33
|
|
|
478
|
my $class = ref $proto || $proto; |
|
191
|
82
|
|
|
|
|
162
|
my $webapp = shift; |
|
192
|
82
|
|
|
|
|
136
|
my $conf_name = shift; |
|
193
|
|
|
|
|
|
|
|
|
194
|
82
|
|
|
|
|
459
|
my $self = { |
|
195
|
|
|
|
|
|
|
'conf_name' => $conf_name, |
|
196
|
|
|
|
|
|
|
'base_config' => {}, |
|
197
|
|
|
|
|
|
|
'current_config' => {}, |
|
198
|
|
|
|
|
|
|
'webapp' => $webapp, |
|
199
|
|
|
|
|
|
|
}; |
|
200
|
|
|
|
|
|
|
|
|
201
|
82
|
|
|
|
|
253
|
bless $self, $class; |
|
202
|
|
|
|
|
|
|
|
|
203
|
82
|
|
|
|
|
502
|
weaken $self->{'webapp'}; |
|
204
|
|
|
|
|
|
|
|
|
205
|
82
|
|
|
|
|
353
|
return $self; |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
2
|
|
|
2
|
|
7
|
sub _default_type { 'HTMLTemplate' } |
|
209
|
2
|
|
|
2
|
|
9
|
sub _default_extension { '.html' } |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 METHODS |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 config |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Initialize the C system and provide the default |
|
216
|
|
|
|
|
|
|
configuration. |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
$self->template->config( |
|
219
|
|
|
|
|
|
|
default_type => 'HTMLTemplate', |
|
220
|
|
|
|
|
|
|
); |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
You can keep multiple configurations handy at the same time by passing a |
|
223
|
|
|
|
|
|
|
value to C: |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
$self->template('oldstyle')->config( |
|
226
|
|
|
|
|
|
|
default_type => 'HTML::Template', |
|
227
|
|
|
|
|
|
|
); |
|
228
|
|
|
|
|
|
|
$self->template('newstyle')->config( |
|
229
|
|
|
|
|
|
|
default_type => 'HTML::Template::Expr', |
|
230
|
|
|
|
|
|
|
); |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Then in a runmode you can mix and match configurations: |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
$self->template('oldstyle')->load # loads an HTML::Template driver object |
|
235
|
|
|
|
|
|
|
$self->template('newstyle')->load # loads an HTML::Template::Expr driver object |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
The configuration passed to C is divided into three areas: |
|
239
|
|
|
|
|
|
|
I, I, and I
|
|
240
|
|
|
|
|
|
|
configuration>: |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Config Type What it Configures |
|
243
|
|
|
|
|
|
|
----------- ------------------ |
|
244
|
|
|
|
|
|
|
Plugin Config AnyTemplate itself |
|
245
|
|
|
|
|
|
|
Driver Config AnyTemplate Driver (e.g. HTMLTemplate) |
|
246
|
|
|
|
|
|
|
Native Config Actual template module (e.g. HTML::Template) |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
These are described in more detail below. |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head3 Plugin Configuration |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
These configuration params are specific to the C itself. |
|
253
|
|
|
|
|
|
|
They are included at the top level of the configuration hash passed to C. For instance: |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
$self->template->config( |
|
256
|
|
|
|
|
|
|
default_type => 'HTMLTemplate', |
|
257
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
258
|
|
|
|
|
|
|
); |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
The I parameters and their defaults are as follows: |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=over 4 |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=item default_type |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=item type |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
The default type of template for this named configuration. Should be the name of a driver |
|
269
|
|
|
|
|
|
|
in the C namespace: |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Type Driver |
|
272
|
|
|
|
|
|
|
---- ------ |
|
273
|
|
|
|
|
|
|
HTMLTemplate CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate |
|
274
|
|
|
|
|
|
|
HTMLTemplateExpr CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExpr |
|
275
|
|
|
|
|
|
|
TemplateToolkit CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit |
|
276
|
|
|
|
|
|
|
Petal CGI::Application::Plugin::AnyTemplate::Driver::Petal |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=item include_paths |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
Include Paths (sometimes called search paths) are used by the various |
|
281
|
|
|
|
|
|
|
template backends to find filenames that aren't fully qualified by an |
|
282
|
|
|
|
|
|
|
absolute path. Each directory is searched in turn until the template |
|
283
|
|
|
|
|
|
|
file is found. |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Can be a single string or a reference to a list. |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=item auto_add_template_extension |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
Add a template-system specific extension to template filenames. |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
So, if this feature is enabled and you provide the filename C, |
|
292
|
|
|
|
|
|
|
then the actual filename will depend on the current template driver: |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
Driver Template |
|
295
|
|
|
|
|
|
|
------ -------- |
|
296
|
|
|
|
|
|
|
HTMLTemplate myfile.html |
|
297
|
|
|
|
|
|
|
HTMLTemplateExpr myfile.html |
|
298
|
|
|
|
|
|
|
TemplateToolkit myfile.tmpl |
|
299
|
|
|
|
|
|
|
Petal myfile.xhtml |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
The per-type extension is controlled by the driver config for each |
|
302
|
|
|
|
|
|
|
C driver (see below under L<"Driver and Native Configuration"> for how |
|
303
|
|
|
|
|
|
|
to set this). |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
The C feature is on by default. To disable |
|
306
|
|
|
|
|
|
|
it, pass a value of zero: |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
$self->template->config( |
|
309
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
310
|
|
|
|
|
|
|
); |
|
311
|
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
The automatic extension feature is not just there to save typing - it's |
|
313
|
|
|
|
|
|
|
actually there so you can have templates of different types sitting in |
|
314
|
|
|
|
|
|
|
the same directory. |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
sub my_runmode { |
|
317
|
|
|
|
|
|
|
my $self = shift; |
|
318
|
|
|
|
|
|
|
$self->template->fill; |
|
319
|
|
|
|
|
|
|
} |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
Then in your template path you can have three files: |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
my_runmode.html |
|
324
|
|
|
|
|
|
|
my_runmode.tmpl |
|
325
|
|
|
|
|
|
|
my_runmode.xhtml |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
Then you can change which templates is used by changing the value of |
|
328
|
|
|
|
|
|
|
C that you pass to C<< $self->template->config >>. |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
For applications that want to dynamically choose their template system |
|
331
|
|
|
|
|
|
|
without changing app code, it's a cleaner solution to use the extensions |
|
332
|
|
|
|
|
|
|
than trying to swap template paths at runtime. Even if you keep each |
|
333
|
|
|
|
|
|
|
type of template in its own directory, it's simpler to include all |
|
334
|
|
|
|
|
|
|
the directories all the time and use different extensions for different |
|
335
|
|
|
|
|
|
|
template types. |
|
336
|
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=item template_filename_generator |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
If you don't pass a filename to C, one will be generated for you |
|
340
|
|
|
|
|
|
|
based on the current run mode. If you want to customize this process, |
|
341
|
|
|
|
|
|
|
you can pass a reference to a subroutine to do the translation. This |
|
342
|
|
|
|
|
|
|
subroutine will be passed a reference to the CGI::Application C<$self> |
|
343
|
|
|
|
|
|
|
object. |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
Here is a subroutine that emulates the built-in behaviour of |
|
346
|
|
|
|
|
|
|
C: |
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
$self->template->config( |
|
349
|
|
|
|
|
|
|
template_filename_generator => sub { |
|
350
|
|
|
|
|
|
|
my ($self, $calling_method_name) = @_; |
|
351
|
|
|
|
|
|
|
return $self->get_current_runmode; |
|
352
|
|
|
|
|
|
|
} |
|
353
|
|
|
|
|
|
|
} |
|
354
|
|
|
|
|
|
|
); |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
Here is an example of using a template filename generator to make full |
|
358
|
|
|
|
|
|
|
templates with full paths based on the module name as well as the |
|
359
|
|
|
|
|
|
|
current run mode (this is similar to how L |
|
360
|
|
|
|
|
|
|
generates its template filenames): |
|
361
|
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
package My::WebApp; |
|
363
|
|
|
|
|
|
|
use File::Spec; |
|
364
|
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
sub cgiapp_init { |
|
366
|
|
|
|
|
|
|
my $self = shift; |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
$self->template->config( |
|
369
|
|
|
|
|
|
|
template_filename_generator => sub { |
|
370
|
|
|
|
|
|
|
my $self = shift; |
|
371
|
|
|
|
|
|
|
my $run_mode = $self->get_current_runmode; |
|
372
|
|
|
|
|
|
|
my $module = ref $self; |
|
373
|
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
my @segments = split /::/, $module; |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
return File::Spec->catfile(@segments, $run_mode); |
|
377
|
|
|
|
|
|
|
} |
|
378
|
|
|
|
|
|
|
); |
|
379
|
|
|
|
|
|
|
} |
|
380
|
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
sub run_mode { |
|
382
|
|
|
|
|
|
|
my $self = shift; |
|
383
|
|
|
|
|
|
|
$self->template->load; # loads My/WebApp/run_mode.html |
|
384
|
|
|
|
|
|
|
} |
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
sub other_run_mode { |
|
387
|
|
|
|
|
|
|
my $self = shift; |
|
388
|
|
|
|
|
|
|
$self->template->load; # loads My/WebApp/other_run_mode.html |
|
389
|
|
|
|
|
|
|
} |
|
390
|
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
Note that if the C option is on (which it |
|
392
|
|
|
|
|
|
|
is by default), then the extension will be added to your generated |
|
393
|
|
|
|
|
|
|
filename after you return it. If you do not want this to happen, then |
|
394
|
|
|
|
|
|
|
set C to a false value. |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=item component_handler_class |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
Normally, component embedding is handled by |
|
399
|
|
|
|
|
|
|
L. If you want to |
|
400
|
|
|
|
|
|
|
use a different class for this purpose, specify the class name as the |
|
401
|
|
|
|
|
|
|
value of this paramter. |
|
402
|
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
It still has to provide the same interface as |
|
404
|
|
|
|
|
|
|
L. See the source |
|
405
|
|
|
|
|
|
|
code of that module for details. |
|
406
|
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
=item return_references |
|
408
|
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
When true (the default), C |
|
410
|
|
|
|
|
|
|
rather than a copy. Normally this won't matter. For instance, |
|
411
|
|
|
|
|
|
|
C doesn't care whether you return a string or a |
|
412
|
|
|
|
|
|
|
reference to a string from your run modes. |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
However, if you want to manipulate the output of the C<$html> returned |
|
415
|
|
|
|
|
|
|
from the template, you may find it convenient to make C |
|
416
|
|
|
|
|
|
|
string instead of a reference. Especially if you are converting old |
|
417
|
|
|
|
|
|
|
code based on HTML::Template which expects C |
|
418
|
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
=back |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
=head3 Driver and Native Configuration |
|
422
|
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
You can configure all the drivers at once with a single call to |
|
424
|
|
|
|
|
|
|
C, by including subsections for each driver type: |
|
425
|
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
$self->template->config( |
|
427
|
|
|
|
|
|
|
default_type => 'HTMLTemplate', |
|
428
|
|
|
|
|
|
|
HTMLTemplate => { |
|
429
|
|
|
|
|
|
|
cache => 1, |
|
430
|
|
|
|
|
|
|
global_vars => 1, |
|
431
|
|
|
|
|
|
|
die_on_bad_params => 0, |
|
432
|
|
|
|
|
|
|
template_extension => '.html', |
|
433
|
|
|
|
|
|
|
}, |
|
434
|
|
|
|
|
|
|
HTMLTemplateExpr => { |
|
435
|
|
|
|
|
|
|
cache => 1, |
|
436
|
|
|
|
|
|
|
global_vars => 1, |
|
437
|
|
|
|
|
|
|
die_on_bad_params => 0, |
|
438
|
|
|
|
|
|
|
template_extension => '.html', |
|
439
|
|
|
|
|
|
|
}, |
|
440
|
|
|
|
|
|
|
HTMLTemplatePluggable => { |
|
441
|
|
|
|
|
|
|
cache => 1, |
|
442
|
|
|
|
|
|
|
global_vars => 1, |
|
443
|
|
|
|
|
|
|
die_on_bad_params => 0, |
|
444
|
|
|
|
|
|
|
template_extension => '.html', |
|
445
|
|
|
|
|
|
|
}, |
|
446
|
|
|
|
|
|
|
TemplateToolkit => { |
|
447
|
|
|
|
|
|
|
POST_CHOMP => 1, |
|
448
|
|
|
|
|
|
|
template_extension => '.tmpl', |
|
449
|
|
|
|
|
|
|
}, |
|
450
|
|
|
|
|
|
|
Petal => { |
|
451
|
|
|
|
|
|
|
error_on_undef => 0, |
|
452
|
|
|
|
|
|
|
template_extension => '.xhtml', |
|
453
|
|
|
|
|
|
|
}, |
|
454
|
|
|
|
|
|
|
); |
|
455
|
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
Each driver knows how to separate its own configuration from the |
|
458
|
|
|
|
|
|
|
configuration belonging to the underlying template system. |
|
459
|
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
For instance in the example above, the C driver knows that |
|
461
|
|
|
|
|
|
|
C is a driver config parameter, but |
|
462
|
|
|
|
|
|
|
C and C are all HTML::Template |
|
463
|
|
|
|
|
|
|
configuration parameters. |
|
464
|
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
Similarly, The C driver knows that template_extension |
|
466
|
|
|
|
|
|
|
is a driver config parameter, but C is a |
|
467
|
|
|
|
|
|
|
C configuration parameter. |
|
468
|
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
For details on driver configuration, see the docs for the individual |
|
470
|
|
|
|
|
|
|
drivers: |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
=over 4 |
|
473
|
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
=item L |
|
475
|
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
=item L |
|
477
|
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
=item L |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
=item L |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
=item L |
|
483
|
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=back |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
=head3 Copying Query data into Templates |
|
487
|
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
B |
|
489
|
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
When you enable this feature all data in C<< $self->query >> are copied |
|
491
|
|
|
|
|
|
|
into the template object before the template is processed. |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
For the C, C and |
|
494
|
|
|
|
|
|
|
C drivers this is done with the C |
|
495
|
|
|
|
|
|
|
feature of L and L, respectively: |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
my $template = HTML::Template->new( |
|
498
|
|
|
|
|
|
|
associate => $self->query, |
|
499
|
|
|
|
|
|
|
); |
|
500
|
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
For the other systems, this feature is emulated, by copying the query |
|
502
|
|
|
|
|
|
|
params into the template params before the template is processed. |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
To enable this feature, pass a true value to C or |
|
505
|
|
|
|
|
|
|
C (depending on the template system): |
|
506
|
|
|
|
|
|
|
$self->template->config( |
|
507
|
|
|
|
|
|
|
default_type => 'HTMLTemplate', |
|
508
|
|
|
|
|
|
|
HTMLTemplate => { |
|
509
|
|
|
|
|
|
|
associate_query => 1, |
|
510
|
|
|
|
|
|
|
}, |
|
511
|
|
|
|
|
|
|
HTMLTemplateExpr => { |
|
512
|
|
|
|
|
|
|
associate_query => 1, |
|
513
|
|
|
|
|
|
|
}, |
|
514
|
|
|
|
|
|
|
HTMLTemplatePluggable => { |
|
515
|
|
|
|
|
|
|
associate_query => 1, |
|
516
|
|
|
|
|
|
|
}, |
|
517
|
|
|
|
|
|
|
TemplateToolkit => { |
|
518
|
|
|
|
|
|
|
emulate_associate_query => 1, |
|
519
|
|
|
|
|
|
|
}, |
|
520
|
|
|
|
|
|
|
Petal => { |
|
521
|
|
|
|
|
|
|
emulate_associate_query => 1, |
|
522
|
|
|
|
|
|
|
}, |
|
523
|
|
|
|
|
|
|
); |
|
524
|
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
The reason this feature is now disabled by default is that it |
|
526
|
|
|
|
|
|
|
poses a potential XSS (Cross Site Scripting) security risk. |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
The reason this feature is now deprecated is that in an ideal world |
|
529
|
|
|
|
|
|
|
developers shouldn't have to flatten objects and hashes in order to make |
|
530
|
|
|
|
|
|
|
them available to their templates. They should be able to pass the query |
|
531
|
|
|
|
|
|
|
object (or another object such as a config object) directly into the |
|
532
|
|
|
|
|
|
|
template: |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
$template->param( |
|
535
|
|
|
|
|
|
|
'query' => $self->query, |
|
536
|
|
|
|
|
|
|
'cfg' => $self->cfg, |
|
537
|
|
|
|
|
|
|
'ENV' => $ENV, |
|
538
|
|
|
|
|
|
|
); |
|
539
|
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
And in the template retrieve parameters directly: |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
your username: [% query.param('username') %] |
|
543
|
|
|
|
|
|
|
administrator: [% cfg.admin %] |
|
544
|
|
|
|
|
|
|
hostname: [% ENV.SERVER_NAME %] |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
This approach works with L, L, and |
|
547
|
|
|
|
|
|
|
L (via the L plugin). |
|
548
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
Note that C and C are not compatible. So if |
|
550
|
|
|
|
|
|
|
you want to associate the query and an additional object, pass a list to |
|
551
|
|
|
|
|
|
|
C: |
|
552
|
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
$template->config( |
|
554
|
|
|
|
|
|
|
HTMLTemplate => { |
|
555
|
|
|
|
|
|
|
associate => [$self->query, $self->conf] |
|
556
|
|
|
|
|
|
|
} |
|
557
|
|
|
|
|
|
|
); |
|
558
|
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
=cut |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
sub config { |
|
563
|
82
|
|
|
82
|
1
|
1438
|
my $self = shift; |
|
564
|
|
|
|
|
|
|
|
|
565
|
82
|
100
|
|
|
|
532
|
my $config = ref $_[0] eq 'HASH' ? { %{ $_[0] } } : { @_ }; |
|
|
2
|
|
|
|
|
13
|
|
|
566
|
|
|
|
|
|
|
|
|
567
|
82
|
|
|
|
|
323
|
$config->{'callers_package'} = scalar caller; |
|
568
|
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
# reset storage and add configuration |
|
570
|
82
|
|
|
|
|
325
|
$self->_clear_configuration($self->{'base_config'}); |
|
571
|
82
|
|
|
|
|
285
|
$self->_add_configuration($self->{'base_config'}, $config); |
|
572
|
|
|
|
|
|
|
} |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
# The template method returns or creates an CAP::AnyTemplate object |
|
576
|
|
|
|
|
|
|
# either the default one (if no name provided, or the named one) |
|
577
|
|
|
|
|
|
|
sub template { |
|
578
|
194
|
|
|
194
|
0
|
1483196
|
my ($self, $config_name) = @_; |
|
579
|
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
# TODO: make CAPAT subclassable somehow? |
|
581
|
|
|
|
|
|
|
|
|
582
|
194
|
100
|
|
|
|
705
|
if (defined $config_name) { |
|
583
|
|
|
|
|
|
|
# Named config |
|
584
|
95
|
100
|
|
|
|
512
|
if (not exists $self->{$CAPAT_Namespace}->{'__NAMED_CONFIGS'}->{$config_name}) { |
|
585
|
49
|
|
|
|
|
216
|
$self->{$CAPAT_Namespace}->{'__NAMED_CONFIGS'}->{$config_name} = __PACKAGE__->_new($self, $config_name); |
|
586
|
|
|
|
|
|
|
} |
|
587
|
95
|
|
|
|
|
604
|
return $self->{$CAPAT_Namespace}->{'__NAMED_CONFIGS'}->{$config_name}; |
|
588
|
|
|
|
|
|
|
} |
|
589
|
|
|
|
|
|
|
else { |
|
590
|
|
|
|
|
|
|
# Default config |
|
591
|
99
|
100
|
|
|
|
634
|
if (not exists $self->{$CAPAT_Namespace}->{'__DEFAULT_CONFIG'}) { |
|
592
|
33
|
|
|
|
|
294
|
$self->{$CAPAT_Namespace}->{'__DEFAULT_CONFIG'} = __PACKAGE__->_new($self); |
|
593
|
|
|
|
|
|
|
} |
|
594
|
99
|
|
|
|
|
608
|
return $self->{$CAPAT_Namespace}->{'__DEFAULT_CONFIG'}; |
|
595
|
|
|
|
|
|
|
} |
|
596
|
|
|
|
|
|
|
} |
|
597
|
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
=head2 load |
|
599
|
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
Create a new template object and configure it. |
|
601
|
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
This can be as simple (and magical) as: |
|
603
|
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
my $template = $self->template->load; |
|
605
|
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
When you call C with no parameters, it uses the default template |
|
607
|
|
|
|
|
|
|
type, the default template configuration, and it determines the name of |
|
608
|
|
|
|
|
|
|
the template based on the name of the current run mode. It determines |
|
609
|
|
|
|
|
|
|
the current run mode by calling C<< $self->get_current_runmode >>. |
|
610
|
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
If you want to have the current runmode updated when you pass control to |
|
612
|
|
|
|
|
|
|
another runmode, use the L module: |
|
613
|
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
use CGI::Application::Plugin::Forward; |
|
615
|
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
sub first_runmode { |
|
617
|
|
|
|
|
|
|
my $self = shift; |
|
618
|
|
|
|
|
|
|
return $self->forward('second_runmode'); |
|
619
|
|
|
|
|
|
|
} |
|
620
|
|
|
|
|
|
|
sub second_runmode { |
|
621
|
|
|
|
|
|
|
my $self = shift; |
|
622
|
|
|
|
|
|
|
my $template = $self->template->load; # loads 'second_runmode.html' |
|
623
|
|
|
|
|
|
|
} |
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
If instead you call C<< $self->other_method >> directly, the value |
|
626
|
|
|
|
|
|
|
of C<< $self->get_current_runmode >> will not be updated: |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
sub first_runmode { |
|
629
|
|
|
|
|
|
|
my $self = shift; |
|
630
|
|
|
|
|
|
|
return $self->other_method; |
|
631
|
|
|
|
|
|
|
} |
|
632
|
|
|
|
|
|
|
sub other_method { |
|
633
|
|
|
|
|
|
|
my $self = shift; |
|
634
|
|
|
|
|
|
|
my $template = $self->template->load; # loads 'first_runmode.html' |
|
635
|
|
|
|
|
|
|
} |
|
636
|
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
If you want to override the way the default template filename is |
|
638
|
|
|
|
|
|
|
generated, you can do so with the C |
|
639
|
|
|
|
|
|
|
configuration parameter. |
|
640
|
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
If you call C with one paramter, it is taken to be either the |
|
642
|
|
|
|
|
|
|
filename or a reference to a string containing the template text: |
|
643
|
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
my $template = $self->template->load('somefile'); |
|
645
|
|
|
|
|
|
|
my $template = $self->template->load(\$some_text); |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
If the parameter C is true, then the |
|
648
|
|
|
|
|
|
|
appropriate extension will be added for this template type. |
|
649
|
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
If you call C with more than one parameter, then |
|
651
|
|
|
|
|
|
|
you can specify filename and configuration paramters directly: |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
my $template = $self->template->load( |
|
654
|
|
|
|
|
|
|
file => 'some_file.tmpl', |
|
655
|
|
|
|
|
|
|
type => 'HTMLTemplate', |
|
656
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
657
|
|
|
|
|
|
|
add_include_paths => '..', |
|
658
|
|
|
|
|
|
|
HTMLTemplate => { |
|
659
|
|
|
|
|
|
|
die_on_bad_params => 1, |
|
660
|
|
|
|
|
|
|
}, |
|
661
|
|
|
|
|
|
|
); |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
To initialize the template from a string rather than a file, use: |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
my $template = $self->template->load( |
|
666
|
|
|
|
|
|
|
string => \$some_text, |
|
667
|
|
|
|
|
|
|
); |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
The configuration parameters you pass to C are merged with the |
|
670
|
|
|
|
|
|
|
configuration that was passed to L<"config">. |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
You can include any of the configuration parameters that you can pass to |
|
673
|
|
|
|
|
|
|
config, plus the following extra parameters: |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
=over 4 |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
=item file |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
If you are loading the template from a file, then the C parameter |
|
680
|
|
|
|
|
|
|
contains the template's filename. |
|
681
|
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
=item string |
|
683
|
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
If you are loading the template from a string, then the C parameter |
|
685
|
|
|
|
|
|
|
contains the text of the template. It can be either a scalar or a |
|
686
|
|
|
|
|
|
|
reference to a scalar. Both of the following will work: |
|
687
|
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
# passing a string |
|
689
|
|
|
|
|
|
|
my $template = $self->template->load( |
|
690
|
|
|
|
|
|
|
string => $some_text, |
|
691
|
|
|
|
|
|
|
); |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
# passing a reference to a string |
|
694
|
|
|
|
|
|
|
my $template = $self->template->load( |
|
695
|
|
|
|
|
|
|
string => \$some_text, |
|
696
|
|
|
|
|
|
|
); |
|
697
|
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
=item add_include_paths |
|
699
|
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
Additional include paths. These will be merged with C |
|
701
|
|
|
|
|
|
|
before being passed to the template driver. |
|
702
|
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
=back |
|
704
|
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
The C method returns a template driver object. See below under |
|
706
|
|
|
|
|
|
|
C, for how to use this object. |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
=cut |
|
709
|
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
sub load { |
|
711
|
112
|
|
|
112
|
1
|
254
|
my $self = shift; |
|
712
|
|
|
|
|
|
|
|
|
713
|
112
|
|
|
|
|
180
|
my $config; |
|
714
|
112
|
100
|
|
|
|
349
|
if (@_) { |
|
715
|
58
|
100
|
|
|
|
175
|
if (@_ == 1) { |
|
716
|
16
|
50
|
|
|
|
47
|
if (ref $_[0] eq 'HASH') { |
|
717
|
|
|
|
|
|
|
# args: single hashref |
|
718
|
0
|
|
|
|
|
0
|
$config = $_[0]; |
|
719
|
|
|
|
|
|
|
} |
|
720
|
|
|
|
|
|
|
else { |
|
721
|
|
|
|
|
|
|
# args: single value (=filename or string_ref) |
|
722
|
16
|
100
|
|
|
|
52
|
if (ref $_[0] eq 'SCALAR') { |
|
723
|
6
|
|
|
|
|
23
|
$config = { |
|
724
|
|
|
|
|
|
|
'string' => $_[0], |
|
725
|
|
|
|
|
|
|
}; |
|
726
|
|
|
|
|
|
|
} |
|
727
|
|
|
|
|
|
|
else { |
|
728
|
10
|
|
|
|
|
38
|
$config = { |
|
729
|
|
|
|
|
|
|
'file' => $_[0], |
|
730
|
|
|
|
|
|
|
}; |
|
731
|
|
|
|
|
|
|
} |
|
732
|
|
|
|
|
|
|
} |
|
733
|
|
|
|
|
|
|
} |
|
734
|
|
|
|
|
|
|
else { |
|
735
|
|
|
|
|
|
|
# args: hash |
|
736
|
42
|
|
|
|
|
170
|
$config = { @_ }; |
|
737
|
|
|
|
|
|
|
} |
|
738
|
|
|
|
|
|
|
} |
|
739
|
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
# set current configuration from base |
|
741
|
|
|
|
|
|
|
|
|
742
|
112
|
100
|
|
|
|
444
|
if (keys %$config) { |
|
743
|
58
|
|
|
|
|
2435
|
$self->{'current_config'} = Clone::clone($self->{'base_config'}); |
|
744
|
58
|
|
|
|
|
550
|
$self->_add_configuration($self->{'current_config'}, $config); |
|
745
|
|
|
|
|
|
|
} |
|
746
|
|
|
|
|
|
|
else { |
|
747
|
54
|
|
|
|
|
161
|
$self->{'current_config'} = $self->{'base_config'}; |
|
748
|
|
|
|
|
|
|
} |
|
749
|
|
|
|
|
|
|
|
|
750
|
112
|
|
|
|
|
305
|
my $plugin_config = $self->{'current_config'}{'plugin_config'}; |
|
751
|
|
|
|
|
|
|
|
|
752
|
112
|
|
|
|
|
186
|
my $return_references = 1; |
|
753
|
112
|
100
|
|
|
|
369
|
if (exists $plugin_config->{'return_references'}) { |
|
754
|
10
|
|
|
|
|
19
|
$return_references = $plugin_config->{'return_references'}; |
|
755
|
|
|
|
|
|
|
} |
|
756
|
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
# determine template type |
|
758
|
|
|
|
|
|
|
|
|
759
|
112
|
|
66
|
|
|
714
|
my $type = $plugin_config->{'type'} |
|
760
|
|
|
|
|
|
|
|| $plugin_config->{'default_type'} |
|
761
|
|
|
|
|
|
|
|| $self->_default_type; |
|
762
|
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
# require driver |
|
765
|
112
|
|
|
|
|
430
|
my $driver_class = $self->_require_driver($type); |
|
766
|
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
# Generate driver config and native config |
|
768
|
111
|
|
|
|
|
851
|
my %driver_config = $driver_class->default_driver_config; |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
# Copy all params with keys listed in the driver_config_keys |
|
771
|
111
|
|
|
|
|
587
|
foreach my $param ($driver_class->driver_config_keys) { |
|
772
|
525
|
100
|
|
|
|
1687
|
if (exists $self->{'current_config'}{'driver_config'}{$type}{$param}) { |
|
773
|
17
|
|
|
|
|
95
|
$driver_config{$param} = $self->{'current_config'}{'driver_config'}{$type}{$param}; |
|
774
|
|
|
|
|
|
|
} |
|
775
|
|
|
|
|
|
|
} |
|
776
|
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
# Copy whatever is left over into native config |
|
778
|
111
|
|
|
|
|
393
|
my $native_config = $self->{'current_config'}{'native_config'}{$type}; |
|
779
|
|
|
|
|
|
|
|
|
780
|
111
|
|
|
|
|
192
|
foreach my $param (keys %{ $self->{'current_config'}{'driver_config'}{$type} }) { |
|
|
111
|
|
|
|
|
510
|
|
|
781
|
|
|
|
|
|
|
# skip values copied into %driver_config |
|
782
|
17
|
50
|
|
|
|
258
|
if (not exists $driver_config{$param}) { |
|
783
|
0
|
|
|
|
|
0
|
$native_config->{$param} = $self->{'current_config'}{'driver_config'}{$type}{$param}; |
|
784
|
|
|
|
|
|
|
} |
|
785
|
|
|
|
|
|
|
} |
|
786
|
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
# Get the string, if supplied |
|
788
|
111
|
|
|
|
|
203
|
my $string_ref; |
|
789
|
111
|
100
|
|
|
|
404
|
if (exists $plugin_config->{'string'}) { |
|
790
|
8
|
|
50
|
|
|
30
|
$string_ref = $plugin_config->{'string'} || ''; |
|
791
|
8
|
50
|
|
|
|
29
|
$string_ref = \$string_ref unless ref $string_ref; |
|
792
|
|
|
|
|
|
|
} |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
# if no string, then guess template filename |
|
795
|
111
|
|
|
|
|
159
|
my $filename; |
|
796
|
111
|
100
|
|
|
|
379
|
unless ($string_ref) { |
|
797
|
103
|
|
|
|
|
637
|
$filename = $self->_guess_template_filename($plugin_config, \%driver_config, $type, $self->{'webapp'}->get_current_runmode); |
|
798
|
|
|
|
|
|
|
} |
|
799
|
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
# call load_tmpl hook |
|
802
|
111
|
|
|
|
|
184
|
my %tmpl_params; |
|
803
|
111
|
50
|
|
|
|
781
|
if ($self->{'webapp'}->can('call_hook')) { |
|
804
|
111
|
|
|
|
|
476
|
my %ht_params = ( |
|
805
|
|
|
|
|
|
|
'path' => $plugin_config->{'add_include_paths'}, |
|
806
|
|
|
|
|
|
|
); |
|
807
|
111
|
|
|
|
|
621
|
$self->{'webapp'}->new_hook('load_tmpl'); |
|
808
|
111
|
|
|
|
|
944
|
$self->{'webapp'}->call_hook( |
|
809
|
|
|
|
|
|
|
'load_tmpl', |
|
810
|
|
|
|
|
|
|
\%ht_params, |
|
811
|
|
|
|
|
|
|
\%tmpl_params, |
|
812
|
|
|
|
|
|
|
$filename, |
|
813
|
|
|
|
|
|
|
); |
|
814
|
111
|
|
|
|
|
5688
|
$plugin_config->{'add_include_paths'} = $ht_params{'path'}; |
|
815
|
|
|
|
|
|
|
} |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
# manage include paths |
|
819
|
|
|
|
|
|
|
|
|
820
|
111
|
|
|
|
|
371
|
my $include_paths = $self->_merged_include_paths($plugin_config); |
|
821
|
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
# create and initialize driver |
|
823
|
|
|
|
|
|
|
|
|
824
|
111
|
|
|
|
|
1306
|
my $driver = $driver_class->_new( |
|
825
|
|
|
|
|
|
|
'driver_config' => \%driver_config, |
|
826
|
|
|
|
|
|
|
'native_config' => $native_config, |
|
827
|
|
|
|
|
|
|
'include_paths' => $include_paths, |
|
828
|
|
|
|
|
|
|
'filename' => $filename, |
|
829
|
|
|
|
|
|
|
'string_ref' => $string_ref, |
|
830
|
|
|
|
|
|
|
'return_references' => $return_references, |
|
831
|
|
|
|
|
|
|
'callers_package' => $plugin_config->{'callers_package'}, |
|
832
|
|
|
|
|
|
|
'webapp' => $self->{'webapp'}, |
|
833
|
|
|
|
|
|
|
'conf_name' => $self->{'conf_name'}, |
|
834
|
|
|
|
|
|
|
'component_handler_class' => $plugin_config->{'component_handler_class'}, |
|
835
|
|
|
|
|
|
|
); |
|
836
|
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
# Store the tmpl params we picked up from the load_tmpl hook |
|
838
|
109
|
100
|
|
|
|
571
|
if (keys %tmpl_params) { |
|
839
|
2
|
|
|
|
|
28
|
$driver->param(%tmpl_params); |
|
840
|
|
|
|
|
|
|
} |
|
841
|
|
|
|
|
|
|
|
|
842
|
109
|
|
|
|
|
603
|
return $driver; |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
} |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
# These are the param keys of the plugin_config (see below) |
|
847
|
|
|
|
|
|
|
sub _plugin_config_keys { |
|
848
|
140
|
|
|
140
|
|
691
|
qw/ |
|
849
|
|
|
|
|
|
|
callers_package |
|
850
|
|
|
|
|
|
|
auto_add_template_extension |
|
851
|
|
|
|
|
|
|
default_type |
|
852
|
|
|
|
|
|
|
type |
|
853
|
|
|
|
|
|
|
include_paths |
|
854
|
|
|
|
|
|
|
add_include_paths |
|
855
|
|
|
|
|
|
|
file |
|
856
|
|
|
|
|
|
|
string |
|
857
|
|
|
|
|
|
|
component_handler_class |
|
858
|
|
|
|
|
|
|
return_references |
|
859
|
|
|
|
|
|
|
template_filename_generator |
|
860
|
|
|
|
|
|
|
/; |
|
861
|
|
|
|
|
|
|
} |
|
862
|
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
# This is the default plugin_config (see below) |
|
864
|
|
|
|
|
|
|
sub _default_plugin_config { |
|
865
|
|
|
|
|
|
|
( |
|
866
|
82
|
|
|
82
|
|
364
|
auto_add_template_extension => 1 |
|
867
|
|
|
|
|
|
|
); |
|
868
|
|
|
|
|
|
|
} |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
# Internally, the configuration is split into three separate sections: |
|
871
|
|
|
|
|
|
|
# |
|
872
|
|
|
|
|
|
|
# General |
|
873
|
|
|
|
|
|
|
# ------- |
|
874
|
|
|
|
|
|
|
# 'plugin' - configuration for CAP::AnyTemplate itself |
|
875
|
|
|
|
|
|
|
# - This includes keys specified in _plugin_config_keys() |
|
876
|
|
|
|
|
|
|
# |
|
877
|
|
|
|
|
|
|
# Per-driver |
|
878
|
|
|
|
|
|
|
# ---------- |
|
879
|
|
|
|
|
|
|
# 'driver' - configuration for the driver module |
|
880
|
|
|
|
|
|
|
# 'native' - configuration for the underlying template system module |
|
881
|
|
|
|
|
|
|
# |
|
882
|
|
|
|
|
|
|
# For instance: |
|
883
|
|
|
|
|
|
|
# |
|
884
|
|
|
|
|
|
|
# $self->template->config( |
|
885
|
|
|
|
|
|
|
# default_type => 'TemplateToolkit', # plugin config |
|
886
|
|
|
|
|
|
|
# auto_extension => 1, # plugin config |
|
887
|
|
|
|
|
|
|
# 'TemplateToolkit' => { |
|
888
|
|
|
|
|
|
|
# template_extension => '.html', # driver config |
|
889
|
|
|
|
|
|
|
# POST_CHOMP => 1, # native config |
|
890
|
|
|
|
|
|
|
# } |
|
891
|
|
|
|
|
|
|
# ); |
|
892
|
|
|
|
|
|
|
# |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
sub _clear_configuration { |
|
895
|
82
|
|
|
82
|
|
151
|
my ($self, $storage) = @_; |
|
896
|
|
|
|
|
|
|
|
|
897
|
82
|
|
|
|
|
278
|
$storage->{'plugin_config'} = { $self->_default_plugin_config }; |
|
898
|
82
|
|
|
|
|
204
|
$storage->{'driver_config'} = {}; |
|
899
|
82
|
|
|
|
|
214
|
$storage->{'native_config'} = {}; |
|
900
|
|
|
|
|
|
|
} |
|
901
|
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
sub _add_configuration { |
|
903
|
140
|
|
|
140
|
|
317
|
my ($self, $storage, $config) = @_; |
|
904
|
|
|
|
|
|
|
|
|
905
|
140
|
|
50
|
|
|
507
|
$storage->{'plugin_config'} ||= { $self->_default_plugin_config }; |
|
906
|
140
|
|
50
|
|
|
400
|
$storage->{'driver_config'} ||= {}; |
|
907
|
140
|
|
50
|
|
|
497
|
$storage->{'native_config'} ||= {}; |
|
908
|
|
|
|
|
|
|
|
|
909
|
140
|
|
|
|
|
446
|
foreach my $key ($self->_plugin_config_keys) { |
|
910
|
1540
|
100
|
|
|
|
3853
|
$storage->{'plugin_config'}{$key} = delete $config->{$key} if exists $config->{$key}; |
|
911
|
|
|
|
|
|
|
} |
|
912
|
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
# After we've removed the config keys for the 'plugin' |
|
914
|
|
|
|
|
|
|
# configuration, the only keys that should remain |
|
915
|
|
|
|
|
|
|
# are the names of drivers |
|
916
|
|
|
|
|
|
|
|
|
917
|
140
|
|
|
|
|
658
|
foreach my $driver (keys %$config) { |
|
918
|
|
|
|
|
|
|
|
|
919
|
95
|
|
|
|
|
289
|
my $module = $self->_require_driver($driver); |
|
920
|
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
# Start with a blank config |
|
922
|
95
|
|
100
|
|
|
521
|
$storage->{'driver_config'}{$driver} ||= {}; |
|
923
|
|
|
|
|
|
|
|
|
924
|
|
|
|
|
|
|
# add the module's default config |
|
925
|
95
|
|
|
|
|
767
|
my %default_config = $module->default_driver_config; |
|
926
|
|
|
|
|
|
|
|
|
927
|
95
|
|
|
|
|
306
|
foreach my $key (keys %default_config) { |
|
928
|
300
|
|
100
|
|
|
1131
|
$storage->{'driver_config'}{$key} ||= $default_config{$key}; |
|
929
|
|
|
|
|
|
|
} |
|
930
|
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
# add the config provided by the user |
|
932
|
|
|
|
|
|
|
# values of known driver config keys get put into driver_config |
|
933
|
95
|
|
|
|
|
456
|
foreach my $key ($module->driver_config_keys) { |
|
934
|
330
|
100
|
|
|
|
863
|
if (exists $config->{$driver}{$key}) { |
|
935
|
53
|
|
|
|
|
210
|
$storage->{'driver_config'}{$driver}{$key} = delete $config->{$driver}{$key}; |
|
936
|
|
|
|
|
|
|
} |
|
937
|
|
|
|
|
|
|
} |
|
938
|
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
# ... and the remaining keys get put into native_config |
|
940
|
95
|
|
|
|
|
170
|
foreach my $key (keys %{ $config->{$driver} }) { |
|
|
95
|
|
|
|
|
417
|
|
|
941
|
45
|
|
|
|
|
306
|
$storage->{'native_config'}{$driver}{$key} = $config->{$driver}{$key}; |
|
942
|
|
|
|
|
|
|
} |
|
943
|
|
|
|
|
|
|
} |
|
944
|
|
|
|
|
|
|
} |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
sub _merged_include_paths { |
|
947
|
111
|
|
|
111
|
|
183
|
my ($self, $config) = @_; |
|
948
|
|
|
|
|
|
|
|
|
949
|
111
|
|
|
|
|
198
|
my @include_paths; |
|
950
|
|
|
|
|
|
|
|
|
951
|
111
|
100
|
|
|
|
547
|
if (my $tmpl_path = $self->{'webapp'}->tmpl_path) { |
|
952
|
8
|
50
|
|
|
|
84
|
if (ref $tmpl_path ne 'ARRAY') { |
|
953
|
8
|
|
|
|
|
20
|
$tmpl_path = [ $tmpl_path ]; |
|
954
|
|
|
|
|
|
|
} |
|
955
|
8
|
|
|
|
|
19
|
push @include_paths, @$tmpl_path; |
|
956
|
|
|
|
|
|
|
} |
|
957
|
|
|
|
|
|
|
|
|
958
|
111
|
100
|
100
|
|
|
1675
|
if ($config->{'include_paths'} and ref $config->{'include_paths'} ne 'ARRAY') { |
|
959
|
79
|
|
|
|
|
229
|
$config->{'include_paths'} = [ $config->{'include_paths'} ]; |
|
960
|
|
|
|
|
|
|
} |
|
961
|
111
|
100
|
|
|
|
176
|
push @include_paths, @{ $config->{'include_paths'} || [] }; |
|
|
111
|
|
|
|
|
516
|
|
|
962
|
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
|
|
964
|
|
|
|
|
|
|
|
|
965
|
111
|
|
100
|
|
|
486
|
$config->{'add_include_paths'} ||= []; |
|
966
|
111
|
100
|
|
|
|
385
|
$config->{'add_include_paths'} = [$config->{'add_include_paths'}] unless ref $config->{'add_include_paths'} eq 'ARRAY'; |
|
967
|
|
|
|
|
|
|
|
|
968
|
111
|
|
|
|
|
152
|
unshift @include_paths, @{$config->{'add_include_paths'}}; |
|
|
111
|
|
|
|
|
228
|
|
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
# remove duplicates |
|
971
|
111
|
|
|
|
|
169
|
my %seen_include_path; |
|
972
|
|
|
|
|
|
|
my @unique_include_paths; |
|
973
|
111
|
|
|
|
|
205
|
foreach my $path (@include_paths) { |
|
974
|
127
|
50
|
|
|
|
387
|
next if $seen_include_path{$path}; |
|
975
|
127
|
|
|
|
|
292
|
$seen_include_path{$path} = 1; |
|
976
|
127
|
|
|
|
|
346
|
push @unique_include_paths, $path; |
|
977
|
|
|
|
|
|
|
} |
|
978
|
|
|
|
|
|
|
|
|
979
|
111
|
50
|
|
|
|
372
|
return @unique_include_paths if wantarray; |
|
980
|
111
|
|
|
|
|
398
|
return \@unique_include_paths; |
|
981
|
|
|
|
|
|
|
} |
|
982
|
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
# Finds a template driver beneath the namespace of the current package |
|
984
|
|
|
|
|
|
|
# followed by '::Driver::'. Requires this module and returns its package |
|
985
|
|
|
|
|
|
|
# name |
|
986
|
|
|
|
|
|
|
# |
|
987
|
|
|
|
|
|
|
# |
|
988
|
|
|
|
|
|
|
# For instance: |
|
989
|
|
|
|
|
|
|
# $module = _require_driver('HTMLTemplate'); |
|
990
|
|
|
|
|
|
|
# print $module; # 'CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate' |
|
991
|
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
sub _require_driver { |
|
993
|
207
|
|
|
207
|
|
357
|
my ($self, $driver) = @_; |
|
994
|
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
# only allow word characters and colons |
|
996
|
207
|
50
|
|
|
|
815
|
if ($driver =~ /[^\w:]/) { |
|
997
|
0
|
|
|
|
|
0
|
croak "CAP::AnyTemplate: Illegal template driver name: $driver\n"; |
|
998
|
|
|
|
|
|
|
} |
|
999
|
|
|
|
|
|
|
|
|
1000
|
207
|
|
|
|
|
630
|
my $module = (ref $self) . '::Driver::' . $driver; |
|
1001
|
|
|
|
|
|
|
|
|
1002
|
207
|
|
|
|
|
18536
|
eval "require $module"; |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
207
|
100
|
|
|
|
998
|
if ($@) { |
|
1005
|
1
|
|
|
|
|
264
|
croak "CAP::AnyTemplate: template driver $module could not be found: $@"; |
|
1006
|
|
|
|
|
|
|
} |
|
1007
|
206
|
|
|
|
|
513
|
return $module; |
|
1008
|
|
|
|
|
|
|
} |
|
1009
|
|
|
|
|
|
|
|
|
1010
|
|
|
|
|
|
|
sub _guess_template_filename { |
|
1011
|
103
|
|
|
103
|
|
743
|
my ($self, $plugin_config, $driver_config, $type, $crm) = @_; |
|
1012
|
|
|
|
|
|
|
|
|
1013
|
|
|
|
|
|
|
|
|
1014
|
103
|
|
|
|
|
148
|
my $filename; |
|
1015
|
103
|
100
|
|
|
|
361
|
if (exists $plugin_config->{'file'}) { |
|
1016
|
24
|
|
|
|
|
58
|
$filename = $plugin_config->{'file'}; |
|
1017
|
|
|
|
|
|
|
} |
|
1018
|
|
|
|
|
|
|
else { |
|
1019
|
|
|
|
|
|
|
# filename set to current run mode |
|
1020
|
79
|
|
|
|
|
120
|
$filename = $crm; |
|
1021
|
|
|
|
|
|
|
|
|
1022
|
79
|
100
|
|
|
|
311
|
if (ref $plugin_config->{'template_filename_generator'} eq 'CODE') { |
|
1023
|
4
|
|
|
|
|
21
|
$filename = $plugin_config->{'template_filename_generator'}->($self->{'webapp'}); |
|
1024
|
|
|
|
|
|
|
} |
|
1025
|
|
|
|
|
|
|
} |
|
1026
|
|
|
|
|
|
|
|
|
1027
|
103
|
100
|
|
|
|
383
|
if ($plugin_config->{'auto_add_template_extension'}) { |
|
1028
|
|
|
|
|
|
|
|
|
1029
|
|
|
|
|
|
|
# add extension |
|
1030
|
89
|
|
66
|
|
|
344
|
my $extension = $driver_config->{'template_extension'} |
|
1031
|
|
|
|
|
|
|
|| $self->_default_extension; |
|
1032
|
|
|
|
|
|
|
|
|
1033
|
89
|
|
|
|
|
197
|
$filename = $filename . $extension; |
|
1034
|
|
|
|
|
|
|
} |
|
1035
|
|
|
|
|
|
|
|
|
1036
|
103
|
|
|
|
|
262
|
return $filename; |
|
1037
|
|
|
|
|
|
|
} |
|
1038
|
|
|
|
|
|
|
|
|
1039
|
|
|
|
|
|
|
=head2 fill |
|
1040
|
|
|
|
|
|
|
|
|
1041
|
|
|
|
|
|
|
Fill is a convenience method which in a single step creates the |
|
1042
|
|
|
|
|
|
|
template, fills it with the template paramters and returns its output. |
|
1043
|
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
You can call it with or without a filename (or string ref). |
|
1045
|
|
|
|
|
|
|
|
|
1046
|
|
|
|
|
|
|
The code: |
|
1047
|
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
$self->template->fill('filename', \%params); |
|
1049
|
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
is equivalent to: |
|
1051
|
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
my $template = $self->template->load('filename'); |
|
1053
|
|
|
|
|
|
|
$template->output(\%params); |
|
1054
|
|
|
|
|
|
|
|
|
1055
|
|
|
|
|
|
|
|
|
1056
|
|
|
|
|
|
|
And the code: |
|
1057
|
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
$self->template->fill(\$some_text, \%params); |
|
1059
|
|
|
|
|
|
|
|
|
1060
|
|
|
|
|
|
|
is equivalent to: |
|
1061
|
|
|
|
|
|
|
|
|
1062
|
|
|
|
|
|
|
my $template = $self->template->load(\$some_text); |
|
1063
|
|
|
|
|
|
|
$template->output(\%params); |
|
1064
|
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
And the code: |
|
1066
|
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
$self->template->fill(\%params); |
|
1068
|
|
|
|
|
|
|
|
|
1069
|
|
|
|
|
|
|
is equivalent to: |
|
1070
|
|
|
|
|
|
|
|
|
1071
|
|
|
|
|
|
|
my $template = $self->template->load; |
|
1072
|
|
|
|
|
|
|
$template->output(\%params); |
|
1073
|
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
And the code: |
|
1075
|
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
$self->template->fill('filename'); |
|
1077
|
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
is equivalent to: |
|
1079
|
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
my $template = $self->template->load('filename'); |
|
1081
|
|
|
|
|
|
|
$template->output; |
|
1082
|
|
|
|
|
|
|
|
|
1083
|
|
|
|
|
|
|
And the code: |
|
1084
|
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
$self->template->fill(\$some_text); |
|
1086
|
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
is equivalent to: |
|
1088
|
|
|
|
|
|
|
|
|
1089
|
|
|
|
|
|
|
my $template = $self->template->load(\$some_text); |
|
1090
|
|
|
|
|
|
|
$template->output; |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
And the code: |
|
1093
|
|
|
|
|
|
|
|
|
1094
|
|
|
|
|
|
|
$self->template->fill; |
|
1095
|
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
is equivalent to: |
|
1097
|
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
my $template = $self->template->load; |
|
1099
|
|
|
|
|
|
|
$template->output; |
|
1100
|
|
|
|
|
|
|
|
|
1101
|
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
=cut |
|
1103
|
|
|
|
|
|
|
|
|
1104
|
|
|
|
|
|
|
sub fill { |
|
1105
|
14
|
|
|
14
|
1
|
30
|
my $self = shift; |
|
1106
|
|
|
|
|
|
|
|
|
1107
|
14
|
|
|
|
|
23
|
my ($file_or_string, $params, $template); |
|
1108
|
|
|
|
|
|
|
|
|
1109
|
14
|
100
|
|
|
|
64
|
if (@_ == 2) { # two params, first is filename |
|
|
|
100
|
|
|
|
|
|
|
1110
|
4
|
|
|
|
|
10
|
($file_or_string, $params) = @_; |
|
1111
|
4
|
|
|
|
|
16
|
$template = $self->load($file_or_string); |
|
1112
|
|
|
|
|
|
|
} |
|
1113
|
|
|
|
|
|
|
elsif (@_ == 1) { # one param, first is param, filename or scallarref |
|
1114
|
8
|
100
|
100
|
|
|
63
|
if (ref $_[0] and ref $_[0] eq 'HASH') { # single param is param ref |
|
1115
|
4
|
|
|
|
|
9
|
($params) = @_; |
|
1116
|
4
|
|
|
|
|
17
|
$template = $self->load; |
|
1117
|
|
|
|
|
|
|
} |
|
1118
|
|
|
|
|
|
|
else { # single string param is filename or scalarref |
|
1119
|
4
|
|
|
|
|
10
|
($file_or_string) = @_; |
|
1120
|
4
|
|
|
|
|
15
|
$template = $self->load($file_or_string); |
|
1121
|
|
|
|
|
|
|
} |
|
1122
|
|
|
|
|
|
|
} |
|
1123
|
|
|
|
|
|
|
else { |
|
1124
|
2
|
|
|
|
|
7
|
$template = $self->load; |
|
1125
|
|
|
|
|
|
|
} |
|
1126
|
|
|
|
|
|
|
|
|
1127
|
14
|
|
100
|
|
|
63
|
$params ||= {}; |
|
1128
|
|
|
|
|
|
|
|
|
1129
|
14
|
|
|
|
|
106
|
$template->output($params); |
|
1130
|
|
|
|
|
|
|
} |
|
1131
|
|
|
|
|
|
|
|
|
1132
|
|
|
|
|
|
|
=head2 process |
|
1133
|
|
|
|
|
|
|
|
|
1134
|
|
|
|
|
|
|
C<"process"> is an alias for L<"fill">. |
|
1135
|
|
|
|
|
|
|
|
|
1136
|
|
|
|
|
|
|
=cut |
|
1137
|
|
|
|
|
|
|
|
|
1138
|
|
|
|
|
|
|
sub process { |
|
1139
|
2
|
|
|
2
|
1
|
9
|
goto &fill; |
|
1140
|
|
|
|
|
|
|
} |
|
1141
|
|
|
|
|
|
|
|
|
1142
|
|
|
|
|
|
|
=head1 APPLICATION METHODS |
|
1143
|
|
|
|
|
|
|
|
|
1144
|
|
|
|
|
|
|
These methods are called directly on your application's C<$self> object. |
|
1145
|
|
|
|
|
|
|
|
|
1146
|
|
|
|
|
|
|
=head2 load_tmpl |
|
1147
|
|
|
|
|
|
|
|
|
1148
|
|
|
|
|
|
|
This is an emulation of L's built-in C |
|
1149
|
|
|
|
|
|
|
method. For instance: |
|
1150
|
|
|
|
|
|
|
|
|
1151
|
|
|
|
|
|
|
$self->load_tmpl('some_template.html'); |
|
1152
|
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
It is not exported by default. To enable it, use: |
|
1154
|
|
|
|
|
|
|
|
|
1155
|
|
|
|
|
|
|
use CGI::Application::Plugin::AnyTemplate qw/:load_tmpl/; |
|
1156
|
|
|
|
|
|
|
|
|
1157
|
|
|
|
|
|
|
You can call it the same way as documented in C and it |
|
1158
|
|
|
|
|
|
|
will have the same effect. However, it will respect the current |
|
1159
|
|
|
|
|
|
|
template C, so you can still use it to fill templates of different |
|
1160
|
|
|
|
|
|
|
backends. |
|
1161
|
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
The idea is that you can take an existing L-based |
|
1163
|
|
|
|
|
|
|
webapp which uses C templates, and add the following |
|
1164
|
|
|
|
|
|
|
code to it: |
|
1165
|
|
|
|
|
|
|
|
|
1166
|
|
|
|
|
|
|
use CGI::Application::Plugin::AnyTemplate qw/:load_tmpl/; |
|
1167
|
|
|
|
|
|
|
|
|
1168
|
|
|
|
|
|
|
sub setup { |
|
1169
|
|
|
|
|
|
|
my $self = shift; |
|
1170
|
|
|
|
|
|
|
$self->template->config(type => TemplateToolkit); |
|
1171
|
|
|
|
|
|
|
} |
|
1172
|
|
|
|
|
|
|
|
|
1173
|
|
|
|
|
|
|
This will change all existing calls to load_tmpl within your application |
|
1174
|
|
|
|
|
|
|
to use L based templates. |
|
1175
|
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
Calling: |
|
1177
|
|
|
|
|
|
|
|
|
1178
|
|
|
|
|
|
|
my $template = $self->load_tmpl('some_template.html'); |
|
1179
|
|
|
|
|
|
|
|
|
1180
|
|
|
|
|
|
|
It is the equivalent of calling: |
|
1181
|
|
|
|
|
|
|
|
|
1182
|
|
|
|
|
|
|
my $template = $self->template->load( |
|
1183
|
|
|
|
|
|
|
file => 'some_template.html', |
|
1184
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
1185
|
|
|
|
|
|
|
); |
|
1186
|
|
|
|
|
|
|
|
|
1187
|
|
|
|
|
|
|
If you add extra options to C, these will be assumed to be |
|
1188
|
|
|
|
|
|
|
L specific options, with the exception of the C |
|
1189
|
|
|
|
|
|
|
option, which will be extracted and used as 'add_include_paths': |
|
1190
|
|
|
|
|
|
|
|
|
1191
|
|
|
|
|
|
|
my $template = $self->load_tmpl('some_template.html', |
|
1192
|
|
|
|
|
|
|
cache => 0, |
|
1193
|
|
|
|
|
|
|
path => '/path/to/templates', |
|
1194
|
|
|
|
|
|
|
); |
|
1195
|
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
This will get translated into: |
|
1197
|
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
my $template = $self->template->load( |
|
1199
|
|
|
|
|
|
|
file => 'some_template.html', |
|
1200
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
1201
|
|
|
|
|
|
|
add_include_paths => '/path/to/templates', |
|
1202
|
|
|
|
|
|
|
HTMLTemplate => { |
|
1203
|
|
|
|
|
|
|
cache => 0, |
|
1204
|
|
|
|
|
|
|
} |
|
1205
|
|
|
|
|
|
|
); |
|
1206
|
|
|
|
|
|
|
|
|
1207
|
|
|
|
|
|
|
Note that if you specify any L-specific options here, |
|
1208
|
|
|
|
|
|
|
they will completely overwrite any options that you passed to config. |
|
1209
|
|
|
|
|
|
|
|
|
1210
|
|
|
|
|
|
|
Some notes and caveats about using the C method: |
|
1211
|
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
=over 4 |
|
1213
|
|
|
|
|
|
|
|
|
1214
|
|
|
|
|
|
|
=item * |
|
1215
|
|
|
|
|
|
|
|
|
1216
|
|
|
|
|
|
|
This method only works for the default template configuration (i.e. C<< $self->template() >>). |
|
1217
|
|
|
|
|
|
|
If you set up a named configuration (e.g. C<< $self->template('myconfig') >>) |
|
1218
|
|
|
|
|
|
|
there is no way to access it with C. Since plugins should be |
|
1219
|
|
|
|
|
|
|
using named configurations, this means that the C method |
|
1220
|
|
|
|
|
|
|
should not be used by plugins. See L<"NOTES FOR AUTHORS OF PLUGINS AND REUSABLE APPLICATIONS">, |
|
1221
|
|
|
|
|
|
|
below. |
|
1222
|
|
|
|
|
|
|
|
|
1223
|
|
|
|
|
|
|
=item * |
|
1224
|
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
The C method does not automatically add an extension to the |
|
1226
|
|
|
|
|
|
|
filename you pass to it, even if you have C |
|
1227
|
|
|
|
|
|
|
set to a true value in your call to C<< $self->template->config >>. |
|
1228
|
|
|
|
|
|
|
|
|
1229
|
|
|
|
|
|
|
=item * |
|
1230
|
|
|
|
|
|
|
|
|
1231
|
|
|
|
|
|
|
The C method ignores always returns a string, not a reference to a |
|
1232
|
|
|
|
|
|
|
string. It ignores the setting of the C option. |
|
1233
|
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
=back |
|
1235
|
|
|
|
|
|
|
|
|
1236
|
|
|
|
|
|
|
=cut |
|
1237
|
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
sub load_tmpl { |
|
1239
|
8
|
|
|
8
|
1
|
31320
|
my ($self, $filename, %ht_options) = @_; |
|
1240
|
|
|
|
|
|
|
|
|
1241
|
8
|
|
|
|
|
45
|
my %params = ( |
|
1242
|
|
|
|
|
|
|
file => $filename, |
|
1243
|
|
|
|
|
|
|
auto_add_template_extension => 0, |
|
1244
|
|
|
|
|
|
|
HTMLTemplate => \%ht_options, |
|
1245
|
|
|
|
|
|
|
); |
|
1246
|
|
|
|
|
|
|
|
|
1247
|
8
|
|
|
|
|
23
|
my $path = delete $ht_options{'path'}; |
|
1248
|
|
|
|
|
|
|
|
|
1249
|
8
|
100
|
|
|
|
92
|
if ($path) { |
|
1250
|
2
|
|
|
|
|
6
|
$params{'add_include_paths'} = $path; |
|
1251
|
|
|
|
|
|
|
} |
|
1252
|
8
|
|
|
|
|
18
|
$params{'return_references'} = undef; |
|
1253
|
|
|
|
|
|
|
|
|
1254
|
8
|
|
|
|
|
30
|
return $self->template->load(%params); |
|
1255
|
|
|
|
|
|
|
} |
|
1256
|
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
=head2 tmpl_path |
|
1258
|
|
|
|
|
|
|
|
|
1259
|
|
|
|
|
|
|
You can set the template C by calling |
|
1260
|
|
|
|
|
|
|
C<< $self->tmpl_path('/path/to/templates') >>. |
|
1261
|
|
|
|
|
|
|
|
|
1262
|
|
|
|
|
|
|
You can also do so by passing a value to the C parameter to |
|
1263
|
|
|
|
|
|
|
your application's C method: |
|
1264
|
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
my $webapp = App->new( |
|
1266
|
|
|
|
|
|
|
TMPL_PATH => '/path/to/templates', |
|
1267
|
|
|
|
|
|
|
); |
|
1268
|
|
|
|
|
|
|
|
|
1269
|
|
|
|
|
|
|
Paths that you set via C/C will be put B in |
|
1270
|
|
|
|
|
|
|
the list of include paths, after C and |
|
1271
|
|
|
|
|
|
|
C. |
|
1272
|
|
|
|
|
|
|
|
|
1273
|
|
|
|
|
|
|
=head1 DRIVER METHODS |
|
1274
|
|
|
|
|
|
|
|
|
1275
|
|
|
|
|
|
|
These are the most commonly used methods of the C driver |
|
1276
|
|
|
|
|
|
|
object. The driver is what you get back from calling C<< $self->template->load >>. |
|
1277
|
|
|
|
|
|
|
|
|
1278
|
|
|
|
|
|
|
=head2 param |
|
1279
|
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
The C method gets and sets values within the template. |
|
1281
|
|
|
|
|
|
|
|
|
1282
|
|
|
|
|
|
|
my $template = $self->template->load; |
|
1283
|
|
|
|
|
|
|
|
|
1284
|
|
|
|
|
|
|
my @param_names = $template->param(); |
|
1285
|
|
|
|
|
|
|
|
|
1286
|
|
|
|
|
|
|
my $value = $template->param('name'); |
|
1287
|
|
|
|
|
|
|
|
|
1288
|
|
|
|
|
|
|
$template->param('name' => 'value'); |
|
1289
|
|
|
|
|
|
|
$template->param( |
|
1290
|
|
|
|
|
|
|
'name1' => 'value1', |
|
1291
|
|
|
|
|
|
|
'name2' => 'value2' |
|
1292
|
|
|
|
|
|
|
); |
|
1293
|
|
|
|
|
|
|
|
|
1294
|
|
|
|
|
|
|
It is designed to behave similarly to the C method in other modules like |
|
1295
|
|
|
|
|
|
|
L and L. |
|
1296
|
|
|
|
|
|
|
|
|
1297
|
|
|
|
|
|
|
=head2 get_param_hash |
|
1298
|
|
|
|
|
|
|
|
|
1299
|
|
|
|
|
|
|
Returns the template variables as a hash of names and values. |
|
1300
|
|
|
|
|
|
|
|
|
1301
|
|
|
|
|
|
|
my %params = $self->template->get_param_hash; |
|
1302
|
|
|
|
|
|
|
|
|
1303
|
|
|
|
|
|
|
In a scalar context, returns a reference to the hash used |
|
1304
|
|
|
|
|
|
|
internally to contain the values: |
|
1305
|
|
|
|
|
|
|
|
|
1306
|
|
|
|
|
|
|
my $params_ref = $self->template->get_param_hash; |
|
1307
|
|
|
|
|
|
|
|
|
1308
|
|
|
|
|
|
|
$params_ref->{'foo'} = 'bar'; # directly change parameter 'foo' |
|
1309
|
|
|
|
|
|
|
|
|
1310
|
|
|
|
|
|
|
=head2 output |
|
1311
|
|
|
|
|
|
|
|
|
1312
|
|
|
|
|
|
|
Returns the template with all the values filled in. |
|
1313
|
|
|
|
|
|
|
|
|
1314
|
|
|
|
|
|
|
return $template->output; |
|
1315
|
|
|
|
|
|
|
|
|
1316
|
|
|
|
|
|
|
You can also supply names and values to the template at this stage: |
|
1317
|
|
|
|
|
|
|
|
|
1318
|
|
|
|
|
|
|
return $template->output('name' => 'value', 'name2' => 'value2'); |
|
1319
|
|
|
|
|
|
|
|
|
1320
|
|
|
|
|
|
|
If C option is set to true, then the return value |
|
1321
|
|
|
|
|
|
|
of C |
|
1322
|
|
|
|
|
|
|
C option is false, then a copy of the string will be |
|
1323
|
|
|
|
|
|
|
returned. By default C is true. |
|
1324
|
|
|
|
|
|
|
|
|
1325
|
|
|
|
|
|
|
When you call the C |
|
1326
|
|
|
|
|
|
|
template are run. See L<"EMBEDDED COMPONENTS">, below. |
|
1327
|
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
=head1 PRE- AND POST- PROCESS |
|
1329
|
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
There are several ways to customize the template process. You can |
|
1331
|
|
|
|
|
|
|
modify the template parameters before the template is filled, and you |
|
1332
|
|
|
|
|
|
|
can modify the output of the template after it has been filled. |
|
1333
|
|
|
|
|
|
|
|
|
1334
|
|
|
|
|
|
|
Multiple applications and plugins can hook into the template process |
|
1335
|
|
|
|
|
|
|
pipeline, each making changes to the template input and output. |
|
1336
|
|
|
|
|
|
|
|
|
1337
|
|
|
|
|
|
|
For instance, it will be possible to make a general-purpose |
|
1338
|
|
|
|
|
|
|
C plugin that adds arbitrary data to each new |
|
1339
|
|
|
|
|
|
|
template (such as query parameters or configuration data). |
|
1340
|
|
|
|
|
|
|
|
|
1341
|
|
|
|
|
|
|
Note that the API has changed for version 0.10 in a |
|
1342
|
|
|
|
|
|
|
non-backwards-compatible way in order to use the new hook system |
|
1343
|
|
|
|
|
|
|
provided by recent versions of C. |
|
1344
|
|
|
|
|
|
|
|
|
1345
|
|
|
|
|
|
|
=head2 The load_tmpl hook |
|
1346
|
|
|
|
|
|
|
|
|
1347
|
|
|
|
|
|
|
The C hook is designed to be compatible with the C |
|
1348
|
|
|
|
|
|
|
hook defined by C itself. |
|
1349
|
|
|
|
|
|
|
|
|
1350
|
|
|
|
|
|
|
The C hook is called before the template object is created. |
|
1351
|
|
|
|
|
|
|
Any callbacks that you register to this hook will be called before each |
|
1352
|
|
|
|
|
|
|
template is loaded. Register a C callback with: |
|
1353
|
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
$self->add_callback('load_tmpl',\&my_load_tmpl); |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
When the C callback is executed it will be passed three |
|
1357
|
|
|
|
|
|
|
arguments (I L I): |
|
1358
|
|
|
|
|
|
|
|
|
1359
|
|
|
|
|
|
|
1. A hash reference of the extra params passed into C |
|
1360
|
|
|
|
|
|
|
(ignored by AnyTemplate with the exception of 'path') |
|
1361
|
|
|
|
|
|
|
|
|
1362
|
|
|
|
|
|
|
2. Followed by a hash reference to template parameters. |
|
1363
|
|
|
|
|
|
|
You can modify this hash by reference to affect values that are |
|
1364
|
|
|
|
|
|
|
actually passed to the param() method of the template object. |
|
1365
|
|
|
|
|
|
|
|
|
1366
|
|
|
|
|
|
|
3. The name of the template file. |
|
1367
|
|
|
|
|
|
|
|
|
1368
|
|
|
|
|
|
|
Here's an example stub for a load_tmpl() callback: |
|
1369
|
|
|
|
|
|
|
|
|
1370
|
|
|
|
|
|
|
sub my_load_tmpl_callback { |
|
1371
|
|
|
|
|
|
|
my ($self, $ht_params, $tmpl_params, $tmpl_file) = @_; |
|
1372
|
|
|
|
|
|
|
# modify $tmpl_params by reference... |
|
1373
|
|
|
|
|
|
|
} |
|
1374
|
|
|
|
|
|
|
|
|
1375
|
|
|
|
|
|
|
Currently, of all the params in C<$ht_params>, all but 'path' are |
|
1376
|
|
|
|
|
|
|
ignored, because these are specific to C. If you want to |
|
1377
|
|
|
|
|
|
|
write a generic callback that needs to be able to access or modify |
|
1378
|
|
|
|
|
|
|
C parameters then let me know, or add a feature request |
|
1379
|
|
|
|
|
|
|
on L. |
|
1380
|
|
|
|
|
|
|
|
|
1381
|
|
|
|
|
|
|
The C param of C<$ht_params> is initially set to the value of |
|
1382
|
|
|
|
|
|
|
C (if set). Your callback can modify the C |
|
1383
|
|
|
|
|
|
|
param, and C will be set to the result. |
|
1384
|
|
|
|
|
|
|
|
|
1385
|
|
|
|
|
|
|
Plugin authors who want to provide template processing features are |
|
1386
|
|
|
|
|
|
|
encouraged to use the 'load_tmpl' hook when possible, since it will work |
|
1387
|
|
|
|
|
|
|
both with AnyTemplate and with L's built-in |
|
1388
|
|
|
|
|
|
|
C. |
|
1389
|
|
|
|
|
|
|
|
|
1390
|
|
|
|
|
|
|
=head2 The template_pre_process and template_post_process hooks |
|
1391
|
|
|
|
|
|
|
|
|
1392
|
|
|
|
|
|
|
Before the template output is generated, the C<< template_pre_process >> |
|
1393
|
|
|
|
|
|
|
hook is called. Any callbacks that you register to this hook will be |
|
1394
|
|
|
|
|
|
|
called before each template is processed. Register a |
|
1395
|
|
|
|
|
|
|
C callback as follows: |
|
1396
|
|
|
|
|
|
|
|
|
1397
|
|
|
|
|
|
|
$self->add_callback('template_pre_process', \&my_tmpl_pre_process); |
|
1398
|
|
|
|
|
|
|
|
|
1399
|
|
|
|
|
|
|
Pre-process callbacks will be passed a reference to the C<$template> |
|
1400
|
|
|
|
|
|
|
object, and can can modify the parameters passed into the template by |
|
1401
|
|
|
|
|
|
|
using the C method: |
|
1402
|
|
|
|
|
|
|
|
|
1403
|
|
|
|
|
|
|
sub my_tmpl_pre_process { |
|
1404
|
|
|
|
|
|
|
my ($self, $template) = @_; |
|
1405
|
|
|
|
|
|
|
|
|
1406
|
|
|
|
|
|
|
# Change the internal template parameters by reference |
|
1407
|
|
|
|
|
|
|
my $params = $template->get_param_hash; |
|
1408
|
|
|
|
|
|
|
|
|
1409
|
|
|
|
|
|
|
foreach my $key (keys %$params) { |
|
1410
|
|
|
|
|
|
|
$params{$key} = to_piglatin($params{$key}); |
|
1411
|
|
|
|
|
|
|
} |
|
1412
|
|
|
|
|
|
|
|
|
1413
|
|
|
|
|
|
|
# Can also set values using the param method |
|
1414
|
|
|
|
|
|
|
$template->param('foo', 'bar'); |
|
1415
|
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
} |
|
1417
|
|
|
|
|
|
|
|
|
1418
|
|
|
|
|
|
|
|
|
1419
|
|
|
|
|
|
|
After the template output is generated, the C hook is called. |
|
1420
|
|
|
|
|
|
|
You can register a C callback as follows: |
|
1421
|
|
|
|
|
|
|
|
|
1422
|
|
|
|
|
|
|
$self->add_callback('template_post_process', \&my_tmpl_post_process); |
|
1423
|
|
|
|
|
|
|
|
|
1424
|
|
|
|
|
|
|
Any callbacks that you register to this hook will be called after each |
|
1425
|
|
|
|
|
|
|
template is processed, and will be passed both a reference to the |
|
1426
|
|
|
|
|
|
|
template object and a reference to the output generated by the template. |
|
1427
|
|
|
|
|
|
|
This allows you to modify the output of the template: |
|
1428
|
|
|
|
|
|
|
|
|
1429
|
|
|
|
|
|
|
sub my_tmpl_post_process { |
|
1430
|
|
|
|
|
|
|
my ($self, $template, $output_ref) = @_; |
|
1431
|
|
|
|
|
|
|
|
|
1432
|
|
|
|
|
|
|
$$output_ref =~ s/foo/bar/; |
|
1433
|
|
|
|
|
|
|
} |
|
1434
|
|
|
|
|
|
|
|
|
1435
|
|
|
|
|
|
|
|
|
1436
|
|
|
|
|
|
|
|
|
1437
|
|
|
|
|
|
|
=head1 EMBEDDED COMPONENTS |
|
1438
|
|
|
|
|
|
|
|
|
1439
|
|
|
|
|
|
|
=head2 Introduction |
|
1440
|
|
|
|
|
|
|
|
|
1441
|
|
|
|
|
|
|
C allows you to include application |
|
1442
|
|
|
|
|
|
|
components within your templates. |
|
1443
|
|
|
|
|
|
|
|
|
1444
|
|
|
|
|
|
|
For instance, you might include a I component a the top of every |
|
1445
|
|
|
|
|
|
|
page and a I |
|
1446
|
|
|
|
|
|
|
|
|
1447
|
|
|
|
|
|
|
These componenets are actually first-class run modes. When the template |
|
1448
|
|
|
|
|
|
|
engine finds a special tag marking an embedded component, it passes |
|
1449
|
|
|
|
|
|
|
control to the run mode of that name. That run mode can then do |
|
1450
|
|
|
|
|
|
|
whatever a normal run mode could do. But typically it will load its own |
|
1451
|
|
|
|
|
|
|
template and return the template's output. |
|
1452
|
|
|
|
|
|
|
|
|
1453
|
|
|
|
|
|
|
This output returned from the embedded run mode is inserted into the |
|
1454
|
|
|
|
|
|
|
containing template. |
|
1455
|
|
|
|
|
|
|
|
|
1456
|
|
|
|
|
|
|
The syntax for embed components is specific to each type of template |
|
1457
|
|
|
|
|
|
|
driver. |
|
1458
|
|
|
|
|
|
|
|
|
1459
|
|
|
|
|
|
|
=head2 Syntax |
|
1460
|
|
|
|
|
|
|
|
|
1461
|
|
|
|
|
|
|
L syntax: |
|
1462
|
|
|
|
|
|
|
|
|
1463
|
|
|
|
|
|
|
|
|
1464
|
|
|
|
|
|
|
|
|
1465
|
|
|
|
|
|
|
L syntax: |
|
1466
|
|
|
|
|
|
|
|
|
1467
|
|
|
|
|
|
|
|
|
1468
|
|
|
|
|
|
|
|
|
1469
|
|
|
|
|
|
|
L syntax: |
|
1470
|
|
|
|
|
|
|
|
|
1471
|
|
|
|
|
|
|
|
|
1472
|
|
|
|
|
|
|
|
|
1473
|
|
|
|
|
|
|
L syntax: |
|
1474
|
|
|
|
|
|
|
|
|
1475
|
|
|
|
|
|
|
[% CGIAPP.embed("some_run_mode") %] |
|
1476
|
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
L syntax: |
|
1478
|
|
|
|
|
|
|
|
|
1479
|
|
|
|
|
|
|
|
|
1480
|
|
|
|
|
|
|
this text gets replaced by the output of some_run_mode |
|
1481
|
|
|
|
|
|
|
|
|
1482
|
|
|
|
|
|
|
|
|
1483
|
|
|
|
|
|
|
=head2 Getting Template Variables from the Containing Template |
|
1484
|
|
|
|
|
|
|
|
|
1485
|
|
|
|
|
|
|
The component run mode is passed a reference to the template object that |
|
1486
|
|
|
|
|
|
|
contained the component. The component run mode can use this object |
|
1487
|
|
|
|
|
|
|
to access the params that were passed to the containing template. |
|
1488
|
|
|
|
|
|
|
|
|
1489
|
|
|
|
|
|
|
For instance: |
|
1490
|
|
|
|
|
|
|
|
|
1491
|
|
|
|
|
|
|
sub header { |
|
1492
|
|
|
|
|
|
|
my ($self, $containing_template, @other_params) = @_; |
|
1493
|
|
|
|
|
|
|
|
|
1494
|
|
|
|
|
|
|
my %tmplvars = ( |
|
1495
|
|
|
|
|
|
|
'title' => 'My glorious home page', |
|
1496
|
|
|
|
|
|
|
); |
|
1497
|
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
my $template = $self->template->load; |
|
1499
|
|
|
|
|
|
|
|
|
1500
|
|
|
|
|
|
|
$template->param(%tmplvars, $containing_template->get_param_hash); |
|
1501
|
|
|
|
|
|
|
return $template->output; |
|
1502
|
|
|
|
|
|
|
} |
|
1503
|
|
|
|
|
|
|
|
|
1504
|
|
|
|
|
|
|
In this example, the template values of the enclosing template would |
|
1505
|
|
|
|
|
|
|
override any values set by the embedded component. |
|
1506
|
|
|
|
|
|
|
|
|
1507
|
|
|
|
|
|
|
=head2 Passing Parameters |
|
1508
|
|
|
|
|
|
|
|
|
1509
|
|
|
|
|
|
|
The template can pass parameters to the target run mode. These are |
|
1510
|
|
|
|
|
|
|
passed in after the reference to the containing template object. |
|
1511
|
|
|
|
|
|
|
|
|
1512
|
|
|
|
|
|
|
Parameters can either be literal strings, specified within the template |
|
1513
|
|
|
|
|
|
|
text, or they can be keys that will be looked up in the template's |
|
1514
|
|
|
|
|
|
|
params. |
|
1515
|
|
|
|
|
|
|
|
|
1516
|
|
|
|
|
|
|
Literal strings are enclosed in double or single quotes. Param keys are |
|
1517
|
|
|
|
|
|
|
barewords. |
|
1518
|
|
|
|
|
|
|
|
|
1519
|
|
|
|
|
|
|
L syntax: |
|
1520
|
|
|
|
|
|
|
|
|
1521
|
|
|
|
|
|
|
|
|
1522
|
|
|
|
|
|
|
|
|
1523
|
|
|
|
|
|
|
I |
|
1524
|
|
|
|
|
|
|
I |
|
1525
|
|
|
|
|
|
|
I L |
|
1526
|
|
|
|
|
|
|
I. |
|
1527
|
|
|
|
|
|
|
|
|
1528
|
|
|
|
|
|
|
L syntax: |
|
1529
|
|
|
|
|
|
|
|
|
1530
|
|
|
|
|
|
|
|
|
1531
|
|
|
|
|
|
|
|
|
1532
|
|
|
|
|
|
|
L syntax: |
|
1533
|
|
|
|
|
|
|
|
|
1534
|
|
|
|
|
|
|
|
|
1535
|
|
|
|
|
|
|
|
|
1536
|
|
|
|
|
|
|
L syntax: |
|
1537
|
|
|
|
|
|
|
|
|
1538
|
|
|
|
|
|
|
[% CGIAPP.embed("some_run_mode", param1, 'literal string2' ) %] |
|
1539
|
|
|
|
|
|
|
|
|
1540
|
|
|
|
|
|
|
L syntax: |
|
1541
|
|
|
|
|
|
|
|
|
1542
|
|
|
|
|
|
|
|
|
1543
|
|
|
|
|
|
|
this text gets replaced by the output of some_run_mode |
|
1544
|
|
|
|
|
|
|
|
|
1545
|
|
|
|
|
|
|
|
|
1546
|
|
|
|
|
|
|
|
|
1547
|
|
|
|
|
|
|
=cut |
|
1548
|
|
|
|
|
|
|
|
|
1549
|
|
|
|
|
|
|
|
|
1550
|
|
|
|
|
|
|
=head1 NOTES FOR AUTHORS OF PLUGINS AND REUSABLE APPLICATIONS |
|
1551
|
|
|
|
|
|
|
|
|
1552
|
|
|
|
|
|
|
If you are writing a L plugin module, or you are |
|
1553
|
|
|
|
|
|
|
writing a C program that will be distributed to other |
|
1554
|
|
|
|
|
|
|
people (e.g. on CPAN), then it's important to take steps to prevent your |
|
1555
|
|
|
|
|
|
|
application's use of L from |
|
1556
|
|
|
|
|
|
|
conflicting with other plugins or with your end users. |
|
1557
|
|
|
|
|
|
|
|
|
1558
|
|
|
|
|
|
|
When a plugin that uses L calls: |
|
1559
|
|
|
|
|
|
|
|
|
1560
|
|
|
|
|
|
|
$self->template->config(...) |
|
1561
|
|
|
|
|
|
|
|
|
1562
|
|
|
|
|
|
|
It overwrites any existing template configuration with the new settings. |
|
1563
|
|
|
|
|
|
|
So if two plugins do that, they probably clobber each other. |
|
1564
|
|
|
|
|
|
|
|
|
1565
|
|
|
|
|
|
|
However, L has the feature of |
|
1566
|
|
|
|
|
|
|
named independent configs: |
|
1567
|
|
|
|
|
|
|
|
|
1568
|
|
|
|
|
|
|
$self->template('your_module')->config(...) |
|
1569
|
|
|
|
|
|
|
$self->template('my_plugin')->config(...) |
|
1570
|
|
|
|
|
|
|
|
|
1571
|
|
|
|
|
|
|
These configs remain separate from each other. However, you have to |
|
1572
|
|
|
|
|
|
|
keep using these names throughout your module, even when you load and |
|
1573
|
|
|
|
|
|
|
fill the template. For instance: |
|
1574
|
|
|
|
|
|
|
|
|
1575
|
|
|
|
|
|
|
sub my_runmode { |
|
1576
|
|
|
|
|
|
|
my $self = shift; |
|
1577
|
|
|
|
|
|
|
my $template = $self->template('my_plugin')->load; |
|
1578
|
|
|
|
|
|
|
$template->output; |
|
1579
|
|
|
|
|
|
|
} |
|
1580
|
|
|
|
|
|
|
|
|
1581
|
|
|
|
|
|
|
sub your_runmode { |
|
1582
|
|
|
|
|
|
|
my $self = shift; |
|
1583
|
|
|
|
|
|
|
my %params; |
|
1584
|
|
|
|
|
|
|
$self->template('your_module')->fill(\%params); |
|
1585
|
|
|
|
|
|
|
} |
|
1586
|
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
It's uglier and more verbose, but it also prevents plugins from |
|
1588
|
|
|
|
|
|
|
stepping on each other's toes. |
|
1589
|
|
|
|
|
|
|
|
|
1590
|
|
|
|
|
|
|
L plugins that use |
|
1591
|
|
|
|
|
|
|
L should default to |
|
1592
|
|
|
|
|
|
|
using their own package name for the AnyTemplate config name: |
|
1593
|
|
|
|
|
|
|
|
|
1594
|
|
|
|
|
|
|
$self->template(__PACKAGE__)->config(...); |
|
1595
|
|
|
|
|
|
|
$self->template(__PACKAGE__)->fill(...); |
|
1596
|
|
|
|
|
|
|
|
|
1597
|
|
|
|
|
|
|
=head1 CHANGING THE NAME OF THE 'template' METHOD |
|
1598
|
|
|
|
|
|
|
|
|
1599
|
|
|
|
|
|
|
If you want to access the features of this module using a method other |
|
1600
|
|
|
|
|
|
|
than C, you can do so via Anno Siegel's L |
|
1601
|
|
|
|
|
|
|
module (available on CPAN). |
|
1602
|
|
|
|
|
|
|
|
|
1603
|
|
|
|
|
|
|
For instance, to use syntax similar to L: |
|
1604
|
|
|
|
|
|
|
|
|
1605
|
|
|
|
|
|
|
use Exporter::Renaming; |
|
1606
|
|
|
|
|
|
|
use CGI::Application::Plugin::AnyTemplate Renaming => [ template => tt]; |
|
1607
|
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
sub cgiapp_init { |
|
1609
|
|
|
|
|
|
|
my $self = shift; |
|
1610
|
|
|
|
|
|
|
|
|
1611
|
|
|
|
|
|
|
my %params = ( ... ); |
|
1612
|
|
|
|
|
|
|
|
|
1613
|
|
|
|
|
|
|
# Set config file and other options |
|
1614
|
|
|
|
|
|
|
$self->tt->config( |
|
1615
|
|
|
|
|
|
|
default_type => 'TemplateToolkit', |
|
1616
|
|
|
|
|
|
|
); |
|
1617
|
|
|
|
|
|
|
|
|
1618
|
|
|
|
|
|
|
} |
|
1619
|
|
|
|
|
|
|
|
|
1620
|
|
|
|
|
|
|
sub my_runmode { |
|
1621
|
|
|
|
|
|
|
my $self = shift; |
|
1622
|
|
|
|
|
|
|
$self->tt->process('file', \%params); |
|
1623
|
|
|
|
|
|
|
} |
|
1624
|
|
|
|
|
|
|
|
|
1625
|
|
|
|
|
|
|
And to use syntax similar to L's C mechanism: |
|
1626
|
|
|
|
|
|
|
|
|
1627
|
|
|
|
|
|
|
use Exporter::Renaming; |
|
1628
|
|
|
|
|
|
|
use CGI::Application::Plugin::AnyTemplate Renaming => [ template => tmpl]; |
|
1629
|
|
|
|
|
|
|
|
|
1630
|
|
|
|
|
|
|
sub cgiapp_init { |
|
1631
|
|
|
|
|
|
|
my $self = shift; |
|
1632
|
|
|
|
|
|
|
|
|
1633
|
|
|
|
|
|
|
# Set config file and other options |
|
1634
|
|
|
|
|
|
|
$self->tmpl->config( |
|
1635
|
|
|
|
|
|
|
default_type => 'HTMLTemplate', |
|
1636
|
|
|
|
|
|
|
); |
|
1637
|
|
|
|
|
|
|
|
|
1638
|
|
|
|
|
|
|
} |
|
1639
|
|
|
|
|
|
|
|
|
1640
|
|
|
|
|
|
|
sub my_runmode { |
|
1641
|
|
|
|
|
|
|
my $self = shift; |
|
1642
|
|
|
|
|
|
|
|
|
1643
|
|
|
|
|
|
|
my %params = ( ... ); |
|
1644
|
|
|
|
|
|
|
|
|
1645
|
|
|
|
|
|
|
my $template = $self->tmpl->load('file'); |
|
1646
|
|
|
|
|
|
|
$template->param(\%params); |
|
1647
|
|
|
|
|
|
|
$template->output; |
|
1648
|
|
|
|
|
|
|
} |
|
1649
|
|
|
|
|
|
|
|
|
1650
|
|
|
|
|
|
|
=head1 AUTHOR |
|
1651
|
|
|
|
|
|
|
|
|
1652
|
|
|
|
|
|
|
Michael Graham, C<< >> |
|
1653
|
|
|
|
|
|
|
|
|
1654
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
1655
|
|
|
|
|
|
|
|
|
1656
|
|
|
|
|
|
|
I originally wrote this to be a subsystem in Richard Dice's |
|
1657
|
|
|
|
|
|
|
L-based framework, before I moved it into its own module. |
|
1658
|
|
|
|
|
|
|
|
|
1659
|
|
|
|
|
|
|
Various ideas taken from L (Jesse Erlbaum), |
|
1660
|
|
|
|
|
|
|
L (Cees Hek) and C |
|
1661
|
|
|
|
|
|
|
(Stephen Nelson). |
|
1662
|
|
|
|
|
|
|
|
|
1663
|
|
|
|
|
|
|
C singleton support code stolen from L. |
|
1664
|
|
|
|
|
|
|
|
|
1665
|
|
|
|
|
|
|
|
|
1666
|
|
|
|
|
|
|
=head1 BUGS |
|
1667
|
|
|
|
|
|
|
|
|
1668
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
|
1669
|
|
|
|
|
|
|
C, or through the web interface at |
|
1670
|
|
|
|
|
|
|
L. I will be notified, and then you'll automatically |
|
1671
|
|
|
|
|
|
|
be notified of progress on your bug as I make changes. |
|
1672
|
|
|
|
|
|
|
|
|
1673
|
|
|
|
|
|
|
=head1 SOURCE |
|
1674
|
|
|
|
|
|
|
|
|
1675
|
|
|
|
|
|
|
The source code repository for this module can be found at http://github.com/mgraham/CAP-AnyTemplate/ |
|
1676
|
|
|
|
|
|
|
|
|
1677
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
1678
|
|
|
|
|
|
|
|
|
1679
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::Base |
|
1680
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::ComponentHandler |
|
1681
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate |
|
1682
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExpr |
|
1683
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggable |
|
1684
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit |
|
1685
|
|
|
|
|
|
|
CGI::Application::Plugin::AnyTemplate::Driver::Petal |
|
1686
|
|
|
|
|
|
|
|
|
1687
|
|
|
|
|
|
|
CGI::Application |
|
1688
|
|
|
|
|
|
|
|
|
1689
|
|
|
|
|
|
|
Template::Toolkit |
|
1690
|
|
|
|
|
|
|
HTML::Template |
|
1691
|
|
|
|
|
|
|
|
|
1692
|
|
|
|
|
|
|
HTML::Template::Pluggable |
|
1693
|
|
|
|
|
|
|
HTML::Template::Plugin::Dot |
|
1694
|
|
|
|
|
|
|
|
|
1695
|
|
|
|
|
|
|
Petal |
|
1696
|
|
|
|
|
|
|
|
|
1697
|
|
|
|
|
|
|
Exporter::Renaming |
|
1698
|
|
|
|
|
|
|
|
|
1699
|
|
|
|
|
|
|
CGI::Application::Plugin::TT |
|
1700
|
|
|
|
|
|
|
|
|
1701
|
|
|
|
|
|
|
|
|
1702
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
1703
|
|
|
|
|
|
|
|
|
1704
|
|
|
|
|
|
|
Copyright 2005 Michael Graham, All Rights Reserved. |
|
1705
|
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
1707
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
1708
|
|
|
|
|
|
|
|
|
1709
|
|
|
|
|
|
|
=cut |
|
1710
|
|
|
|
|
|
|
|
|
1711
|
|
|
|
|
|
|
1; |