6 Einträge, 1 Seite |
QuoteBTW Deutsche Umlaute sind in ISO-8859-1 enthalten, da es sich hierbei um Latin 1 handelt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl
use warnings;
use strict;
use HTML::Template;
# oeffne das html-Template
my $template = HTML::Template->new(filename => 'test.tmpl'); # Fehlerabfrage: siehe unten
# Parameter einfuellen
$template->param(HOME => $ENV{HOME});
$template->param(PATH => $ENV{PATH});
# den nötigen Content-Type senden und dann das gefuellte Template ausgeben
print "Content-Type: text/html\n\n";
print $template->output;
1
2
Mein Home-Verzeichnis ist /home/some/directory
Mein PATH ist auf /bin;/usr/bin gesetzt.
1
2
3
4
5
6
7
$template->param(
EMPLOYEE_INFO => [
{ name => 'Sam', job => 'Programmierer' },
{ name => 'Steve', job => 'Administrator' },
]
);
print $template->output();
QuoteName: Sam
Job: Programmierer
Name: Steve
Job: Administrator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ein paar Listen von Daten, die in die Schleife gepackt werden sollen:
my @words = qw(I Am Cool);
my @numbers = qw(1 2 3);
my @loop_data = (); # Initialisiere eine Liste, die die Schleifendaten aufnehmen soll
while (@words and @numbers) {
my %row_data; # verwende einen neuen Hash fuer die Daten der Zeile
# Fuelle die Zeile
$row_data{WORD} = shift @words;
$row_data{NUMBER} = shift @numbers;
# Der kritische Punkt: werfe eine Referenz auf diese Zeile
# auf die Schleife
push(@loop_data, \%row_data);
}
# weise die Schleifendaten dem Schleifenparameter als Referenz zu
$template->param(THIS_LOOP => \@loop_data);
QuoteWord: I
Number: 1
Word: Am
Number: 2
Word: Cool
Number: 3
1
2
3
4
5
6
7
8
9
10
$template->param(
LOOP => [
{ name => 'Bobby',
nicknames => [
{ name => 'the big bad wolf' },
{ name => 'He-Man' },
],
},
],
);
1 2 3 4 5 6 7
my $template = HTML::Template->new( filename => 'file.tmpl', option => 'value' );
1 2 3 4 5 6 7
my $t = HTML::Template->new( scalarref => $ref_to_template_text, option => 'value' );
1 2 3 4 5 6 7
my $t = HTML::Template->new( arrayref => $ref_to_array_of_lines , option => 'value' );
my $t = HTML::Template->new( filehandle => *FH, option => 'value');
my $t = HTML::Template->new_file('file.tmpl', option => 'value');
1 2 3 4
my $t = HTML::Template->new_scalar_ref($ref_to_template_text, option => 'value');
1 2 3 4
my $t = HTML::Template->new_array_ref($ref_to_array_of_lines, option => 'value');
$template->param(parameterName => 'wert')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
Caching Options
* cache - if set to 1 the module will cache in memory the parsed
templates based on the filename parameter and modification date
of the file. This only applies to templates opened with the
filename parameter specified, not scalarref or arrayref
templates. Caching also looks at the modification times of any
files included using <TMPL_INCLUDE> tags, but again, only if the
template is opened with filename parameter.
This is mainly of use in a persistent environment like
Apache/mod_perl. It has absolutely no benefit in a normal CGI
environment since the script is unloaded from memory after every
request. For a cache that does work for normal CGIs see the
'shared_cache' option below.
Note that different new() parameter settings do not cause a
cache refresh, only a change in the modification time of the
template will trigger a cache refresh. For most usages this is
fine. My simplistic testing shows that using cache yields a 90%
performance increase under mod_perl. Cache defaults to 0.
* shared_cache - if set to 1 the module will store its cache in
shared memory using the IPC::SharedCache module (available from
CPAN). The effect of this will be to maintain a single shared
copy of each parsed template for all instances of HTML::Template
to use. This can be a significant reduction in memory usage in a
multiple server environment. As an example, on one of our
systems we use 4MB of template cache and maintain 25 httpd
processes - shared_cache results in saving almost 100MB! Of
course, some reduction in speed versus normal caching is to be
expected. Another difference between normal caching and
shared_cache is that shared_cache will work in a CGI environment
- normal caching is only useful in a persistent environment like
Apache/mod_perl.
By default HTML::Template uses the IPC key 'TMPL' as a shared
root segment (0x4c504d54 in hex), but this can be changed by
setting the 'ipc_key' new() parameter to another 4-character or
integer key. Other options can be used to affect the shared
memory cache correspond to IPC::SharedCache options - ipc_mode,
ipc_segment_size and ipc_max_size. See IPC::SharedCache for a
description of how these work - in most cases you shouldn't need
to change them from the defaults.
For more information about the shared memory cache system used
by HTML::Template see IPC::SharedCache.
* double_cache - if set to 1 the module will use a combination of
shared_cache and normal cache mode for the best possible
caching. Of course, it also uses the most memory of all the
cache modes. All the same ipc_* options that work with
shared_cache apply to double_cache as well. By default
double_cache is off.
* blind_cache - if set to 1 the module behaves exactly as with
normal caching but does not check to see if the file has changed
on each request. This option should be used with caution, but
could be of use on high-load servers. My tests show blind_cache
performing only 1 to 2 percent faster than cache under mod_perl.
NOTE: Combining this option with shared_cache can result in
stale templates stuck permanently in shared memory!
* file_cache - if set to 1 the module will store its cache in a
file using the Storable module. It uses no additional memory,
and my simplistic testing shows that it yields a 50% performance
advantage. Like shared_cache, it will work in a CGI environment.
Default is 0.
If you set this option you must set the "file_cache_dir" option.
See below for details.
NOTE: Storable using flock() to ensure safe access to cache
files. Using file_cache on a system or filesystem (NFS) without
flock() support is dangerous.
* file_cache_dir - sets the directory where the module will store
the cache files if file_cache is enabled. Your script will need
write permissions to this directory. You'll also need to make
sure the sufficient space is available to store the cache files.
* file_cache_dir_mode - sets the file mode for newly created
file_cache directories and subdirectories. Defaults to 0700 for
security but this may be inconvenient if you do not have access
to the account running the webserver.
* double_file_cache - if set to 1 the module will use a
combination of file_cache and normal cache mode for the best
possible caching. The file_cache_* options that work with
file_cache apply to double_file_cache as well. By default
double_file_cache is 0.
Filesystem Options
* path - you can set this variable with a list of paths to search
for files specified with the "filename" option to new() and for
files included with the <TMPL_INCLUDE> tag. This list is only
consulted when the filename is relative. The HTML_TEMPLATE_ROOT
environment variable is always tried first if it exists. Also,
if HTML_TEMPLATE_ROOT is set then an attempt will be made to
prepend HTML_TEMPLATE_ROOT onto paths in the path array. In the
case of a <TMPL_INCLUDE> file, the path to the including file is
also tried before path is consulted.
Example:
my $template = HTML::Template->new( filename => 'file.tmpl',
path => [ '/path/to/templates',
'/alternate/path'
]
);
NOTE: the paths in the path list must be expressed as UNIX
paths, separated by the forward-slash character ('/').
* search_path_on_include - if set to a true value the module will
search from the top of the array of paths specified by the path
option on every <TMPL_INCLUDE> and use the first matching
template found. The normal behavior is to look only in the
current directory for a template to include. Defaults to 0.
Debugging Options
* debug - if set to 1 the module will write random debugging
information to STDERR. Defaults to 0.
* stack_debug - if set to 1 the module will use Data::Dumper to
print out the contents of the parse_stack to STDERR. Defaults to
0.
* cache_debug - if set to 1 the module will send information on
cache loads, hits and misses to STDERR. Defaults to 0.
* shared_cache_debug - if set to 1 the module will turn on the
debug option in IPC::SharedCache - see IPC::SharedCache for
details. Defaults to 0.
* memory_debug - if set to 1 the module will send information on
cache memory usage to STDERR. Requires the GTop module. Defaults
to 0.
Miscellaneous Options
* associate - this option allows you to inherit the parameter
values from other objects. The only requirement for the other
object is that it have a param() method that works like
HTML::Template's param(). A good candidate would be a CGI.pm
query object. Example:
my $query = new CGI;
my $template = HTML::Template->new(filename => 'template.tmpl',
associate => $query);
Now, $template->output() will act as though
$template->param('FormField', $cgi->param('FormField'));
had been specified for each key/value pair that would be
provided by the $cgi->param() method. Parameters you set
directly take precedence over associated parameters.
You can specify multiple objects to associate by passing an
anonymous array to the associate option. They are searched for
parameters in the order they appear:
my $template = HTML::Template->new(filename => 'template.tmpl',
associate => [$query, $other_obj]);
The old associateCGI() call is still supported, but should be
considered obsolete.
NOTE: The parameter names are matched in a case-insensitve
manner. If you have two parameters in a CGI object like 'NAME'
and 'Name' one will be chosen randomly by associate. This
behavior can be changed by the following option.
* case_sensitive - setting this option to true causes
HTML::Template to treat template variable names
case-sensitively. The following example would only set one
parameter without the "case_sensitive" option:
my $template = HTML::Template->new(filename => 'template.tmpl',
case_sensitive => 1);
$template->param(
FieldA => 'foo',
fIELDa => 'bar',
);
This option defaults to off.
NOTE: with case_sensitive and loop_context_vars the special loop
variables are available in lower-case only.
* loop_context_vars - when this parameter is set to true (it is
false by default) four loop context variables are made available
inside a loop: , , , . They can
be used with <TMPL_IF>, <TMPL_UNLESS> and <TMPL_ELSE> to control
how a loop is output.
In addition to the above, a var is also made
available when loop context variables are turned on.
Example:
<TMPL_LOOP NAME="FOO">
<TMPL_IF NAME="">
This only outputs on the first pass.
</TMPL_IF>
<TMPL_IF NAME="">
This outputs every other pass, on the odd passes.
</TMPL_IF>
<TMPL_UNLESS NAME="">
This outputs every other pass, on the even passes.
</TMPL_IF>
<TMPL_IF NAME="">
This outputs on passes that are neither first nor last.
</TMPL_IF>
This is pass number <TMPL_VAR NAME="">.
<TMPL_IF NAME="">
This only outputs on the last pass.
<TMPL_IF>
</TMPL_LOOP>
One use of this feature is to provide a "separator" similar in
effect to the perl function join(). Example:
<TMPL_LOOP FRUIT>
<TMPL_IF > and </TMPL_IF>
<TMPL_VAR KIND><TMPL_UNLESS >, <TMPL_ELSE>.</TMPL_UNLESS>
</TMPL_LOOP>
Would output (in a browser) something like:
Apples, Oranges, Brains, Toes, and Kiwi.
Given an appropriate param() call, of course. NOTE: A loop with
only a single pass will get both and set to
true, but not .
* no_includes - set this option to 1 to disallow the
<TMPL_INCLUDE> tag in the template file. This can be used to
make opening untrusted templates slightly less dangerous.
Defaults to 0.
* max_includes - set this variable to determine the maximum depth
that includes can reach. Set to 10 by default. Including files
to a depth greater than this value causes an error message to be
displayed. Set to 0 to disable this protection.
* global_vars - normally variables declared outside a loop are not
available inside a loop. This option makes <TMPL_VAR>s like
global variables in Perl - they have unlimited scope. This
option also affects <TMPL_IF> and <TMPL_UNLESS>.
Example:
This is a normal variable: <TMPL_VAR NORMAL>.<P>
<TMPL_LOOP NAME=FROOT_LOOP>
Here it is inside the loop: <TMPL_VAR NORMAL><P>
</TMPL_LOOP>
Normally this wouldn't work as expected, since <TMPL_VAR
NORMAL>'s value outside the loop is not available inside the
loop.
The global_vars option also allows you to access the values of
an enclosing loop within an inner loop. For example, in this
loop the inner loop will have access to the value of OUTER_VAR
in the correct iteration:
<TMPL_LOOP OUTER_LOOP>
OUTER: <TMPL_VAR OUTER_VAR>
<TMPL_LOOP INNER_LOOP>
INNER: <TMPL_VAR INNER_VAR>
INSIDE OUT: <TMPL_VAR OUTER_VAR>
</TMPL_LOOP>
</TMPL_LOOP>
* filter - this option allows you to specify a filter for your
template files. A filter is a subroutine that will be called
after HTML::Template reads your template file but before it
starts parsing template tags.
In the most simple usage, you simply assign a code reference to
the filter parameter. This subroutine will recieve a single
arguement - a reference to a string containing the template file
text. Here is an example that accepts templates with tags that
look like "!!!ZAP_VAR FOO!!!" and transforms them into
HTML::Template tags:
my $filter = sub {
my $text_ref = shift;
$$text_ref =~ s/!!!ZAP_(.*?)!!!/<TMPL_$1>/g;
};
# open zap.tmpl using the above filter
my $template = HTML::Template->new(filename => 'zap.tmpl',
filter => $filter);
More complicated usages are possible. You can request that your
filter receieve the template text as an array of lines rather
than as a single scalar. To do that you need to specify your
filter using a hash-ref. In this form you specify the filter
using the "sub" key and the desired argument format using the
"format" key. The available formats are "scalar" and "array".
Using the "array" format will incur a performance penalty but
may be more convenient in some situations.
my $template = HTML::Template->new(filename => 'zap.tmpl',
filter => { sub => $filter,
format => 'array' });
You may also have multiple filters. This allows simple filters
to be combined for more elaborate functionality. To do this you
specify an array of filters. The filters are applied in the
order they are specified.
my $template = HTML::Template->new(filename => 'zap.tmpl',
filter => [
{ sub => \&decompress,
format => 'scalar' },
{ sub => \&remove_spaces,
format => 'array' }
]);
The specified filters will be called for any TMPL_INCLUDEed
files just as they are for the main template file.
param()
param() can be called in a number of ways
1) To return a list of parameters in the template :
my @parameter_names = $self->param();
2) To return the value set to a param :
my $value = $self->param('PARAM');
3) To set the value of a parameter :
# For simple TMPL_VARs:
$self->param(PARAM => 'value');
# with a subroutine reference that gets called to get the value
# of the scalar. The sub will recieve the template object as a
# parameter.
$self->param(PARAM => sub { return 'value' });
# And TMPL_LOOPs:
$self->param(LOOP_PARAM =>
[
{ PARAM => VALUE_FOR_FIRST_PASS, ... },
{ PARAM => VALUE_FOR_SECOND_PASS, ... }
...
]
);
4) To set the value of a a number of parameters :
# For simple TMPL_VARs:
$self->param(PARAM => 'value',
PARAM2 => 'value'
);
# And with some TMPL_LOOPs:
$self->param(PARAM => 'value',
PARAM2 => 'value',
LOOP_PARAM =>
[
{ PARAM => VALUE_FOR_FIRST_PASS, ... },
{ PARAM => VALUE_FOR_SECOND_PASS, ... }
...
],
ANOTHER_LOOP_PARAM =>
[
{ PARAM => VALUE_FOR_FIRST_PASS, ... },
{ PARAM => VALUE_FOR_SECOND_PASS, ... }
...
]
);
5) To set the value of a a number of parameters using a hash-ref :
$self->param(
{
PARAM => 'value',
PARAM2 => 'value',
LOOP_PARAM =>
[
{ PARAM => VALUE_FOR_FIRST_PASS, ... },
{ PARAM => VALUE_FOR_SECOND_PASS, ... }
...
],
ANOTHER_LOOP_PARAM =>
[
{ PARAM => VALUE_FOR_FIRST_PASS, ... },
{ PARAM => VALUE_FOR_SECOND_PASS, ... }
...
]
}
);
clear_params()
Sets all the parameters to undef. Useful internally, if nowhere else!
output()
output() returns the final result of the template. In most situations
you'll want to print this, like:
print $template->output();
When output is called each occurrence of <TMPL_VAR NAME=name> is
replaced with the value assigned to "name" via param(). If a named
parameter is unset it is simply replaced with ''. <TMPL_LOOPS> are
evaluated once per parameter set, accumlating output on each pass.
Calling output() is guaranteed not to change the state of the Template
object, in case you were wondering. This property is mostly important
for the internal implementation of loops.
You may optionally supply a filehandle to print to automatically as the
template is generated. This may improve performance and lower memory
consumption. Example:
$template->output(print_to => *STDOUT);
The return value is undefined when using the "print_to" option.
query()
This method allow you to get information about the template structure.
It can be called in a number of ways. The simplest usage of query is
simply to check whether a parameter name exists in the template, using
the "name" option:
if ($template->query(name => 'foo')) {
# do something if a varaible of any type
# named FOO is in the template
}
This same usage returns the type of the parameter. The type is the same
as the tag minus the leading 'TMPL_'. So, for example, a TMPL_VAR
parameter returns 'VAR' from query().
if ($template->query(name => 'foo') eq 'VAR') {
# do something if FOO exists and is a TMPL_VAR
}
Note that the variables associated with TMPL_IFs and TMPL_UNLESSs will
be identified as 'VAR' unless they are also used in a TMPL_LOOP, in
which case they will return 'LOOP'.
"query()" also allows you to get a list of parameters inside a loop (and
inside loops inside loops). Example loop:
<TMPL_LOOP NAME="EXAMPLE_LOOP">
<TMPL_VAR NAME="BEE">
<TMPL_VAR NAME="BOP">
<TMPL_LOOP NAME="EXAMPLE_INNER_LOOP">
<TMPL_VAR NAME="INNER_BEE">
<TMPL_VAR NAME="INNER_BOP">
</TMPL_LOOP>
</TMPL_LOOP>
And some query calls:
# returns 'LOOP'
$type = $template->query(name => 'EXAMPLE_LOOP');
# returns ('bop', 'bee', 'example_inner_loop')
@param_names = $template->query(loop => 'EXAMPLE_LOOP');
# both return 'VAR'
$type = $template->query(name => ['EXAMPLE_LOOP', 'BEE']);
$type = $template->query(name => ['EXAMPLE_LOOP', 'BOP']);
# and this one returns 'LOOP'
$type = $template->query(name => ['EXAMPLE_LOOP',
'EXAMPLE_INNER_LOOP']);
# and finally, this returns ('inner_bee', 'inner_bop')
@inner_param_names = $template->query(loop => ['EXAMPLE_LOOP',
'EXAMPLE_INNER_LOOP']);
# for non existent parameter names you get undef
# this returns undef.
$type = $template->query(name => 'DWEAZLE_ZAPPA');
# calling loop on a non-loop parameter name will cause an error.
# this dies:
$type = $template->query(loop => 'DWEAZLE_ZAPPA');
As you can see above the "loop" option returns a list of parameter names
and both "name" and "loop" take array refs in order to refer to
parameters inside loops. It is an error to use "loop" with a parameter
that is not a loop.
Note that all the names are returned in lowercase and the types are
uppercase.
Just like "param()", "query()" with no arguements returns all the
parameter names in the template at the top level.
FREQUENTLY ASKED QUESTIONS
In the interest of greater understanding I've started a FAQ section of
the perldocs. Please look in here before you send me email.
1 Q: Is there a place to go to discuss HTML::Template and/or get help?
A: There's a mailing-list for discussing HTML::Template at
html-template-users@lists.sourceforge.net. To join:
[URL=http://lists.sourceforge.net/lists/listinfo/html-template-users]http://lists.sourceforge.net/lists/listinfo/html-template-users[/URL]
If you just want to get email when new releases are available you
can join the announcements mailing-list here:
[URL=http://lists.sourceforge.net/lists/listinfo/html-template-announce]http://lists.sourceforge.net/lists/listinfo/html-template-announce[/URL]
2 Q: Is there a searchable archive for the mailing-list?
A: Yes, you can find an archive of the SourceForge list here:
[URL=http://www.geocrawler.com/lists/3/SourceForge/23294/0/]http://www.geocrawler.com/lists/3/SourceForge/23294/0/[/URL]
For an archive of the old vm.com list, setup by Sean P. Scanlon,
see:
[URL=http://bluedot.net/mail/archive/]http://bluedot.net/mail/archive/[/URL]
3 Q: I want support for <TMPL_XXX>! How about it?
A: Maybe. I definitely encourage people to discuss their ideas for
HTML::Template on the mailing list. Please be ready to explain to me
how the new tag fits in with HTML::Template's mission to provide a
fast, lightweight system for using HTML templates.
NOTE: Offering to program said addition and provide it in the form
of a patch to the most recent version of HTML::Template will
definitely have a softening effect on potential opponents!
4 Q: I found a bug, can you fix it?
A: That depends. Did you send me the VERSION of HTML::Template, a
test script and a test template? If so, then almost certainly.
If you're feeling really adventurous, HTML::Template has a
publically available CVS server. See below for more information in
the PUBLIC CVS SERVER section.
5 Q: <TMPL_VAR>s from the main template aren't working inside a
<TMPL_LOOP>! Why?
A: This is the intended behavior. <TMPL_LOOP> introduces a separate
scope for <TMPL_VAR>s much like a subroutine call in Perl introduces
a separate scope for "my" variables.
If you want your <TMPL_VAR>s to be global you can set the
'global_vars' option when you call new(). See above for
documentation of the 'global_vars' new() option.
6 Q: Why do you use /[Tt]/ instead of /t/i? It's so ugly!
A: Simple - the case-insensitive match switch is very inefficient.
According to _Mastering_Regular_Expressions_ from O'Reilly Press,
/[Tt]/ is faster and more space efficient than /t/i - by as much as
double against long strings. //i essentially does a lc() on the
string and keeps a temporary copy in memory.
When this changes, and it is in the 5.6 development series, I will
gladly use //i. Believe me, I realize [Tt] is hideously ugly.
7 Q: How can I pre-load my templates using cache-mode and mod_perl?
A: Add something like this to your startup.pl:
use HTML::Template;
use File::Find;
print STDERR "Pre-loading HTML Templates...\n";
find(
sub {
return unless /\.tmpl$/;
HTML::Template->new(
filename => "$File::Find::dir/$_",
cache => 1,
);
},
'/path/to/templates',
'/another/path/to/templates/'
);
Note that you'll need to modify the "return unless" line to specify
the extension you use for your template files - I use .tmpl, as you
can see. You'll also need to specify the path to your template
files.
One potential problem: the "/path/to/templates/" must be EXACTLY the
same path you use when you call HTML::Template->new(). Otherwise the
cache won't know they're the same file and will load a new copy -
instead getting a speed increase, you'll double your memory usage.
To find out if this is happening set cache_debug => 1 in your
application code and look for "CACHE MISS" messages in the logs.
8 Q: What characters are allowed in TMPL_* NAMEs?
A: Numbers, letters, '.', '/', '+', '-' and '_'.
9 Q: How can I execute a program from inside my template?
A: Short answer: you can't. Longer answer: you shouldn't since this
violates the fundamental concept behind HTML::Template - that design
and code should be seperate.
But, inevitably some people still want to do it. If that describes
you then you should take a look at HTML::Template::Expr. Using
HTML::Template::Expr it should be easy to write a run_program()
function. Then you can do awful stuff like:
<tmpl_var expr="run_program('foo.pl')">
Just, please, don't tell me about it. I'm feeling guilty enough just
for writing HTML::Template::Expr in the first place.
10 Q: Can I get a copy of these docs in Japanese?
A: Yes you can. See Kawai Takanori's translation at:
[URL=http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm]http://member.nifty.ne.jp/hippo2000/perltips/html/template.htm[/URL]
11 Q: What's the best way to create a <select> form element using
HTML::Template?
A: There is much disagreement on this issue. My personal preference
is to use CGI.pm's excellent popup_menu() and scrolling_list()
functions to fill in a single <tmpl_var select_foo> variable.
To some people this smacks of mixing HTML and code in a way that
they hoped HTML::Template would help them avoid. To them I'd say
that HTML is a violation of the principle of separating design from
programming. There's no clear separation between the programmatic
elements of the <form> tags and the layout of the <form> tags.
You'll have to draw the line somewhere - clearly the designer can't
be entirely in charge of form creation.
It's a balancing act and you have to weigh the pros and cons on each
side. It is certainly possible to produce a <select> element
entirely inside the template. What you end up with is a rat's nest
of loops and conditionals. Alternately you can give up a certain
amount of flexibility in return for vastly simplifying your
templates. I generally choose the latter.
Another option is to investigate HTML::FillInForm which some have
reported success using to solve this problem.
6 Einträge, 1 Seite |