| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
################################################## |
|
3
|
|
|
|
|
|
|
## |
|
4
|
|
|
|
|
|
|
## Name: CGI::FastTemplate |
|
5
|
|
|
|
|
|
|
## |
|
6
|
|
|
|
|
|
|
## Copyright (c) 1998-99 Jason Moore . All rights |
|
7
|
|
|
|
|
|
|
## reserved. |
|
8
|
|
|
|
|
|
|
## |
|
9
|
|
|
|
|
|
|
## This program is free software; you can redistribute it and/or |
|
10
|
|
|
|
|
|
|
## modify it under the same terms as Perl itself. |
|
11
|
|
|
|
|
|
|
## |
|
12
|
|
|
|
|
|
|
## This program is distributed in the hope that it will be useful, |
|
13
|
|
|
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
|
|
|
|
## Artistic License for more details. |
|
16
|
|
|
|
|
|
|
## |
|
17
|
|
|
|
|
|
|
## |
|
18
|
|
|
|
|
|
|
## Credits: |
|
19
|
|
|
|
|
|
|
## - fancy regexp taken from article by Brian Slesinsky |
|
20
|
|
|
|
|
|
|
## http://www.hotwired.com/webmonkey/code/97/21/index2a_page4.html?tw=perl |
|
21
|
|
|
|
|
|
|
## |
|
22
|
|
|
|
|
|
|
## - modified regexp to support ${VAR} and $VAR styles suggested by Eric L. Brine |
|
23
|
|
|
|
|
|
|
## |
|
24
|
|
|
|
|
|
|
## |
|
25
|
|
|
|
|
|
|
## Documentation: |
|
26
|
|
|
|
|
|
|
## See |
|
27
|
|
|
|
|
|
|
## 'perldoc CGI::FastTemplate' |
|
28
|
|
|
|
|
|
|
## or |
|
29
|
|
|
|
|
|
|
## 'perldoc ./FastTemplate' |
|
30
|
|
|
|
|
|
|
## |
|
31
|
|
|
|
|
|
|
## History: |
|
32
|
|
|
|
|
|
|
## See 'README' |
|
33
|
|
|
|
|
|
|
## |
|
34
|
|
|
|
|
|
|
## $Id: FastTemplate.pm,v 1.2 1999/06/27 02:12:23 jmoore Exp $ |
|
35
|
|
|
|
|
|
|
## |
|
36
|
|
|
|
|
|
|
################################################## |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
package CGI::FastTemplate; |
|
39
|
|
|
|
|
|
|
|
|
40
|
5
|
|
|
5
|
|
7131
|
use strict; |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
22213
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$CGI::FastTemplate::VERSION = '1.09'; |
|
43
|
|
|
|
|
|
|
$CGI::FastTemplate::ROOT = undef; |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$CGI::FastTemplate::VAR_ID = '$'; |
|
46
|
|
|
|
|
|
|
$CGI::FastTemplate::DELIM_LEFT = '{'; |
|
47
|
|
|
|
|
|
|
$CGI::FastTemplate::DELIM_RIGHT = '}'; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
## |
|
50
|
|
|
|
|
|
|
## define indexes for object attributes |
|
51
|
|
|
|
|
|
|
## |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub STRICT () {0}; |
|
54
|
|
|
|
|
|
|
sub namespace () {1}; |
|
55
|
|
|
|
|
|
|
sub namespaces () {2}; |
|
56
|
|
|
|
|
|
|
sub last_parse () {3}; |
|
57
|
|
|
|
|
|
|
sub template_name () {4}; |
|
58
|
|
|
|
|
|
|
sub template_data () {5}; |
|
59
|
|
|
|
|
|
|
sub ROOT () {6}; |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
################################################## |
|
63
|
|
|
|
|
|
|
## |
|
64
|
|
|
|
|
|
|
sub new |
|
65
|
|
|
|
|
|
|
## |
|
66
|
|
|
|
|
|
|
## - instantiates FastTemplate |
|
67
|
|
|
|
|
|
|
## |
|
68
|
|
|
|
|
|
|
{ |
|
69
|
5
|
|
|
5
|
0
|
2289
|
my($class,$root) = @_; |
|
70
|
5
|
|
|
|
|
17
|
my $self = []; |
|
71
|
5
|
|
|
|
|
16
|
bless $self, $class; |
|
72
|
|
|
|
|
|
|
|
|
73
|
5
|
|
|
|
|
22
|
$self->init; |
|
74
|
|
|
|
|
|
|
|
|
75
|
5
|
|
|
|
|
11
|
$self->[STRICT] = 1; |
|
76
|
|
|
|
|
|
|
|
|
77
|
5
|
100
|
|
|
|
21
|
if (defined($root)) |
|
78
|
|
|
|
|
|
|
{ |
|
79
|
1
|
|
|
|
|
7
|
$self->set_root($root); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
5
|
|
|
|
|
16
|
return($self); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
################################################## |
|
85
|
|
|
|
|
|
|
## |
|
86
|
|
|
|
|
|
|
sub strict |
|
87
|
|
|
|
|
|
|
## |
|
88
|
|
|
|
|
|
|
{ |
|
89
|
2
|
|
|
2
|
1
|
14
|
my($self) = shift; |
|
90
|
2
|
|
|
|
|
7
|
$self->[STRICT] = 1; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
################################################## |
|
94
|
|
|
|
|
|
|
## |
|
95
|
|
|
|
|
|
|
sub no_strict |
|
96
|
|
|
|
|
|
|
## |
|
97
|
|
|
|
|
|
|
{ |
|
98
|
2
|
|
|
2
|
1
|
10
|
my($self) = shift; |
|
99
|
2
|
|
|
|
|
6
|
$self->[STRICT] = undef; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
################################################## |
|
103
|
|
|
|
|
|
|
## |
|
104
|
|
|
|
|
|
|
sub clear_all |
|
105
|
|
|
|
|
|
|
## |
|
106
|
|
|
|
|
|
|
## - initializes (or clears!) variables |
|
107
|
|
|
|
|
|
|
## |
|
108
|
|
|
|
|
|
|
{ |
|
109
|
5
|
|
|
5
|
1
|
14
|
my($self) = shift; |
|
110
|
|
|
|
|
|
|
|
|
111
|
5
|
50
|
|
|
|
22
|
if (!ref($self)) |
|
112
|
|
|
|
|
|
|
{ |
|
113
|
0
|
|
|
|
|
0
|
print STDERR "FastTemplate: Unable to call init without instance.\n"; |
|
114
|
0
|
|
|
|
|
0
|
return(); |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
5
|
|
|
|
|
32
|
$self->[namespace] = {}; ## main hash where we resolve variables |
|
118
|
5
|
|
|
|
|
16
|
$self->[namespaces] = []; ## array of hash refs |
|
119
|
|
|
|
|
|
|
|
|
120
|
5
|
|
|
|
|
12
|
$self->[last_parse] = undef; ## remember where we stored the last parse so print() |
|
121
|
|
|
|
|
|
|
## will have a default |
|
122
|
|
|
|
|
|
|
|
|
123
|
5
|
|
|
|
|
12
|
$self->[template_name] = {}; ## template name: template file |
|
124
|
5
|
|
|
|
|
19
|
$self->[template_data] = {}; ## template name: template content/data |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
*init = \&clear_all; ## alias to 'clear' : 'init' |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
################################################## |
|
129
|
|
|
|
|
|
|
## |
|
130
|
|
|
|
|
|
|
sub clear_define |
|
131
|
|
|
|
|
|
|
## |
|
132
|
|
|
|
|
|
|
## - clears values entered with define() |
|
133
|
|
|
|
|
|
|
## |
|
134
|
|
|
|
|
|
|
{ |
|
135
|
0
|
|
|
0
|
1
|
0
|
my($self) = shift; |
|
136
|
0
|
|
|
|
|
0
|
$self->[template_name] = {}; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
################################################## |
|
140
|
|
|
|
|
|
|
## |
|
141
|
|
|
|
|
|
|
sub clear_tpl |
|
142
|
|
|
|
|
|
|
## |
|
143
|
|
|
|
|
|
|
## - clears hash that holds loaded templates. |
|
144
|
|
|
|
|
|
|
## - if passed an array of names, clears only those loaded templates |
|
145
|
|
|
|
|
|
|
## |
|
146
|
|
|
|
|
|
|
{ |
|
147
|
0
|
|
|
0
|
1
|
0
|
my($self) = shift; |
|
148
|
0
|
|
|
|
|
0
|
my @args = @_; |
|
149
|
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
0
|
if (@args == 0) ## clear entire cache |
|
151
|
|
|
|
|
|
|
{ |
|
152
|
0
|
|
|
|
|
0
|
$self->[template_data] = {}; |
|
153
|
0
|
|
|
|
|
0
|
return(1); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
## clear just a selection of entries |
|
157
|
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
0
|
for (@args) |
|
159
|
|
|
|
|
|
|
{ |
|
160
|
0
|
|
|
|
|
0
|
delete( ${$self->[template_data]}{$_} ); |
|
|
0
|
|
|
|
|
0
|
|
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
return(1); |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
################################################## |
|
168
|
|
|
|
|
|
|
## |
|
169
|
|
|
|
|
|
|
sub clear_href |
|
170
|
|
|
|
|
|
|
## |
|
171
|
|
|
|
|
|
|
## - removes from the end, a given number of hash references |
|
172
|
|
|
|
|
|
|
## from the namespace list. |
|
173
|
|
|
|
|
|
|
## |
|
174
|
|
|
|
|
|
|
## - 1: number of hash references to erase |
|
175
|
|
|
|
|
|
|
## |
|
176
|
|
|
|
|
|
|
{ |
|
177
|
0
|
|
|
0
|
1
|
0
|
my($self, $number) = @_; |
|
178
|
|
|
|
|
|
|
|
|
179
|
0
|
0
|
|
|
|
0
|
if (!defined($number)) |
|
180
|
|
|
|
|
|
|
{ |
|
181
|
0
|
|
|
|
|
0
|
$self->[namespaces] = []; |
|
182
|
0
|
|
|
|
|
0
|
return(1); |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
0
|
for (1..$number) |
|
186
|
|
|
|
|
|
|
{ |
|
187
|
0
|
|
|
|
|
0
|
pop(@{$self->[namespaces]}); ## toss it away |
|
|
0
|
|
|
|
|
0
|
|
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
0
|
return(1); |
|
191
|
|
|
|
|
|
|
} |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
################################################# |
|
194
|
|
|
|
|
|
|
## |
|
195
|
|
|
|
|
|
|
sub clear_parse |
|
196
|
|
|
|
|
|
|
## |
|
197
|
|
|
|
|
|
|
## - clears hash which holds parsed variables |
|
198
|
|
|
|
|
|
|
## - if called with a scalar only clears that key/element in the namespace. |
|
199
|
|
|
|
|
|
|
## so, $tpl->clear("ROWS") which is almost the same as, |
|
200
|
|
|
|
|
|
|
## $tpl->assign(ROWS => ""); |
|
201
|
|
|
|
|
|
|
## |
|
202
|
|
|
|
|
|
|
## - if called with an array, all keys in the array are deleted |
|
203
|
|
|
|
|
|
|
## e.g. $tpl->clear("ROWS", "COLS"); has the same effect as |
|
204
|
|
|
|
|
|
|
## $tpl->assign(ROWS => "", |
|
205
|
|
|
|
|
|
|
## COLS => ""); |
|
206
|
|
|
|
|
|
|
## |
|
207
|
|
|
|
|
|
|
## |
|
208
|
|
|
|
|
|
|
{ |
|
209
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
210
|
|
|
|
|
|
|
|
|
211
|
0
|
0
|
|
|
|
0
|
if (@_ == 0) ## clear everything |
|
212
|
|
|
|
|
|
|
{ |
|
213
|
0
|
|
|
|
|
0
|
$self->[namespace] = {}; ## main hash where we resolve variables |
|
214
|
0
|
|
|
|
|
0
|
$self->[last_parse] = undef; ## remember where we stored the last parse so print() |
|
215
|
0
|
|
|
|
|
0
|
return(1); |
|
216
|
|
|
|
|
|
|
} |
|
217
|
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
0
|
for (@_) |
|
219
|
|
|
|
|
|
|
{ |
|
220
|
0
|
|
|
|
|
0
|
delete(${$self->[namespace]}{$_}); |
|
|
0
|
|
|
|
|
0
|
|
|
221
|
|
|
|
|
|
|
} |
|
222
|
0
|
|
|
|
|
0
|
return(1); |
|
223
|
|
|
|
|
|
|
} |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
*clear = \&clear_parse; ## alias clear -> clear_parse |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
################################################## |
|
228
|
|
|
|
|
|
|
## |
|
229
|
|
|
|
|
|
|
sub set_root |
|
230
|
|
|
|
|
|
|
## |
|
231
|
|
|
|
|
|
|
## - sets template root directory. |
|
232
|
|
|
|
|
|
|
{ |
|
233
|
1
|
|
|
1
|
0
|
2
|
my($self, $root) = @_; |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
## set object default root directory |
|
236
|
|
|
|
|
|
|
|
|
237
|
1
|
|
|
|
|
2
|
$CGI::FastTemplate::ROOT = $root; |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
## set instance template dir |
|
240
|
|
|
|
|
|
|
## |
|
241
|
|
|
|
|
|
|
## - no needed |
|
242
|
|
|
|
|
|
|
## |
|
243
|
|
|
|
|
|
|
|
|
244
|
1
|
50
|
|
|
|
9
|
if (ref($self)) |
|
245
|
|
|
|
|
|
|
{ |
|
246
|
1
|
|
|
|
|
2
|
$self->[ROOT] = $root; |
|
247
|
|
|
|
|
|
|
} |
|
248
|
|
|
|
|
|
|
|
|
249
|
1
|
|
|
|
|
2
|
return(1); |
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
################################################## |
|
253
|
|
|
|
|
|
|
## |
|
254
|
|
|
|
|
|
|
sub define |
|
255
|
|
|
|
|
|
|
## |
|
256
|
|
|
|
|
|
|
## - sets alias/name to associate with template filenames |
|
257
|
|
|
|
|
|
|
## - note: names are relative to ROOT directory (set with set_root) |
|
258
|
|
|
|
|
|
|
## - e.g. the following works |
|
259
|
|
|
|
|
|
|
## $tpl->set_root("/tmp/docs"); |
|
260
|
|
|
|
|
|
|
## $tpl->define( main => "../dev_docs"); |
|
261
|
|
|
|
|
|
|
## (assuming you have templates in /tmp/dev_docs) |
|
262
|
|
|
|
|
|
|
## |
|
263
|
|
|
|
|
|
|
## - files are not loaded until used, so go nuts when defining. each line |
|
264
|
|
|
|
|
|
|
## only costs a wee bit of memory and compile time. |
|
265
|
|
|
|
|
|
|
## |
|
266
|
|
|
|
|
|
|
## - note: define is cumulative |
|
267
|
|
|
|
|
|
|
## |
|
268
|
|
|
|
|
|
|
{ |
|
269
|
1
|
|
|
1
|
1
|
8
|
my($self, %define) = @_; |
|
270
|
|
|
|
|
|
|
|
|
271
|
1
|
|
|
|
|
5
|
for (keys(%define)) |
|
272
|
|
|
|
|
|
|
{ |
|
273
|
2
|
|
|
|
|
6
|
$self->[template_name]->{$_} = $define{$_}; |
|
274
|
|
|
|
|
|
|
} |
|
275
|
|
|
|
|
|
|
|
|
276
|
1
|
|
|
|
|
3
|
return(1); |
|
277
|
|
|
|
|
|
|
} |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
################################################## |
|
280
|
|
|
|
|
|
|
## |
|
281
|
|
|
|
|
|
|
sub assign |
|
282
|
|
|
|
|
|
|
## |
|
283
|
|
|
|
|
|
|
## - assigns values of a HASH directly to internal namespace |
|
284
|
|
|
|
|
|
|
## HASH |
|
285
|
|
|
|
|
|
|
## |
|
286
|
|
|
|
|
|
|
## Args: |
|
287
|
|
|
|
|
|
|
## - 1: hash reference (to add to array of namespaces) |
|
288
|
|
|
|
|
|
|
## - 1: hash (to merge with main namespace hash) |
|
289
|
|
|
|
|
|
|
## |
|
290
|
|
|
|
|
|
|
## - returns: 1 on success |
|
291
|
|
|
|
|
|
|
## |
|
292
|
|
|
|
|
|
|
{ |
|
293
|
7
|
|
|
7
|
1
|
46
|
my $self = shift; |
|
294
|
|
|
|
|
|
|
|
|
295
|
7
|
50
|
|
|
|
34
|
if (ref($_[0]) eq "HASH") |
|
296
|
|
|
|
|
|
|
{ |
|
297
|
0
|
|
|
|
|
0
|
push(@{$self->[namespaces]}, $_[0]); |
|
|
0
|
|
|
|
|
0
|
|
|
298
|
0
|
|
|
|
|
0
|
return(1); |
|
299
|
|
|
|
|
|
|
} |
|
300
|
|
|
|
|
|
|
|
|
301
|
7
|
|
|
|
|
96
|
my %assign = @_; |
|
302
|
|
|
|
|
|
|
|
|
303
|
7
|
|
|
|
|
16
|
my($name,$value); |
|
304
|
7
|
|
|
|
|
30
|
while ( ($name,$value) = each(%assign) ) |
|
305
|
|
|
|
|
|
|
{ |
|
306
|
14
|
|
|
|
|
56
|
$self->[namespace]->{$name} = $value; |
|
307
|
|
|
|
|
|
|
} |
|
308
|
|
|
|
|
|
|
|
|
309
|
7
|
|
|
|
|
20
|
return(1); |
|
310
|
|
|
|
|
|
|
} |
|
311
|
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
################################################## |
|
314
|
|
|
|
|
|
|
## |
|
315
|
|
|
|
|
|
|
sub parse |
|
316
|
|
|
|
|
|
|
## |
|
317
|
|
|
|
|
|
|
## - parses a scalar to resolve/interpolate any variables |
|
318
|
|
|
|
|
|
|
## it finds. |
|
319
|
|
|
|
|
|
|
## |
|
320
|
|
|
|
|
|
|
## - 1: hash of what we are parse in TARGET:SOURCE form |
|
321
|
|
|
|
|
|
|
## NOTE: SOURCE with a "." as the first character get appended |
|
322
|
|
|
|
|
|
|
## to existing TARGET |
|
323
|
|
|
|
|
|
|
## |
|
324
|
|
|
|
|
|
|
{ |
|
325
|
9
|
|
|
9
|
1
|
173
|
my($self, %parse) = @_; |
|
326
|
|
|
|
|
|
|
|
|
327
|
9
|
|
|
|
|
12
|
my $target; |
|
328
|
9
|
|
|
|
|
26
|
for $target (keys(%parse)) |
|
329
|
|
|
|
|
|
|
{ |
|
330
|
|
|
|
|
|
|
## |
|
331
|
|
|
|
|
|
|
## make all sources an array... |
|
332
|
|
|
|
|
|
|
## |
|
333
|
|
|
|
|
|
|
|
|
334
|
9
|
50
|
|
|
|
29
|
if (ref($parse{$target}) ne "ARRAY") |
|
335
|
|
|
|
|
|
|
{ |
|
336
|
9
|
|
|
|
|
29
|
$parse{$target} = [$parse{$target}]; |
|
337
|
|
|
|
|
|
|
} |
|
338
|
|
|
|
|
|
|
|
|
339
|
9
|
|
|
|
|
13
|
my($p, $append); |
|
340
|
|
|
|
|
|
|
|
|
341
|
9
|
|
|
|
|
14
|
for $p (@{$parse{$target}}) |
|
|
9
|
|
|
|
|
21
|
|
|
342
|
|
|
|
|
|
|
{ |
|
343
|
9
|
100
|
|
|
|
51
|
if (substr($p,0,1) eq ".") ## detect append |
|
344
|
|
|
|
|
|
|
{ |
|
345
|
5
|
|
|
|
|
5
|
$append = 1; |
|
346
|
5
|
|
|
|
|
12
|
$p = substr($p, 1); |
|
347
|
|
|
|
|
|
|
} |
|
348
|
|
|
|
|
|
|
|
|
349
|
9
|
50
|
|
|
|
46
|
if (!exists($self->[template_name]{$p})) |
|
350
|
|
|
|
|
|
|
{ |
|
351
|
0
|
|
|
|
|
0
|
print STDERR "FastTemplate: Template alias: $p does not exist.\n"; |
|
352
|
0
|
|
|
|
|
0
|
next; |
|
353
|
|
|
|
|
|
|
} |
|
354
|
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
## load template if we need to |
|
356
|
|
|
|
|
|
|
|
|
357
|
9
|
100
|
|
|
|
31
|
if (!exists($self->[template_data]{$p})) |
|
358
|
|
|
|
|
|
|
{ |
|
359
|
2
|
|
|
|
|
26
|
$self->slurp($self->[template_name]->{$p}, \$self->[template_data]->{$p} ); |
|
360
|
|
|
|
|
|
|
} |
|
361
|
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
## copy SOURCE (template_data) to temp variable |
|
363
|
|
|
|
|
|
|
## (can't use namespace, since we might be appending to it.) |
|
364
|
|
|
|
|
|
|
|
|
365
|
9
|
|
|
|
|
38
|
my $temp_parse = $self->[template_data]->{$p}; |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
######### |
|
368
|
|
|
|
|
|
|
## parse |
|
369
|
|
|
|
|
|
|
######### |
|
370
|
|
|
|
|
|
|
|
|
371
|
9
|
|
|
|
|
283
|
$temp_parse =~ s/\$(?:([A-Z][A-Z0-9_]+)|\{([A-Z][A-Z0-9_]+)\})/ |
|
372
|
|
|
|
|
|
|
|
|
373
|
18
|
|
|
|
|
87
|
my $v = $self->[namespace]->{$+}; |
|
374
|
|
|
|
|
|
|
|
|
375
|
18
|
100
|
|
|
|
45
|
if (!defined($v)) |
|
376
|
|
|
|
|
|
|
{ |
|
377
|
|
|
|
|
|
|
## look in array of hash refs for value of variable |
|
378
|
2
|
|
|
|
|
3
|
my $r; |
|
379
|
2
|
|
|
|
|
4
|
for $r (@{$self->[namespaces]}) |
|
|
2
|
|
|
|
|
5
|
|
|
380
|
|
|
|
|
|
|
{ |
|
381
|
0
|
0
|
|
|
|
0
|
if (exists($$r{$+})) ## found it |
|
382
|
|
|
|
|
|
|
{ |
|
383
|
0
|
|
|
|
|
0
|
$v = $$r{$+}; |
|
384
|
0
|
|
|
|
|
0
|
last; |
|
385
|
|
|
|
|
|
|
} |
|
386
|
|
|
|
|
|
|
} |
|
387
|
|
|
|
|
|
|
} |
|
388
|
18
|
100
|
|
|
|
41
|
if (!defined($v)) ## $v should be empty not undef, to prevent |
|
389
|
|
|
|
|
|
|
{ ## warnings under -w |
|
390
|
2
|
100
|
|
|
|
7
|
if ($self->[STRICT]) |
|
391
|
|
|
|
|
|
|
{ |
|
392
|
1
|
|
|
|
|
56
|
print STDERR "[CGI::FastTemplate] Warning: no value found for variable: $+\n"; |
|
393
|
1
|
|
|
|
|
4
|
$v = '$' . $+; ## keep original variable name in output |
|
394
|
|
|
|
|
|
|
} |
|
395
|
|
|
|
|
|
|
else |
|
396
|
|
|
|
|
|
|
{ |
|
397
|
1
|
|
|
|
|
3
|
$v = ""; ## remove variable name |
|
398
|
|
|
|
|
|
|
} |
|
399
|
|
|
|
|
|
|
} |
|
400
|
18
|
|
|
|
|
74
|
$v; |
|
401
|
|
|
|
|
|
|
/ge; |
|
402
|
|
|
|
|
|
|
|
|
403
|
9
|
|
|
|
|
21
|
$self->[last_parse] = $target; |
|
404
|
|
|
|
|
|
|
## assign temp to final TARGET |
|
405
|
|
|
|
|
|
|
|
|
406
|
9
|
100
|
|
|
|
29
|
if ($append) |
|
407
|
|
|
|
|
|
|
{ |
|
408
|
5
|
|
|
|
|
36
|
$self->[namespace]->{$target} .= $temp_parse; |
|
409
|
|
|
|
|
|
|
} |
|
410
|
|
|
|
|
|
|
else |
|
411
|
|
|
|
|
|
|
{ |
|
412
|
4
|
|
|
|
|
29
|
$self->[namespace]->{$target} = $temp_parse; |
|
413
|
|
|
|
|
|
|
} |
|
414
|
|
|
|
|
|
|
} |
|
415
|
|
|
|
|
|
|
} |
|
416
|
|
|
|
|
|
|
} |
|
417
|
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
################################################## |
|
419
|
|
|
|
|
|
|
## |
|
420
|
|
|
|
|
|
|
sub slurp |
|
421
|
|
|
|
|
|
|
## |
|
422
|
|
|
|
|
|
|
## - slurps (loads) in file into a scalar. |
|
423
|
|
|
|
|
|
|
## - cool trick to undef the end of line character |
|
424
|
|
|
|
|
|
|
## grabbed from some usenet posting. (don't remember) |
|
425
|
|
|
|
|
|
|
## |
|
426
|
|
|
|
|
|
|
## - i think the maximum file size is (2**32-1) approx. 2 megs. |
|
427
|
|
|
|
|
|
|
## |
|
428
|
|
|
|
|
|
|
## - 1: filename (minus path) |
|
429
|
|
|
|
|
|
|
## - 2: reference to put result in [optional] |
|
430
|
|
|
|
|
|
|
## returns: scalar |
|
431
|
|
|
|
|
|
|
## |
|
432
|
|
|
|
|
|
|
## |
|
433
|
|
|
|
|
|
|
{ |
|
434
|
2
|
|
|
2
|
0
|
4
|
my($self, $filename, $ref) = @_; |
|
435
|
2
|
|
|
|
|
4
|
my $temp; |
|
436
|
|
|
|
|
|
|
|
|
437
|
2
|
50
|
33
|
|
|
14
|
if (ref($self) && defined($self->[ROOT])) ## use instance ROOT |
|
|
|
0
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
{ |
|
439
|
2
|
|
|
|
|
5
|
$filename = $self->[ROOT] . "/" . $filename; |
|
440
|
|
|
|
|
|
|
} |
|
441
|
|
|
|
|
|
|
elsif (defined($CGI::FastTemplate::ROOT)) ## use object ROOT |
|
442
|
|
|
|
|
|
|
{ |
|
443
|
0
|
|
|
|
|
0
|
$filename = $CGI::FastTemplate::ROOT . "/" . $filename; |
|
444
|
|
|
|
|
|
|
} |
|
445
|
|
|
|
|
|
|
|
|
446
|
2
|
50
|
|
|
|
79
|
if (!open(TEMPLATE, $filename)) |
|
447
|
|
|
|
|
|
|
{ |
|
448
|
0
|
|
|
|
|
0
|
print STDERR "FastTemplate: slurp: cannot open: $filename ($!)"; |
|
449
|
0
|
|
|
|
|
0
|
return(); |
|
450
|
|
|
|
|
|
|
} |
|
451
|
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
## cool trick! |
|
453
|
|
|
|
|
|
|
|
|
454
|
2
|
|
|
|
|
9
|
local($/) = undef; |
|
455
|
2
|
|
|
|
|
55
|
$temp = ; |
|
456
|
2
|
|
|
|
|
18
|
close(TEMPLATE); |
|
457
|
|
|
|
|
|
|
|
|
458
|
2
|
50
|
|
|
|
9
|
if (defined($ref)) ## fill reference |
|
459
|
|
|
|
|
|
|
{ |
|
460
|
2
|
|
|
|
|
4
|
$$ref = $temp; |
|
461
|
2
|
|
|
|
|
8
|
return(1); |
|
462
|
|
|
|
|
|
|
} |
|
463
|
|
|
|
|
|
|
|
|
464
|
0
|
|
|
|
|
0
|
return($temp); |
|
465
|
|
|
|
|
|
|
} |
|
466
|
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
################################################## |
|
469
|
|
|
|
|
|
|
## |
|
470
|
|
|
|
|
|
|
sub define_nofile |
|
471
|
|
|
|
|
|
|
## |
|
472
|
|
|
|
|
|
|
## - allows caller to bypass storing templates in files and |
|
473
|
|
|
|
|
|
|
## using define() to map aliased to the file |
|
474
|
|
|
|
|
|
|
## |
|
475
|
|
|
|
|
|
|
## 1: hash (or hash ref) {template name => raw template data} |
|
476
|
|
|
|
|
|
|
## e.g. $raw_tpl = 'Hello $NAME.'; |
|
477
|
|
|
|
|
|
|
## |
|
478
|
|
|
|
|
|
|
## define_nofile(greeting => $raw_tpl); |
|
479
|
|
|
|
|
|
|
## |
|
480
|
|
|
|
|
|
|
## Note: single ticks (literal) in the above example are required when |
|
481
|
|
|
|
|
|
|
## constructing templates to prevent the variables from being |
|
482
|
|
|
|
|
|
|
## evalualted/interpolated _before_ being passed into the templating |
|
483
|
|
|
|
|
|
|
## module. |
|
484
|
|
|
|
|
|
|
## |
|
485
|
|
|
|
|
|
|
## returns: 1 on success, undef on failure |
|
486
|
|
|
|
|
|
|
## |
|
487
|
|
|
|
|
|
|
{ |
|
488
|
3
|
|
|
3
|
1
|
20
|
my $self = shift; |
|
489
|
|
|
|
|
|
|
|
|
490
|
3
|
|
|
|
|
6
|
my $href; |
|
491
|
|
|
|
|
|
|
|
|
492
|
3
|
50
|
|
|
|
15
|
if (ref($_[0]) eq "HASH") |
|
493
|
|
|
|
|
|
|
{ |
|
494
|
0
|
|
|
|
|
0
|
$href = $_[0]; |
|
495
|
|
|
|
|
|
|
} |
|
496
|
|
|
|
|
|
|
else |
|
497
|
|
|
|
|
|
|
{ |
|
498
|
3
|
|
|
|
|
18
|
my %h = @_; |
|
499
|
3
|
|
|
|
|
12
|
$href = \%h; |
|
500
|
|
|
|
|
|
|
} |
|
501
|
|
|
|
|
|
|
|
|
502
|
3
|
|
|
|
|
7
|
my $k; |
|
503
|
3
|
|
|
|
|
12
|
for $k (keys(%$href)) |
|
504
|
|
|
|
|
|
|
{ |
|
505
|
4
|
|
|
|
|
16
|
$self->[template_name]->{$k} = 1; ## exists will now be true (loading skipped) |
|
506
|
4
|
|
|
|
|
14
|
$self->[template_data]->{$k} = $$href{$k}; ## |
|
507
|
|
|
|
|
|
|
} |
|
508
|
3
|
|
|
|
|
14
|
return(1); |
|
509
|
|
|
|
|
|
|
} |
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
*define_raw = \&define_nofile; |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
################################################## |
|
514
|
|
|
|
|
|
|
## |
|
515
|
|
|
|
|
|
|
sub print |
|
516
|
|
|
|
|
|
|
## |
|
517
|
|
|
|
|
|
|
## - calls built in perl function "print" on given |
|
518
|
|
|
|
|
|
|
## hash key. |
|
519
|
|
|
|
|
|
|
## |
|
520
|
|
|
|
|
|
|
{ |
|
521
|
0
|
|
|
0
|
1
|
0
|
my($self, $var) = @_; |
|
522
|
|
|
|
|
|
|
|
|
523
|
0
|
0
|
|
|
|
0
|
if (!defined($var)) |
|
524
|
|
|
|
|
|
|
{ |
|
525
|
0
|
0
|
|
|
|
0
|
if (!defined($self->[last_parse])) |
|
526
|
|
|
|
|
|
|
{ |
|
527
|
0
|
|
|
|
|
0
|
print STDERR "FastTemplate: Nothing has been parsed. Nothing to print.\n"; |
|
528
|
0
|
|
|
|
|
0
|
return(); |
|
529
|
|
|
|
|
|
|
} |
|
530
|
0
|
|
|
|
|
0
|
print $self->[namespace]->{$self->[last_parse]}; |
|
531
|
0
|
|
|
|
|
0
|
return(1); |
|
532
|
|
|
|
|
|
|
} |
|
533
|
|
|
|
|
|
|
|
|
534
|
0
|
|
|
|
|
0
|
print $self->[namespace]->{$var}; |
|
535
|
0
|
|
|
|
|
0
|
return(1); |
|
536
|
|
|
|
|
|
|
} |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
################################################## |
|
540
|
|
|
|
|
|
|
## |
|
541
|
|
|
|
|
|
|
sub fetch |
|
542
|
|
|
|
|
|
|
## |
|
543
|
|
|
|
|
|
|
## - returns a scalar ref to a value in the namespace |
|
544
|
|
|
|
|
|
|
## |
|
545
|
|
|
|
|
|
|
## - 1: value to fetch |
|
546
|
|
|
|
|
|
|
## |
|
547
|
|
|
|
|
|
|
## - returns: scalar ref |
|
548
|
|
|
|
|
|
|
## |
|
549
|
|
|
|
|
|
|
{ |
|
550
|
4
|
|
|
4
|
1
|
81
|
my($self, $what) = @_; |
|
551
|
|
|
|
|
|
|
|
|
552
|
4
|
50
|
|
|
|
29
|
if (!exists($self->[namespace]->{$what})) |
|
553
|
|
|
|
|
|
|
{ |
|
554
|
0
|
|
|
|
|
0
|
print STDERR "Unable to fetch $what from FastTemplate object. Doesn't exist.\n"; |
|
555
|
0
|
|
|
|
|
0
|
return(); |
|
556
|
|
|
|
|
|
|
} |
|
557
|
|
|
|
|
|
|
|
|
558
|
4
|
|
|
|
|
14
|
return( \$self->[namespace]->{$what} ); |
|
559
|
|
|
|
|
|
|
} |
|
560
|
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
################################################## |
|
562
|
|
|
|
|
|
|
## |
|
563
|
|
|
|
|
|
|
## sub DESTROY () {} |
|
564
|
|
|
|
|
|
|
## |
|
565
|
|
|
|
|
|
|
## - null function |
|
566
|
|
|
|
|
|
|
## - this is not required, so it should probably be removed completely in |
|
567
|
|
|
|
|
|
|
## the next version. |
|
568
|
|
|
|
|
|
|
## |
|
569
|
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
1; |
|
571
|
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
=head1 NAME |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
CGI::FastTemplate - Perl extension for managing templates, and performing variable interpolation. |
|
575
|
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
578
|
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
use CGI::FastTemplate; |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
$tpl = new CGI::FastTemplate(); |
|
582
|
|
|
|
|
|
|
$tpl = new CGI::FastTemplate("/path/to/templates"); |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
CGI::FastTemplate->set_root("/path/to/templates"); ## all instances will use this path |
|
585
|
|
|
|
|
|
|
$tpl->set_root("/path/to/templates"); ## this instance will use this path |
|
586
|
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
$tpl->define( main => "main.tpl", |
|
588
|
|
|
|
|
|
|
row => "table_row.tpl", |
|
589
|
|
|
|
|
|
|
all => "table_all.tpl", |
|
590
|
|
|
|
|
|
|
); |
|
591
|
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
$tpl->assign(TITLE => "I am the title."); |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
my %defaults = ( FONT => "", |
|
595
|
|
|
|
|
|
|
EMAIL => 'jmoore@sober.com', |
|
596
|
|
|
|
|
|
|
); |
|
597
|
|
|
|
|
|
|
$tpl->assign(\%defaults); |
|
598
|
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
$tpl->parse(ROWS => ".row"); ## the '.' appends to ROWS |
|
600
|
|
|
|
|
|
|
$tpl->parse(CONTENT => ["row", "all"]); |
|
601
|
|
|
|
|
|
|
$tpl->parse(CONTENT => "main"); |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
$tpl->print(); ## defaults to last parsed |
|
604
|
|
|
|
|
|
|
$tpl->print("CONTENT"); ## same as print() as "CONTENT" was last parsed |
|
605
|
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
$ref = $tpl->fetch("CONTENT"); |
|
607
|
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
610
|
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
=head2 What is a template? |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
A template is a text file with variables in it. When a template is |
|
614
|
|
|
|
|
|
|
parsed, the variables are interpolated to text. (The text can be a few |
|
615
|
|
|
|
|
|
|
bytes or a few hundred kilobytes.) Here is a simple template with one |
|
616
|
|
|
|
|
|
|
variable ('$NAME'): |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
Hello $NAME. How are you? |
|
619
|
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
=head2 When are templates useful? |
|
621
|
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
Templates are very useful for CGI programming, because adding HTML to your |
|
623
|
|
|
|
|
|
|
perl code clutters your code and forces you to do any HTML modifications. |
|
624
|
|
|
|
|
|
|
By putting all of your HTML in separate template files, you can let |
|
625
|
|
|
|
|
|
|
a graphic or interface designer change the look of your application |
|
626
|
|
|
|
|
|
|
without having to bug you, or let them muck around in your perl code. |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
=head2 There are other templating modules on CPAN, what makes FastTemplate |
|
629
|
|
|
|
|
|
|
different? |
|
630
|
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
CGI::FastTemplate has the following attributes: |
|
632
|
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
B |
|
634
|
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
FastTemplate doesn't use eval, and parses with a single regular |
|
636
|
|
|
|
|
|
|
expression. It just does simple variable interpolation (i.e. there is |
|
637
|
|
|
|
|
|
|
no logic that you can add to templates - you keep the logic in the code). |
|
638
|
|
|
|
|
|
|
That's why it's has 'Fast' in it's name! |
|
639
|
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
B |
|
641
|
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
FastTemplate functions accept and return references whenever possible, which saves |
|
643
|
|
|
|
|
|
|
needless copying of arguments (hashes, scalars, etc). |
|
644
|
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
B |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
The API is robust and flexible, and allows you to build very complex HTML |
|
648
|
|
|
|
|
|
|
documents or HTML interfaces. It is 100% perl and works on Unix or NT. |
|
649
|
|
|
|
|
|
|
Also, it isn't restricted to building HTML documents -- it could be used |
|
650
|
|
|
|
|
|
|
to build any ascii based document (e.g. postscript, XML, email). |
|
651
|
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
The similar modules on CPAN are: |
|
653
|
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
Module HTML::Template (S/SA/SAMTREGAR/HTML-Template-0.04.tar.gz) |
|
655
|
|
|
|
|
|
|
Module Taco::Template (KWILLIAMS/Taco-0.04.tar.gz) |
|
656
|
|
|
|
|
|
|
Module Text::BasicTemplate (D/DC/DCARRAWAY/Text-BasicTemplate-0.9.8.tar.gz) |
|
657
|
|
|
|
|
|
|
Module Text::Template (MJD/Text-Template-1.20.tar.gz) |
|
658
|
|
|
|
|
|
|
Module HTML::Mason (J/JS/JSWARTZ/HTML-Mason-0.5.1.tar.gz) |
|
659
|
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=head2 What are the steps to use FastTemplate? |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
The main steps are: |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
1. define |
|
666
|
|
|
|
|
|
|
2. assign |
|
667
|
|
|
|
|
|
|
3. parse |
|
668
|
|
|
|
|
|
|
4. print |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
These are outlined in detail in CORE METHODS below. |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
=head1 CORE METHODS |
|
673
|
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
=head2 define(HASH) |
|
675
|
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
The method define() maps a template filename to a (usually shorter) name. e.g. |
|
677
|
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
my $tpl = new FastTemplate(); |
|
679
|
|
|
|
|
|
|
$tpl->define( main => "main.tpl", |
|
680
|
|
|
|
|
|
|
footer => "footer.tpl", |
|
681
|
|
|
|
|
|
|
); |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
This new name is the name that you will use to refer to the templates. Filenames |
|
684
|
|
|
|
|
|
|
should not appear in any place other than a define(). |
|
685
|
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
(Note: This is a required step! This may seem like an annoying extra |
|
687
|
|
|
|
|
|
|
step when you are dealing with a trivial example like the one above, |
|
688
|
|
|
|
|
|
|
but when you are dealing with dozens of templates, it is very handy to |
|
689
|
|
|
|
|
|
|
refer to templates with names that are indepandant of filenames.) |
|
690
|
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
TIP: Since define() does not actually load the templates, it is faster |
|
692
|
|
|
|
|
|
|
and more legible to define all the templates with one call to define(). |
|
693
|
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
=head2 define_nofile(HASH) alias: define_raw(HASH) |
|
695
|
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
Sometimes it is desireable to not have to create a separate template file |
|
697
|
|
|
|
|
|
|
for each template (though in the long run it is usually better to do so). |
|
698
|
|
|
|
|
|
|
The method define_nofile() allows you to do this. For example, if you |
|
699
|
|
|
|
|
|
|
were writing a news tool where you wanted to bold an item if it was |
|
700
|
|
|
|
|
|
|
"new" you could do something like the following: |
|
701
|
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
my $tpl = new FastTemplate(); |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
$tpl->define_nofile( new => '$ITEM_NAME ', |
|
705
|
|
|
|
|
|
|
old => '$ITEM_NAME '); |
|
706
|
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
if ($new) |
|
708
|
|
|
|
|
|
|
{ |
|
709
|
|
|
|
|
|
|
$tpl->parse($ITEM => "new"); |
|
710
|
|
|
|
|
|
|
} |
|
711
|
|
|
|
|
|
|
else |
|
712
|
|
|
|
|
|
|
{ |
|
713
|
|
|
|
|
|
|
$tpl->parse($ITEM => "old"); |
|
714
|
|
|
|
|
|
|
} |
|
715
|
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
Of course, now you, the programmer has to update how new items are |
|
717
|
|
|
|
|
|
|
displayed, whereas if it was in a template, you could offload that task |
|
718
|
|
|
|
|
|
|
to someone else. |
|
719
|
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
=head2 define_nofile(HASH REF) alias: define_raw(HASH REF) |
|
722
|
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
A more efficient way of passing your arguments than using a real hash. |
|
724
|
|
|
|
|
|
|
Just pass in a hash reference instead of a real hash. |
|
725
|
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
=head2 assign(HASH) |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
The method assign() assigns values for variables. In order for a variable |
|
730
|
|
|
|
|
|
|
in a template to be interpolated it must be assigned. There are two forms |
|
731
|
|
|
|
|
|
|
which have some important differences. The simple form, is to accept |
|
732
|
|
|
|
|
|
|
a hash and copy all the key/value pairs into a hash in FastTemplate. |
|
733
|
|
|
|
|
|
|
There is only one hash in FastTemplate, so assigning a value for the |
|
734
|
|
|
|
|
|
|
same key will overwrite that key. |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
e.g. |
|
737
|
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
$tpl->assign(TITLE => "king kong"); |
|
739
|
|
|
|
|
|
|
$tpl->assign(TITLE => "godzilla"); ## overwrites "king kong" |
|
740
|
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
=head2 assign(HASH REF) |
|
742
|
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
A much more efficient way to pass in values is to pass in a hash |
|
744
|
|
|
|
|
|
|
reference. (This is particularly nice if you get back a hash or hash |
|
745
|
|
|
|
|
|
|
reference from a database query.) Passing a hash reference doesn't copy |
|
746
|
|
|
|
|
|
|
the data, but simply keeps the reference in an array. During parsing if |
|
747
|
|
|
|
|
|
|
the value for a variable cannot be found in the main FastTemplate hash, |
|
748
|
|
|
|
|
|
|
it starts to look through the array of hash references for the value. |
|
749
|
|
|
|
|
|
|
As soon as it finds the value it stops. It is important to remember to |
|
750
|
|
|
|
|
|
|
remove hash references when they are no longer needed. |
|
751
|
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
e.g. |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
my %foo = ("TITLE" => "king kong"); |
|
755
|
|
|
|
|
|
|
my %bar = ("TITLE" => "godzilla"); |
|
756
|
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
$tpl->assign(\%foo); ## TITLE resolves to "king kong" |
|
758
|
|
|
|
|
|
|
$tpl->clear_href(1); ## remove last hash ref assignment (\%foo) |
|
759
|
|
|
|
|
|
|
$tpl->assign(\%bar); ## TITLE resolves to "godzilla" |
|
760
|
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
$tpl->clear_href(); ## remove all hash ref assignments |
|
762
|
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
$tpl->assign(\%foo); ## TITLE resolves to "king kong" |
|
764
|
|
|
|
|
|
|
$tpl->assign(\%bar); ## TITLE _still_ resolves to "king kong" |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
=head2 parse(HASH) |
|
768
|
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
The parse function is the main function in FastTemplate. It accepts |
|
770
|
|
|
|
|
|
|
a hash, where the keys are the TARGET and the values are the SOURCE |
|
771
|
|
|
|
|
|
|
templates. There are three forms the hash can be in: |
|
772
|
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
$tpl->parse(MAIN => "main"); ## regular |
|
774
|
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
$tpl->parse(MAIN => ["table", "main"]); ## compound |
|
776
|
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
$tpl->parse(MAIN => ".row"); ## append |
|
778
|
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
In the regular version, the template named "main" is loaded if it hasn't |
|
780
|
|
|
|
|
|
|
been already, all the variables are interpolated, and the result is |
|
781
|
|
|
|
|
|
|
then stored in FastTemplate as the value MAIN. If the variable '$MAIN' |
|
782
|
|
|
|
|
|
|
shows up in a later template, it will be interpolated to be the value of |
|
783
|
|
|
|
|
|
|
the parsed "main" template. This allows you to easily nest templates, |
|
784
|
|
|
|
|
|
|
which brings us to the compound style. |
|
785
|
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
The compound style is designed to make it easier to nest templates. |
|
787
|
|
|
|
|
|
|
The following are equivalent: |
|
788
|
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
$tpl->parse(MAIN => "table"); |
|
790
|
|
|
|
|
|
|
$tpl->parse(MAIN => "main"); |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
## is the same as: |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
$tpl->parse(MAIN => ["table", "main"]); ## this form saves function calls |
|
795
|
|
|
|
|
|
|
## (and makes your code cleaner) |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
It is important to note that when you are using the compound form, |
|
798
|
|
|
|
|
|
|
each template after the first, must contain the variable that you are |
|
799
|
|
|
|
|
|
|
parsing the results into. In the above example, 'main' must contain |
|
800
|
|
|
|
|
|
|
the variable '$MAIN', as that is where the parsed results of 'table' |
|
801
|
|
|
|
|
|
|
is stored. If 'main' does not contain the variable '$MAIN' then the |
|
802
|
|
|
|
|
|
|
parsed results of 'table' will be lost. |
|
803
|
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
The append style is a bit of a kludge, but it allows you to append |
|
805
|
|
|
|
|
|
|
the parsed results to the target variable. This is most useful when |
|
806
|
|
|
|
|
|
|
building tables that have an dynamic number of rows - such as data from |
|
807
|
|
|
|
|
|
|
a database query. |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
=head2 strict() |
|
810
|
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
When strict() is on (it is on by default) all variables found during |
|
812
|
|
|
|
|
|
|
template parsing that are unresolved have a warning printed to STDERR. |
|
813
|
|
|
|
|
|
|
e.g. |
|
814
|
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
[CGI::FastTemplate] Warning: no value found for variable: SOME_VARIABLE |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
Also, new as of version 1.04 the variables will be left in the output |
|
818
|
|
|
|
|
|
|
document. This was done for two reasons: to allow for parsing to be done |
|
819
|
|
|
|
|
|
|
in stages (i.e. multiple passes), and to make it easier to identify |
|
820
|
|
|
|
|
|
|
undefined variables since they appear in the parsed output. |
|
821
|
|
|
|
|
|
|
If you have been using an earlier version of FastTemplate and you want |
|
822
|
|
|
|
|
|
|
the old behavior of replacing unknown variables with an empty string, |
|
823
|
|
|
|
|
|
|
see: no_strict(). |
|
824
|
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
Note: version 1.07 adds support for two styles of variables, so that the |
|
826
|
|
|
|
|
|
|
following are equivalent: $VAR and ${VAR} However, when using strict(), |
|
827
|
|
|
|
|
|
|
variables with curly brackets that are not resolved are outputted as |
|
828
|
|
|
|
|
|
|
plain variables. e.g. if ${VAR} has no value assigned to it, it would |
|
829
|
|
|
|
|
|
|
appear in the output as $VAR. This is a slight inconsistency -- ideally |
|
830
|
|
|
|
|
|
|
the unresolved variable would remain unchanged. |
|
831
|
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
Note: STDERR output should be captured and logged by the webserver so you |
|
833
|
|
|
|
|
|
|
can just tail the error log to see the output. |
|
834
|
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
e.g. |
|
836
|
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
tail -f /etc/httpd/logs/error_log |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
=head2 no_strict() |
|
840
|
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
Turns off warning messages about unresolved template variables. |
|
842
|
|
|
|
|
|
|
As of version 1.04 a call to no_strict() is required to replace unknown |
|
843
|
|
|
|
|
|
|
variables with an empty string. By default, all instances of FastTemplate |
|
844
|
|
|
|
|
|
|
behave as is strict() was called. Also, no_strict() must be set for |
|
845
|
|
|
|
|
|
|
each instance of CGI::FastTemplate. e.g. |
|
846
|
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
CGI::FastTemplate::no_strict; ## no |
|
848
|
|
|
|
|
|
|
|
|
849
|
|
|
|
|
|
|
my $tpl = CGI::FastTemplate; |
|
850
|
|
|
|
|
|
|
$tpl->no_strict; ## yes |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
=head2 print(SCALAR) |
|
854
|
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
The method print() prints the contents of the named variable. If no |
|
856
|
|
|
|
|
|
|
variable is given, then it prints the last variable that was used in a |
|
857
|
|
|
|
|
|
|
call to parse which I find is a reasonable default. |
|
858
|
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
e.g. |
|
860
|
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
$tpl->parse(MAIN => "main"); |
|
862
|
|
|
|
|
|
|
$tpl->print(); ## prints value of MAIN |
|
863
|
|
|
|
|
|
|
$tpl->print("MAIN"); ## same |
|
864
|
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
This method is provided for convenience. |
|
866
|
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
If you need to print other than STDOUT (e.g. socket, file handle) see fetch(). |
|
868
|
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
=head1 OTHER METHODS |
|
870
|
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
=head2 fetch(SCALAR) |
|
872
|
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
Returns a scalar reference to parsed data. |
|
874
|
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
$tpl->parse(CONTENT => "main"); |
|
876
|
|
|
|
|
|
|
my $content = $tpl->fetch("CONTENT"); |
|
877
|
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
print $$content; ## print to STDOUT |
|
879
|
|
|
|
|
|
|
print FILE $$content; ## print to filehandle or pipe |
|
880
|
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
=head2 clear() |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
Note: All of 'clear' functions are for use under mod_perl (or anywhere |
|
885
|
|
|
|
|
|
|
where your scripts are persistant). They generally aren't needed if |
|
886
|
|
|
|
|
|
|
you are writing CGI scripts. |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
Clears the internal hash that stores data passed from calls to assign() and parse(). |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
Often clear() is at the end of a mod_perl script: |
|
891
|
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
$tpl->print(); |
|
893
|
|
|
|
|
|
|
$tpl->clear(); |
|
894
|
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
=head2 clear(ARRAY) |
|
896
|
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
With no arguments, all assigned or parsed variables are cleared, but if passed an ARRAY of variable names, then only |
|
898
|
|
|
|
|
|
|
those variables will be cleared. |
|
899
|
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
e.g. |
|
901
|
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
$tpl->assign(TITLE => "Welcome"); |
|
903
|
|
|
|
|
|
|
$tpl->clear("TITLE"); ## title is now empty |
|
904
|
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
Another way of achieving the same effect of clearnign variables is to just assign an empty string. |
|
906
|
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
e.g. |
|
908
|
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
$tpl->assign(TITLE => ''); ## same as: $tpl->clear("TITLE"); |
|
910
|
|
|
|
|
|
|
|
|
911
|
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
=head2 clear_parse() |
|
913
|
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
See: clear() |
|
915
|
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
=head2 clear_href(NUMBER) |
|
917
|
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
Removes a given number of hash references from the list of hash refs that is built using: |
|
919
|
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
$tpl->assign(HASH REF); |
|
921
|
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
If called with no arguments, it removes all hash references |
|
923
|
|
|
|
|
|
|
from the array. This is often used for database queries where each row from the query |
|
924
|
|
|
|
|
|
|
is a hash or hash reference. |
|
925
|
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
e.g. |
|
927
|
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
while($hash_row = $sth->fetchrow_hashref) |
|
929
|
|
|
|
|
|
|
{ |
|
930
|
|
|
|
|
|
|
$tpl->assign($hash_row); |
|
931
|
|
|
|
|
|
|
$tpl->parse(ROW => ".row"); |
|
932
|
|
|
|
|
|
|
$tpl->clear_href(1); |
|
933
|
|
|
|
|
|
|
} |
|
934
|
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
=head2 clear_define() |
|
938
|
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
Clears the internal hash that stores data passed to: |
|
940
|
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
$tpl->define(); |
|
942
|
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
Note: The hash that holds the loaded templates is not touched with |
|
944
|
|
|
|
|
|
|
this method. See: clear_tpl |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
=head2 clear_tpl() clear_tpl(NAME) |
|
948
|
|
|
|
|
|
|
|
|
949
|
|
|
|
|
|
|
The first time a template is used, it is loaded and stored in a hash |
|
950
|
|
|
|
|
|
|
in memory. clear_tpl() removes all the templates being held in memory. |
|
951
|
|
|
|
|
|
|
clear_tpl(NAME) only removes the one with NAME. This is generally not |
|
952
|
|
|
|
|
|
|
required for normal CGI programming, but if you have long running scripts |
|
953
|
|
|
|
|
|
|
(e.g. mod_perl) and have very large templates that a re infrequently |
|
954
|
|
|
|
|
|
|
used gives you some control over how memory is being used. |
|
955
|
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
|
|
957
|
|
|
|
|
|
|
=head2 clear_all() |
|
958
|
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
Cleans the module of any data, except for the ROOT directory. Equivalent to: |
|
960
|
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
$tpl->clear_define(); |
|
962
|
|
|
|
|
|
|
$tpl->clear_href(); |
|
963
|
|
|
|
|
|
|
$tpl->clear_tpl(); |
|
964
|
|
|
|
|
|
|
$tpl->clear_parse(); |
|
965
|
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
=head2 Variables |
|
967
|
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
A variable is defined as: |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
$[A-Z0-9][A-Z0-9_]+ |
|
971
|
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
|
|
973
|
|
|
|
|
|
|
This means, that a variable must begin with a dollar sign '$'. |
|
974
|
|
|
|
|
|
|
The second character must be an uppercase letter or digit 'A-Z0-9'. |
|
975
|
|
|
|
|
|
|
Remaining characters can include an underscore. |
|
976
|
|
|
|
|
|
|
|
|
977
|
|
|
|
|
|
|
As of version 1.07 variables can also be delimited by curly brackets. |
|
978
|
|
|
|
|
|
|
|
|
979
|
|
|
|
|
|
|
${[A-Z0-9][A-Z0-9_]+} |
|
980
|
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
For example, the following are valid variables: |
|
982
|
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
$FOO |
|
984
|
|
|
|
|
|
|
$F123F |
|
985
|
|
|
|
|
|
|
$TOP_OF_PAGE |
|
986
|
|
|
|
|
|
|
${NEW_STYLE} |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
=head2 Variable Interpolation (Template Parsing) |
|
989
|
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
When the a template is being scanned for variables, pattern matching |
|
991
|
|
|
|
|
|
|
is greedy. (For more info on "greediness" of regexps see L.) |
|
992
|
|
|
|
|
|
|
This is important, because if there are valid variable characters after |
|
993
|
|
|
|
|
|
|
your variable, FastTemplate will consider them to be part of the variable. |
|
994
|
|
|
|
|
|
|
As of version 1.07 you can use curly brackets as delimiters for your |
|
995
|
|
|
|
|
|
|
variable names. e.g. ${VARIABLE} You do not need to use curly brackets |
|
996
|
|
|
|
|
|
|
if the character immediately after your variable name is not an uppercase |
|
997
|
|
|
|
|
|
|
letter, digit or underscore. ['A-Z0-9_'] |
|
998
|
|
|
|
|
|
|
|
|
999
|
|
|
|
|
|
|
If a variable cannot be resolved to a value then there are two |
|
1000
|
|
|
|
|
|
|
possibilities. If strict() has been called (it is on by default) then |
|
1001
|
|
|
|
|
|
|
the variable remains and a warning is printed to STDERR. If no_strict() |
|
1002
|
|
|
|
|
|
|
has been called then the variables is converted to an empty string ['']. |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
See L and L for more info. |
|
1005
|
|
|
|
|
|
|
|
|
1006
|
|
|
|
|
|
|
Some examples will make this clearer. |
|
1007
|
|
|
|
|
|
|
|
|
1008
|
|
|
|
|
|
|
Assume: |
|
1009
|
|
|
|
|
|
|
|
|
1010
|
|
|
|
|
|
|
$FOO = "foo"; |
|
1011
|
|
|
|
|
|
|
$BAR = "bar"; |
|
1012
|
|
|
|
|
|
|
$ONE = "1"; |
|
1013
|
|
|
|
|
|
|
$TWO = "2"; |
|
1014
|
|
|
|
|
|
|
$UND = "_"; |
|
1015
|
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
Variable Interpolated/Parsed |
|
1017
|
|
|
|
|
|
|
------------------------------------------------ |
|
1018
|
|
|
|
|
|
|
$FOO foo |
|
1019
|
|
|
|
|
|
|
$FOO-$BAR foo-bar |
|
1020
|
|
|
|
|
|
|
$ONE_$TWO 2 ## $ONE_ is undefined! |
|
1021
|
|
|
|
|
|
|
$ONE_$TWO $ONE_2 ## assume: strict() |
|
1022
|
|
|
|
|
|
|
$ONE$UND$TWO 1_2 ## kludge! |
|
1023
|
|
|
|
|
|
|
${ONE}_$TWO 1_2 ## much better |
|
1024
|
|
|
|
|
|
|
$$FOO $foo |
|
1025
|
|
|
|
|
|
|
$25,000 $25,000 |
|
1026
|
|
|
|
|
|
|
|
|
1027
|
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
|
|
1029
|
|
|
|
|
|
|
=head2 FULL EXAMPLE |
|
1030
|
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
This example will build an HTML page that will consist of a table. |
|
1032
|
|
|
|
|
|
|
The table will have 3 numbered rows. The first step is to decide what |
|
1033
|
|
|
|
|
|
|
templates we need. In order to make it easy for the table to change to |
|
1034
|
|
|
|
|
|
|
a different number of rows, we will have a template for the rows of the |
|
1035
|
|
|
|
|
|
|
table, another for the table, and a third for the head/body part of the |
|
1036
|
|
|
|
|
|
|
HTML page. |
|
1037
|
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
Below are the templates. (Pretend each one is in a separate file.) |
|
1039
|
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
|
|
1041
|
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
|
|
1043
|
|
|
|
|
|
|
$TITLE |
|
1044
|
|
|
|
|
|
|
|
|
1045
|
|
|
|
|
|
|
|
|
1046
|
|
|
|
|
|
|
$MAIN |
|
1047
|
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
|
|
1049
|
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
|
|
1053
|
|
|
|
|
|
|
|
|
1056
|
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
|
|
1059
|
|
|
|
|
|
|
|
|
1060
|
|
|
|
|
|
|
|
|
1061
|
|
|
|
|
|
|
| $NUMBER |
|
1062
|
|
|
|
|
|
|
| $BIG_NUMBER |
|
1063
|
|
|
|
|
|
|
|
|
1064
|
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
|
|
1066
|
|
|
|
|
|
|
Now we can start coding... |
|
1067
|
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
## START ## |
|
1069
|
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
use CGI::FastTemplate; |
|
1071
|
|
|
|
|
|
|
my $tpl = new CGI::FastTemplate("/path/to/template/files"); |
|
1072
|
|
|
|
|
|
|
|
|
1073
|
|
|
|
|
|
|
$tpl->define( main => "main.tpl", |
|
1074
|
|
|
|
|
|
|
table => "table.tpl", |
|
1075
|
|
|
|
|
|
|
row => "row.tpl", |
|
1076
|
|
|
|
|
|
|
); |
|
1077
|
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
$tpl->assign(TITLE => "FastTemplate Test"); |
|
1079
|
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
for $n (1..3) |
|
1081
|
|
|
|
|
|
|
{ |
|
1082
|
|
|
|
|
|
|
$tpl->assign( NUMBER => $n, |
|
1083
|
|
|
|
|
|
|
BIG_NUMBER => $n*10); |
|
1084
|
|
|
|
|
|
|
$tpl->parse(ROWS => ".row"); |
|
1085
|
|
|
|
|
|
|
} |
|
1086
|
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
$tpl->parse(MAIN => ["table", "main"]); |
|
1088
|
|
|
|
|
|
|
$tpl->print(); |
|
1089
|
|
|
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
## END ## |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
When run it returns: |
|
1093
|
|
|
|
|
|
|
|
|
1094
|
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
FastTemplate Test |
|
1097
|
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
|
|
1122
|
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
|
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
|
|
1126
|
|
|
|
|
|
|
|
|
1127
|
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
If you're thinking you could have done the same thing in a few lines |
|
1129
|
|
|
|
|
|
|
of plain perl, well yes you probably could. But, how would a graphic |
|
1130
|
|
|
|
|
|
|
designer tweak the resulting HTML? How would you have a designer editing |
|
1131
|
|
|
|
|
|
|
the HTML while you're editing another part of the code? How would |
|
1132
|
|
|
|
|
|
|
you save the output to a file, or pipe it to another application |
|
1133
|
|
|
|
|
|
|
(e.g. sendmail)? How would you make your application multi-lingual? |
|
1134
|
|
|
|
|
|
|
How would you build an application that has options for high graphics, |
|
1135
|
|
|
|
|
|
|
or text-only? FastTemplate really starts to shine when you are building |
|
1136
|
|
|
|
|
|
|
mid to large scale web applications, simply because it begins to separate |
|
1137
|
|
|
|
|
|
|
the application's generic logic from the specific implementation. |
|
1138
|
|
|
|
|
|
|
|
|
1139
|
|
|
|
|
|
|
|
|
1140
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
1141
|
|
|
|
|
|
|
|
|
1142
|
|
|
|
|
|
|
Copyright (c) 1998-99 Jason Moore . All rights |
|
1143
|
|
|
|
|
|
|
reserved. |
|
1144
|
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
|
1146
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
|
1147
|
|
|
|
|
|
|
|
|
1148
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
|
1149
|
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
1150
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
1151
|
|
|
|
|
|
|
Artistic License for more details. |
|
1152
|
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
=head1 AUTHOR |
|
1154
|
|
|
|
|
|
|
|
|
1155
|
|
|
|
|
|
|
Jason Moore |
|
1156
|
|
|
|
|
|
|
|
|
1157
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
1158
|
|
|
|
|
|
|
|
|
1159
|
|
|
|
|
|
|
mod_perl(1). |
|
1160
|
|
|
|
|
|
|
|
|
1161
|
|
|
|
|
|
|
=cut |
|
1162
|
|
|
|
|
|
|
|
|
1163
|
|
|
|
|
|
|
|