1HTML::Template::PerlIntUesrefracCeo(n3t)ributed Perl DocHuTmMeLn:t:aTteimopnlate::PerlInterface(3)
2
3
4

NAME

6       HTML::Template::PerlInterface - perl interface of HTML::Template::Pro
7

SYNOPSIS

9       This help is only on perl interface of HTML::Template::Pro.  For syntax
10       of html template files you should see "SYNOPSIS" in
11       HTML::Template::SYNTAX.
12
13       First you make a template - this is just a normal HTML file with a few
14       extra tags, the simplest being <TMPL_VAR>
15
16       For example, test.tmpl:
17
18         <html>
19         <head><title>Test Template</title>
20         <body>
21         My Home Directory is <TMPL_VAR NAME=HOME>
22         <p>
23         My Path is set to <TMPL_VAR NAME=PATH>
24         </body>
25         </html>
26
27       See HTML::Template::SYNTAX for their syntax.
28
29       Now create a small CGI program:
30
31         #!/usr/bin/perl -w
32         use HTML::Template::Pro;
33
34         # open the html template
35         my $template = HTML::Template::Pro->new(
36               filename => 'test.tmpl',
37               case_sensitive=> 1);
38
39         # fill in some parameters
40         $template->param(HOME => $ENV{HOME});
41         $template->param(PATH => $ENV{PATH});
42
43         # send the obligatory Content-Type and print the template output
44         print "Content-Type: text/html\n\n";
45
46         # print output
47         $template->output(print_to=>\*STDOUT);
48
49         # this would also work.
50         # print $template->output();
51
52         # this would also work. It is faster,
53         # but (WARNING!) not compatible with original HTML::Template.
54         # $template->output();
55
56       If all is well in the universe this should show something like this in
57       your browser when visiting the CGI:
58
59         My Home Directory is /home/some/directory
60         My Path is set to /bin;/usr/bin
61
62       For the best performance it is recommended to use case_sensitive=>1 in
63       new() and print_to=>\*STDOUT in output().
64
65       Note that (HTML::Template::Pro version 0.90+) output(), called in void
66       context, also prints to stdout using built-in htmltmplpro C library
67       calls, so the last call "$template->output();" might be, in fact, the
68       fastest way to call output().
69
70       IMPORTANT NOTE: you can safely write
71
72         my $template = HTML::Template->new( ... options ...)
73               or even
74         my $template = HTML::Template::Expr->new( ... options ...)
75
76       with HTML::Template::Pro, because in absence of original HTML::Template
77       and HTML::Template::Expr HTML::Template::Pro intercepts their calls.
78
79       You can also use all three modules and safely mix their calls
80       (benchmarking may be the only reason for it).  In case you want to mix
81       calls to HTML::Template::Expr and HTML::Template::Pro, the only proper
82       usage of their load is
83
84       use HTML::Template; use HTML::Template::Expr; use HTML::Template::Pro;
85
86       Of course, if you don't plan to mix them (in most cases) it is enough
87       to simply write
88
89       use HTML::Template::Pro;
90
91       Simply use HTML::Template::Pro, it supports all functions of
92       HTML::Template::Expr.
93

DESCRIPTION

95       HTML::Template::Pro is a fast C/perl+XS implementation of
96       HTML::Template and HTML::Template::Expr.  See HTML::Template::Pro for
97       details.
98
99       It fully supports template language of HTML::Template as described in
100       HTML::Template::SYNTAX.
101
102       Briefly,
103
104       "This module attempts to make using HTML templates simple and natural.
105       It extends standard HTML with a few new HTML-esque tags - <TMPL_VAR>,
106       <TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF>, <TMPL_ELSE> and <TMPL_UNLESS>.
107       The file written with HTML and these new tags is called a template.  It
108       is usually saved separate from your script - possibly even created by
109       someone else!  Using this module you fill in the values for the
110       variables, loops and branches declared in the template.  This allows
111       you to separate design - the HTML - from the data, which you generate
112       in the Perl script."
113
114       Here is described a perl interface of HTML::Template::Pro and
115       HTML::Template + HTML::Template::Expr.  See DISTINCTIONS for brief
116       summary of distinctions between HTML::Template::Pro and HTML::Template.
117

METHODS

119   new()
120       Call new() to create a new Template object:
121
122         my $template = HTML::Template->new( filename => 'file.tmpl',
123                                             option => 'value'
124                                           );
125
126       You must call new() with at least one name => value pair specifying how
127       to access the template text.  You can use "filename => 'file.tmpl'" to
128       specify a filename to be opened as the template.  Alternately you can
129       use:
130
131         my $t = HTML::Template->new( scalarref => $ref_to_template_text,
132                                      option => 'value'
133                                    );
134
135       and
136
137         my $t = HTML::Template->new( arrayref => $ref_to_array_of_lines ,
138                                      option => 'value'
139                                    );
140
141       These initialize the template from in-memory resources.  In almost
142       every case you'll want to use the filename parameter.  If you're
143       worried about all the disk access from reading a template file just use
144       mod_perl and the cache option detailed below.
145
146       You can also read the template from an already opened filehandle,
147       either traditionally as a glob or as a FileHandle:
148
149         my $t = HTML::Template->new( filehandle => *FH, option => 'value');
150
151       The four new() calling methods can also be accessed as below, if you
152       prefer.
153
154         my $t = HTML::Template->new_file('file.tmpl', option => 'value');
155
156         my $t = HTML::Template->new_scalar_ref($ref_to_template_text,
157                                               option => 'value');
158
159         my $t = HTML::Template->new_array_ref($ref_to_array_of_lines,
160                                              option => 'value');
161
162         my $t = HTML::Template->new_filehandle($fh,
163                                              option => 'value');
164
165       And as a final option, for those that might prefer it, you can call new
166       as:
167
168         my $t = HTML::Template->new(type => 'filename',
169                                     source => 'file.tmpl');
170
171       Which works for all three of the source types.
172
173       If the environment variable HTML_TEMPLATE_ROOT is set and your filename
174       doesn't begin with /, then the path will be relative to the value of
175       $HTML_TEMPLATE_ROOT.  Example - if the environment variable
176       HTML_TEMPLATE_ROOT is set to "/home/sam" and I call
177       HTML::Template->new() with filename set to "sam.tmpl", the
178       HTML::Template will try to open "/home/sam/sam.tmpl" to access the
179       template file.  You can also affect the search path for files with the
180       "path" option to new() - see below for more information.
181
182       You can modify the Template object's behavior with new().  The options
183       are available:
184
185       Error Detection Options
186           ·   die_on_bad_params - if set to 0 the module will let you call
187               $template->param(param_name => 'value') even if 'param_name'
188               doesn't exist in the template body.  Defaults to 1 in
189               HTML::Template.
190
191               HTML::Template::Pro always use die_on_bad_params => 0.  It
192               currently can't be changed, because HTML::Template::Pro can't
193               know whether a parameter is bad until it finishes output.
194
195               Note that it is wrapper-only option: it is not implemented in
196               the htmltmplpro C library.
197
198           ·   force_untaint - if set to 1 the module will not allow you to
199               set unescaped parameters with tainted values. If set to 2 you
200               will have to untaint all parameters, including ones with the
201               escape attribute.  This option makes sure you untaint
202               everything so you don't accidentally introduce e.g. cross-site-
203               scripting (CSS) vulnerabilities. Requires taint mode. Defaults
204               to 0.
205
206               In the original HTML::Template, if the "force_untaint" option
207               is set an error occurs if you try to set a value that is
208               tainted in the param() call. In HTML::Template::Pro, an error
209               occurs when output is called.
210
211               Note that the tainted value will never be printed; but, to
212               completely suppress output, one should use call to output()
213               that returns string, like print $tmpl->output(); Then output()
214               will die before it returns the string to print.
215
216               Note that it is wrapper-only perl-specific option: it is not
217               implemented in the htmltmplpro C library.
218
219           ·   strict - if set to 0 the module will allow things that look
220               like they might be TMPL_* tags to get by without dieing.
221               Example:
222
223                  <TMPL_HUH NAME=ZUH>
224
225               Would normally cause an error, but if you call new with strict
226               => 0, HTML::Template will ignore it.  Defaults to 1.
227
228               HTML::Template::Pro always implies strict => 0.
229
230       Caching Options
231           HTML::Template use many caching options such as cache,
232           shared_cache, double_cache, blind_cache, file_cache,
233           file_cache_dir, file_cache_dir_mode, double_file_cache to cache
234           preparsed html templates.
235
236           Since HTML::Template::Pro parses and outputs templates at once, it
237           silently ignores those options.
238
239       Filesystem Options
240           ·   path - you can set this variable with a list of paths to search
241               for files specified with the "filename" option to new() and for
242               files included with the <TMPL_INCLUDE> tag.  This list is only
243               consulted when the filename is relative.  The
244               HTML_TEMPLATE_ROOT environment variable is always tried first
245               if it exists.  Also, if HTML_TEMPLATE_ROOT is set then an
246               attempt will be made to prepend HTML_TEMPLATE_ROOT onto paths
247               in the path array.  In the case of a <TMPL_INCLUDE> file, the
248               path to the including file is also tried before path is
249               consulted.
250
251               Example:
252
253                  my $template = HTML::Template->new( filename => 'file.tmpl',
254                                                      path => [ '/path/to/templates',
255                                                                '/alternate/path'
256                                                              ]
257                                                     );
258
259               NOTE: the paths in the path list must be expressed as UNIX
260               paths, separated by the forward-slash character ('/').
261
262           ·   search_path_on_include - if set to a true value the module will
263               search from the top of the array of paths specified by the path
264               option on every <TMPL_INCLUDE> and use the first matching
265               template found.  The normal behavior is to look only in the
266               current directory for a template to include.  Defaults to 0.
267
268       Debugging Options
269           ·   debug - if set to 1 the module will write random debugging
270               information to STDERR.  Defaults to 0.
271
272           ·   HTML::Template use many cache debug options such as
273               stack_debug, cache_debug, shared_cache_debug, memory_debug.
274               Since HTML::Template::Pro parses and outputs templates at once,
275               it silently ignores those options.
276
277       Miscellaneous Options
278           ·   associate - this option allows you to inherit the parameter
279               values from other objects.  The only requirement for the other
280               object is that it have a "param()" method that works like
281               HTML::Template's "param()".  A good candidate would be a CGI.pm
282               query object.  Example:
283
284                 my $query = new CGI;
285                 my $template = HTML::Template->new(filename => 'template.tmpl',
286                                                    associate => $query);
287
288               Now, "$template->output()" will act as though
289
290                 $template->param('FormField', $cgi->param('FormField'));
291
292               had been specified for each key/value pair that would be
293               provided by the "$cgi->param()" method.  Parameters you set
294               directly take precedence over associated parameters.
295
296               You can specify multiple objects to associate by passing an
297               anonymous array to the associate option.  They are searched for
298               parameters in the order they appear:
299
300                 my $template = HTML::Template->new(filename => 'template.tmpl',
301                                                    associate => [$query, $other_obj]);
302
303               NOTE: If the option case_sensitive => 0, the parameter names
304               are matched in a case-insensitive manner.  If you have two
305               parameters in a CGI object like 'NAME' and 'Name' one will be
306               chosen randomly by associate.  This behavior can be changed by
307               setting option case_sensitive to 1.
308
309           ·   case_sensitive - setting this option to true causes
310               HTML::Template to treat template variable names case-
311               sensitively.  The following example would only set one
312               parameter without the "case_sensitive" option:
313
314                 my $template = HTML::Template->new(filename => 'template.tmpl',
315                                                    case_sensitive => 1);
316                 $template->param(
317                   FieldA => 'foo',
318                   fIELDa => 'bar',
319                 );
320
321               This option defaults to off to keep compatibility with
322               HTML::Template.  Nevertheless, setting case_sensitive => 1 is
323               encouraged, because it significantly improves performance.
324
325               If case_sensitive is set to 0, the perl wrapper is forced to
326               lowercase keys in every hash it will find in "param" tree,
327               which is sometimes an expensive operation. To avoid this, set
328               case_sensitive => 1.
329
330               If case conversion is nessessary, there is an alternative
331               lightweight option tmpl_var_case, which is HTML::Template::Pro
332               specific.
333
334               Note that case_sensitive is wrapper-only option: it is not
335               implemented in the htmltmplpro C library.
336
337           ·   tmpl_var_case - this option is similar to case_sensitive, but
338               is implemented directly in the htmltmplpro C library.  Instead
339               of converting keys in every hash of "param" tree, it converts
340               the name of variable.
341
342               For example, in case of <tmpl_var name="CamelCaseName"> setting
343               tmpl_var_case = ASK_NAME_AS_IS | ASK_NAME_LOWERCASE |
344               ASK_NAME_UPPERCASE will cause HTML::Template::Pro to look into
345               "param" tree for 3 names: CamelCaseName, camelcasename, and
346               CAMELCASENAME.
347
348               By default, the name is asked "as is".
349
350           ·   loop_context_vars - when this parameter is set to true (it is
351               false by default) four loop context variables are made
352               available inside a loop: __first__, __last__, __inner__,
353               __odd__.  They can be used with <TMPL_IF>, <TMPL_UNLESS> and
354               <TMPL_ELSE> to control how a loop is output.
355
356               In addition to the above, a __counter__ var is also made
357               available when loop context variables are turned on.
358
359               Example:
360
361                  <TMPL_LOOP NAME="FOO">
362                     <TMPL_IF NAME="__first__">
363                       This only outputs on the first pass.
364                     </TMPL_IF>
365
366                     <TMPL_IF NAME="__odd__">
367                       This outputs every other pass, on the odd passes.
368                     </TMPL_IF>
369
370                     <TMPL_UNLESS NAME="__odd__">
371                       This outputs every other pass, on the even passes.
372                     </TMPL_UNLESS>
373
374                     <TMPL_IF NAME="__inner__">
375                       This outputs on passes that are neither first nor last.
376                     </TMPL_IF>
377
378                     This is pass number <TMPL_VAR NAME="__counter__">.
379
380                     <TMPL_IF NAME="__last__">
381                       This only outputs on the last pass.
382                     </TMPL_IF>
383                  </TMPL_LOOP>
384
385               One use of this feature is to provide a "separator" similar in
386               effect to the perl function join().  Example:
387
388                  <TMPL_LOOP FRUIT>
389                     <TMPL_IF __last__> and </TMPL_IF>
390                     <TMPL_VAR KIND><TMPL_UNLESS __last__>, <TMPL_ELSE>.</TMPL_UNLESS>
391                  </TMPL_LOOP>
392
393               Would output (in a browser) something like:
394
395                 Apples, Oranges, Brains, Toes, and Kiwi.
396
397               Given an appropriate "param()" call, of course.  NOTE: A loop
398               with only a single pass will get both __first__ and __last__
399               set to true, but not __inner__.
400
401               NOTE: in the original HTML::Template with case_sensitive = 1
402               and loop_context_vars the special loop variables are available
403               in lower-case only.  In HTML::Template::Pro they are recognized
404               regardless of case.
405
406           ·   no_includes - set this option to 1 to disallow the
407               <TMPL_INCLUDE> tag in the template file.  This can be used to
408               make opening untrusted templates slightly less dangerous.
409               Defaults to 0.
410
411           ·   max_includes - set this variable to determine the maximum depth
412               that includes can reach.  Set to 10 by default.  Including
413               files to a depth greater than this value causes an error
414               message to be displayed.  Set to 0 to disable this protection.
415
416           ·   global_vars - normally variables declared outside a loop are
417               not available inside a loop.  This option makes <TMPL_VAR>s
418               like global variables in Perl - they have unlimited scope.
419               This option also affects <TMPL_IF> and <TMPL_UNLESS>.
420
421               Example:
422
423                 This is a normal variable: <TMPL_VAR NORMAL>.<P>
424
425                 <TMPL_LOOP NAME=FROOT_LOOP>
426                    Here it is inside the loop: <TMPL_VAR NORMAL><P>
427                 </TMPL_LOOP>
428
429               Normally this wouldn't work as expected, since <TMPL_VAR
430               NORMAL>'s value outside the loop is not available inside the
431               loop.
432
433               The global_vars option also allows you to access the values of
434               an enclosing loop within an inner loop.  For example, in this
435               loop the inner loop will have access to the value of OUTER_VAR
436               in the correct iteration:
437
438                  <TMPL_LOOP OUTER_LOOP>
439                     OUTER: <TMPL_VAR OUTER_VAR>
440                       <TMPL_LOOP INNER_LOOP>
441                          INNER: <TMPL_VAR INNER_VAR>
442                          INSIDE OUT: <TMPL_VAR OUTER_VAR>
443                       </TMPL_LOOP>
444                  </TMPL_LOOP>
445
446               NOTE: "global_vars" is not "global_loops" (which does not
447               exist).  That means that loops you declare at one scope are not
448               available inside other loops even when "global_vars" is on.
449
450           ·   path_like_variable_scope - this option switches on a Shigeki
451               Morimoto extension to HTML::Template::Pro that allows access to
452               variables that are outside the current loop scope using path-
453               like expressions.
454
455               Example: {{{ <TMPL_LOOP NAME=class>
456                 <TMPL_LOOP NAME=person>
457                   <TMPL_VAR NAME="../teacher_name">  <!-- access to
458               class.teacher_name -->
459                   <TMPL_VAR NAME="name">
460                   <TMPL_VAR NAME="/top_level_value"> <!-- access to top level
461               value -->
462                   <TMPL_VAR NAME="age">
463                     <TMPL_LOOP NAME="../../school">  <!-- enter loop before
464               accessing its vars -->
465                       <TMPL_VAR NAME="school_name">  <!-- access to
466               [../../]school.school_name -->
467                     </TMPL_LOOP>
468                 </TMPL_LOOP> </TMPL_LOOP> }}}
469
470           ·   filter - this option allows you to specify a filter for your
471               template files.  A filter is a subroutine that will be called
472               after HTML::Template reads your template file but before it
473               starts parsing template tags.
474
475               In the most simple usage, you simply assign a code reference to
476               the filter parameter.  This subroutine will recieve a single
477               argument - a reference to a string containing the template file
478               text.  Here is an example that accepts templates with tags that
479               look like "!!!ZAP_VAR FOO!!!" and transforms them into
480               HTML::Template tags:
481
482                  my $filter = sub {
483                    my $text_ref = shift;
484                    $$text_ref =~ s/!!!ZAP_(.*?)!!!/<TMPL_$1>/g;
485                  };
486
487                  # open zap.tmpl using the above filter
488                  my $template = HTML::Template->new(filename => 'zap.tmpl',
489                                                     filter => $filter);
490
491               More complicated usages are possible.  You can request that
492               your filter receive the template text as an array of lines
493               rather than as a single scalar.  To do that you need to specify
494               your filter using a hash-ref.  In this form you specify the
495               filter using the "sub" key and the desired argument format
496               using the "format" key.  The available formats are "scalar" and
497               "array".  Using the "array" format will incur a performance
498               penalty but may be more convenient in some situations.
499
500                  my $template = HTML::Template->new(filename => 'zap.tmpl',
501                                                     filter => { sub => $filter,
502                                                                 format => 'array' });
503
504               You may also have multiple filters.  This allows simple filters
505               to be combined for more elaborate functionality.  To do this
506               you specify an array of filters.  The filters are applied in
507               the order they are specified.
508
509                  my $template = HTML::Template->new(filename => 'zap.tmpl',
510                                                     filter => [
511                                                          { sub => \&decompress,
512                                                            format => 'scalar' },
513                                                          { sub => \&remove_spaces,
514                                                            format => 'array' }
515                                                       ]);
516
517               The specified filters will be called for any TMPL_INCLUDEed
518               files just as they are for the main template file.
519
520       *   default_escape - Set this parameter to "HTML", "URL" or "JS" and
521           HTML::Template will apply the specified escaping to all variables
522           unless they declare a different escape in the template.
523
524   param()
525       "param()" can be called in a number of ways
526
527       1) To return a list of parameters in the template :
528          ( this features is distinct in HTML::Template::Pro:
529          it returns a list of parameters _SET_ after new() )
530
531          my @parameter_names = $self->param();
532
533       2) To return the value set to a param :
534
535          my $value = $self->param('PARAM');
536
537       3) To set the value of a parameter :
538
539             # For simple TMPL_VARs:
540             $self->param(PARAM => 'value');
541
542             # with a subroutine reference that gets called to get the value
543             # of the scalar.  The sub will recieve the template object as a
544             # parameter.
545             $self->param(PARAM => sub { return 'value' });
546
547             # And TMPL_LOOPs:
548             $self->param(LOOP_PARAM =>
549                          [
550                           { PARAM => VALUE_FOR_FIRST_PASS, ... },
551                           { PARAM => VALUE_FOR_SECOND_PASS, ... }
552                           ...
553                          ]
554                         );
555
556       4) To set the value of a a number of parameters :
557
558            # For simple TMPL_VARs:
559            $self->param(PARAM => 'value',
560                         PARAM2 => 'value'
561                        );
562
563             # And with some TMPL_LOOPs:
564             $self->param(PARAM => 'value',
565                          PARAM2 => 'value',
566                          LOOP_PARAM =>
567                          [
568                           { PARAM => VALUE_FOR_FIRST_PASS, ... },
569                           { PARAM => VALUE_FOR_SECOND_PASS, ... }
570                           ...
571                          ],
572                          ANOTHER_LOOP_PARAM =>
573                          [
574                           { PARAM => VALUE_FOR_FIRST_PASS, ... },
575                           { PARAM => VALUE_FOR_SECOND_PASS, ... }
576                           ...
577                          ]
578                         );
579
580       5) To set the value of a a number of parameters using a hash-ref :
581
582             $self->param(
583                          {
584                             PARAM => 'value',
585                             PARAM2 => 'value',
586                             LOOP_PARAM =>
587                             [
588                               { PARAM => VALUE_FOR_FIRST_PASS, ... },
589                               { PARAM => VALUE_FOR_SECOND_PASS, ... }
590                               ...
591                             ],
592                             ANOTHER_LOOP_PARAM =>
593                             [
594                               { PARAM => VALUE_FOR_FIRST_PASS, ... },
595                               { PARAM => VALUE_FOR_SECOND_PASS, ... }
596                               ...
597                             ]
598                           }
599                          );
600
601   clear_params()
602       Sets all the parameters to undef.  Useful internally, if nowhere else!
603
604   output()
605       output() returns the final result of the template.  In most situations
606       you'll want to print this, like:
607
608          print $template->output();
609
610       When output is called each occurrence of <TMPL_VAR NAME=name> is
611       replaced with the value assigned to "name" via "param()".  If a named
612       parameter is unset it is simply replaced with ''.  <TMPL_LOOPS> are
613       evaluated once per parameter set, accumulating output on each pass.
614
615       Calling output() is guaranteed not to change the state of the Template
616       object, in case you were wondering.  This property is mostly important
617       for the internal implementation of loops.
618
619       You may optionally supply a filehandle to print to automatically as the
620       template is generated.  This may improve performance and lower memory
621       consumption.  Example:
622
623          $template->output(print_to => *STDOUT);
624
625       The return value is undefined when using the "print_to" option.
626
627   query()
628       This method is not supported in HTML::Template::Pro.
629

DISTINCTIONS AND INCOMPATIBILITIES

631       The main reason for small incompatibilities between HTML::Template and
632       HTML::Template::Pro is the fact that HTML::Template builds parsed tree
633       of template before anything else. So it has an additional information
634       which HTML::Template::Pro obtains during output.
635
636       In cases when HTML::Template dies, such as no_includes, bad syntax of
637       template, max_includes and so on, HTML::Template::Pro issues warning to
638       STDERR and continue.
639
640   new()
641       the following options are not supported in HTML::Template::Pro:
642
643        vanguard_compatibility_mode.
644
645       The options die_on_bad_params and strict are ignored.
646       HTML::Template::Pro behaves itself as HTML::Template called with
647        die_on_bad_params => 0, strict => 0.
648
649       It currently can't be changed, because HTML::Template::Pro can't know
650       whether a parameter is bad before it start output.  This may change in
651       future releases.
652
653       To keep backward compatibility with HTML::Template, you should
654       explicitly call its new() with die_on_bad_params => 0, strict => 0.
655
656   query()
657       This method is not supported in HTML::Template::Pro.
658
659   param()
660       param() without arguments should return a list of parameters in the
661       template.  In HTML::Template::Pro it returns a list of parameters set
662       after new().
663

BUGS

665       With case_sensitive and loop_context_vars the special loop variables
666       should be available in lower-case only.
667
668       associate is case_sensitive inside loops.
669
670       When submitting bug reports, be sure to include full details, including
671       the VERSION of the module, a test script and a test template
672       demonstrating the problem!
673

EXPR: DEFINING NEW FUNCTIONS

675       To define a new function, pass a "functions" option to new:
676
677         $t = HTML::Template::Pro->new(filename => 'foo.tmpl',
678                                        functions =>
679                                          { func_name => \&func_handler });
680       or
681
682         $t = HTML::Template::Expr->new(filename => 'foo.tmpl',
683                                        functions =>
684                                          { func_name => \&func_handler });
685
686       Or, you can use "register_function" class method to register the
687       function globally:
688
689         HTML::Template::Pro->register_function(func_name => \&func_handler);
690       or
691         HTML::Template::Expr->register_function(func_name => \&func_handler);
692
693       You provide a subroutine reference that will be called during output.
694       It will recieve as arguments the parameters specified in the template.
695       For example, here's a function that checks if a directory exists:
696
697         sub directory_exists {
698           my $dir_name = shift;
699           return 1 if -d $dir_name;
700           return 0;
701         }
702
703       If you call HTML::Template::Expr->new() with a "functions" arg:
704
705         $t = HTML::Template::Expr->new(filename => 'foo.tmpl',
706                                        functions => {
707                                           directory_exists => \&directory_exists
708                                        });
709
710       Then you can use it in your template:
711
712         <tmpl_if expr="directory_exists('/home/sam')">
713
714       This can be abused in ways that make my teeth hurt.
715
716   register_function() extended usage (HTML::Template::Pro specific)
717       "register_function()" can be called in a number of ways
718
719       1) To fetch the names of registered functions in the template:
720
721       ·   if "register_function()" was called in a newly created object it
722           returns a
723
724              list of function's that set _after_ or _in_ new():
725
726              my @registered_functions_names = $self->register_function();
727
728       ·   in global context "register_function()" will return a list of _ALL_
729              avalible function's
730
731              my @all_avalible_functions_names =
732           HTML::Template::Pro->register_function();
733
734           2) To fetching the function by name:
735
736              my $function = $self->register_function('FUNCTION_NAME');
737
738           3) To set a new function:
739
740               # Set function, that can be called in templates, wich are processed
741               # by the current object:
742               $self->register_function(foozicate => sub { ... });
743
744               # Set global function:
745               HTML::Template::Pro->register_function(barify    => sub { ... });
746
747       for details of "how to defined a function" see in "EXPR: DEFINING NEW
748       FUNCTIONS".
749

EXPR MOD_PERL TIP

751       "register_function" class method can be called in mod_perl's startup.pl
752       to define widely used common functions to HTML::Template::Expr. Add
753       something like this to your startup.pl:
754
755         use HTML::Template::Pro;
756
757         HTML::Template::Pro->register_function(foozicate => sub { ... });
758         HTML::Template::Pro->register_function(barify    => sub { ... });
759         HTML::Template::Pro->register_function(baznate   => sub { ... });
760

EXPR CAVEATS

762       HTML::Template::Pro does not forces the HTML::Template global_vars
763       option to be set, whereas currently HTML::Template::Expr does.  Anyway,
764       this also will hopefully go away in a future version of
765       HTML::Template::Expr, so if you need global_vars in your templates then
766       you should set it explicitely.
767

CREDITS

769       to Sam Tregar, sam@tregar.com
770
771       Original credits of HTML::Template:
772
773       This module was the brain child of my boss, Jesse Erlbaum (
774       jesse@vm.com ) at Vanguard Media ( http://vm.com ) .  The most original
775       idea in this module - the <TMPL_LOOP> - was entirely his.
776
777       Fixes, Bug Reports, Optimizations and Ideas have been generously
778       provided by:
779
780          Richard Chen
781          Mike Blazer
782          Adriano Nagelschmidt Rodrigues
783          Andrej Mikus
784          Ilya Obshadko
785          Kevin Puetz
786          Steve Reppucci
787          Richard Dice
788          Tom Hukins
789          Eric Zylberstejn
790          David Glasser
791          Peter Marelas
792          James William Carlson
793          Frank D. Cringle
794          Winfried Koenig
795          Matthew Wickline
796          Doug Steinwand
797          Drew Taylor
798          Tobias Brox
799          Michael Lloyd
800          Simran Gambhir
801          Chris Houser <chouser@bluweb.com>
802          Larry Moore
803          Todd Larason
804          Jody Biggs
805          T.J. Mather
806          Martin Schroth
807          Dave Wolfe
808          uchum
809          Kawai Takanori
810          Peter Guelich
811          Chris Nokleberg
812          Ralph Corderoy
813          William Ward
814          Ade Olonoh
815          Mark Stosberg
816          Lance Thomas
817          Roland Giersig
818          Jere Julian
819          Peter Leonard
820          Kenny Smith
821          Sean P. Scanlon
822          Martin Pfeffer
823          David Ferrance
824          Gyepi Sam
825          Darren Chamberlain
826          Paul Baker
827          Gabor Szabo
828          Craig Manley
829          Richard Fein
830          The Phalanx Project
831          Sven Neuhaus
832
833       Thanks!
834
835       Original credits of HTML::Template::Expr:
836
837       The following people have generously submitted bug reports, patches and
838       ideas:
839
840          Peter Leonard
841          Tatsuhiko Miyagawa
842
843       Thanks!
844

WEBSITE

846       You can find information about HTML::Template::Pro at:
847
848          http://html-tmpl-pro.sourceforge.net
849
850       You can find information about HTML::Template and other related modules
851       at:
852
853          http://html-template.sourceforge.net
854

AUTHOR

856       Sam Tregar, sam@tregar.com (Main text)
857
858       I. Vlasenko, <viy@altlinux.org> (Pecularities of HTML::Template::Pro)
859

LICENSE

861         HTML::Template : A module for using HTML Templates with Perl
862         Copyright (C) 2000-2002 Sam Tregar (sam@tregar.com)
863
864         This module is free software; you can redistribute it and/or modify it
865         under the terms of either:
866
867         a) the GNU General Public License as published by the Free Software
868         Foundation; either version 1, or (at your option) any later version,
869
870         or
871
872         b) the "Artistic License" which comes with this module.
873
874         This program is distributed in the hope that it will be useful,
875         but WITHOUT ANY WARRANTY; without even the implied warranty of
876         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
877         the GNU General Public License or the Artistic License for more details.
878
879         You should have received a copy of the Artistic License with this
880         module, in the file ARTISTIC.  If not, I'll be glad to provide one.
881
882         You should have received a copy of the GNU General Public License
883         along with this program; if not, write to the Free Software
884         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
885         USA
886
887
888
889perl v5.12.1                      2009-09-03  HTML::Template::PerlInterface(3)
Impressum