|
NAMEHTML::Template::PerlInterface - perl interface of HTML::Template::ProSYNOPSISThis help is only on perl interface of HTML::Template::Pro. For syntax of html template files you should see "SYNOPSIS" in HTML::Template::SYNTAX.First you make a template - this is just a normal HTML file with a few extra tags, the simplest being <TMPL_VAR> For example, test.tmpl: <html> <head><title>Test Template</title> <body> My Home Directory is <TMPL_VAR NAME=HOME> <p> My Path is set to <TMPL_VAR NAME=PATH> </body> </html> See HTML::Template::SYNTAX for their syntax. Now create a small CGI program: #!/usr/bin/perl -w use HTML::Template::Pro; # open the html template my $template = HTML::Template::Pro->new( filename => 'test.tmpl', case_sensitive=> 1); # fill in some parameters $template->param(HOME => $ENV{HOME}); $template->param(PATH => $ENV{PATH}); # send the obligatory Content-Type and print the template output print "Content-Type: text/html\n\n"; # print output $template->output(print_to=>\*STDOUT); # this would also work. # print $template->output(); # this would also work. It is faster, # but (WARNING!) not compatible with original HTML::Template. # $template->output(); If all is well in the universe this should show something like this in your browser when visiting the CGI: My Home Directory is /home/some/directory My Path is set to /bin;/usr/bin For the best performance it is recommended to use case_sensitive=>1 in new() and print_to=>\*STDOUT in output(). Note that (HTML::Template::Pro version 0.90+) output(), called in void context, also prints to stdout using built-in htmltmplpro C library calls, so the last call "$template->output();" might be, in fact, the fastest way to call output(). IMPORTANT NOTE: you can safely write my $template = HTML::Template->new( ... options ...) or even my $template = HTML::Template::Expr->new( ... options ...) with HTML::Template::Pro, because in absence of original HTML::Template and HTML::Template::Expr HTML::Template::Pro intercepts their calls. You can also use all three modules and safely mix their calls (benchmarking may be the only reason for it). In case you want to mix calls to HTML::Template::Expr and HTML::Template::Pro, the only proper usage of their load is use HTML::Template; use HTML::Template::Expr; use HTML::Template::Pro; Of course, if you don't plan to mix them (in most cases) it is enough to simply write use HTML::Template::Pro; Simply use HTML::Template::Pro, it supports all functions of HTML::Template::Expr. DESCRIPTIONHTML::Template::Pro is a fast C/perl+XS implementation of HTML::Template and HTML::Template::Expr. See HTML::Template::Pro for details.It fully supports template language of HTML::Template as described in HTML::Template::SYNTAX. Briefly, "This module attempts to make using HTML templates simple and natural. It extends standard HTML with a few new HTML-esque tags - <TMPL_VAR>, <TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF>, <TMPL_ELSE> and <TMPL_UNLESS>. The file written with HTML and these new tags is called a template. It is usually saved separate from your script - possibly even created by someone else! Using this module you fill in the values for the variables, loops and branches declared in the template. This allows you to separate design - the HTML - from the data, which you generate in the Perl script." Here is described a perl interface of HTML::Template::Pro and HTML::Template + HTML::Template::Expr. See DISTINCTIONS for brief summary of distinctions between HTML::Template::Pro and HTML::Template. METHODSnew()Call new() to create a new Template object:my $template = HTML::Template->new( filename => 'file.tmpl', option => 'value' ); You must call new() with at least one name => value pair specifying how to access the template text. You can use "filename => 'file.tmpl'" to specify a filename to be opened as the template. Alternately you can use: my $t = HTML::Template->new( scalarref => $ref_to_template_text, option => 'value' ); and my $t = HTML::Template->new( arrayref => $ref_to_array_of_lines , option => 'value' ); These initialize the template from in-memory resources. In almost every case you'll want to use the filename parameter. If you're worried about all the disk access from reading a template file just use mod_perl and the cache option detailed below. You can also read the template from an already opened filehandle, either traditionally as a glob or as a FileHandle: my $t = HTML::Template->new( filehandle => *FH, option => 'value'); The four new() calling methods can also be accessed as below, if you prefer. my $t = HTML::Template->new_file('file.tmpl', option => 'value'); my $t = HTML::Template->new_scalar_ref($ref_to_template_text, option => 'value'); my $t = HTML::Template->new_array_ref($ref_to_array_of_lines, option => 'value'); my $t = HTML::Template->new_filehandle($fh, option => 'value'); And as a final option, for those that might prefer it, you can call new as: my $t = HTML::Template->new(type => 'filename', source => 'file.tmpl'); Which works for all three of the source types. If the environment variable HTML_TEMPLATE_ROOT is set and your filename doesn't begin with /, then the path will be relative to the value of $HTML_TEMPLATE_ROOT. Example - if the environment variable HTML_TEMPLATE_ROOT is set to "/home/sam" and I call HTML::Template->new() with filename set to "sam.tmpl", the HTML::Template will try to open "/home/sam/sam.tmpl" to access the template file. You can also affect the search path for files with the "path" option to new() - see below for more information. You can modify the Template object's behavior with new(). The options are available:
param()"param()" can be called in a number of ways1) To return a list of parameters in the template : ( this features is distinct in HTML::Template::Pro: it returns a list of parameters _SET_ after new() ) 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 receive 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, accumulating 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 is not supported in HTML::Template::Pro.DISTINCTIONS AND INCOMPATIBILITIESThe main reason for small incompatibilities between HTML::Template and HTML::Template::Pro is the fact that HTML::Template builds parsed tree of template before anything else. So it has an additional information which HTML::Template::Pro obtains during output.In cases when HTML::Template dies, such as no_includes, bad syntax of template, max_includes and so on, HTML::Template::Pro issues warning to STDERR and continue. new()the following options are not supported in HTML::Template::Pro:vanguard_compatibility_mode. The options die_on_bad_params and strict are ignored. HTML::Template::Pro behaves itself as HTML::Template called with die_on_bad_params => 0, strict => 0. It currently can't be changed, because HTML::Template::Pro can't know whether a parameter is bad before it start output. This may change in future releases. To keep backward compatibility with HTML::Template, you should explicitly call its new() with die_on_bad_params => 0, strict => 0. query()This method is not supported in HTML::Template::Pro.param()param() without arguments should return a list of parameters in the template. In HTML::Template::Pro it returns a list of parameters set after new().BUGSWith case_sensitive and loop_context_vars the special loop variables should be available in lower-case only.associate is case_sensitive inside loops. When submitting bug reports, be sure to include full details, including the VERSION of the module, a test script and a test template demonstrating the problem! EXPR: DEFINING NEW FUNCTIONSTo define a new function, pass a "functions" option to new:$t = HTML::Template::Pro->new(filename => 'foo.tmpl', functions => { func_name => \&func_handler }); or $t = HTML::Template::Expr->new(filename => 'foo.tmpl', functions => { func_name => \&func_handler }); Or, you can use "register_function" class method to register the function globally: HTML::Template::Pro->register_function(func_name => \&func_handler); or HTML::Template::Expr->register_function(func_name => \&func_handler); You provide a subroutine reference that will be called during output. It will receive as arguments the parameters specified in the template. For example, here's a function that checks if a directory exists: sub directory_exists { my $dir_name = shift; return 1 if -d $dir_name; return 0; } If you call HTML::Template::Expr->new() with a "functions" arg: $t = HTML::Template::Expr->new(filename => 'foo.tmpl', functions => { directory_exists => \&directory_exists }); Then you can use it in your template: <tmpl_if expr="directory_exists('/home/sam')"> This can be abused in ways that make my teeth hurt. register_function() extended usage (HTML::Template::Pro specific)"register_function()" can be called in a number of ways1) To fetch the names of registered functions in the template:
for details of "how to defined a function" see in "EXPR: DEFINING NEW FUNCTIONS". EXPR MOD_PERL TIP"register_function" class method can be called in mod_perl's startup.pl to define widely used common functions to HTML::Template::Expr. Add something like this to your startup.pl:use HTML::Template::Pro; HTML::Template::Pro->register_function(foozicate => sub { ... }); HTML::Template::Pro->register_function(barify => sub { ... }); HTML::Template::Pro->register_function(baznate => sub { ... }); EXPR CAVEATSHTML::Template::Pro does not forces the HTML::Template global_vars option to be set, whereas currently HTML::Template::Expr does. Anyway, this also will hopefully go away in a future version of HTML::Template::Expr, so if you need global_vars in your templates then you should set it explicitly.CREDITSto Sam Tregar, sam@tregar.comOriginal credits of HTML::Template: This module was the brain child of my boss, Jesse Erlbaum ( jesse@vm.com ) at Vanguard Media ( http://vm.com ) . The most original idea in this module - the <TMPL_LOOP> - was entirely his. Fixes, Bug Reports, Optimizations and Ideas have been generously provided by: Richard Chen Mike Blazer Adriano Nagelschmidt Rodrigues Andrej Mikus Ilya Obshadko Kevin Puetz Steve Reppucci Richard Dice Tom Hukins Eric Zylberstejn David Glasser Peter Marelas James William Carlson Frank D. Cringle Winfried Koenig Matthew Wickline Doug Steinwand Drew Taylor Tobias Brox Michael Lloyd Simran Gambhir Chris Houser <chouser@bluweb.com> Larry Moore Todd Larason Jody Biggs T.J. Mather Martin Schroth Dave Wolfe uchum Kawai Takanori Peter Guelich Chris Nokleberg Ralph Corderoy William Ward Ade Olonoh Mark Stosberg Lance Thomas Roland Giersig Jere Julian Peter Leonard Kenny Smith Sean P. Scanlon Martin Pfeffer David Ferrance Gyepi Sam Darren Chamberlain Paul Baker Gabor Szabo Craig Manley Richard Fein The Phalanx Project Sven Neuhaus Thanks! Original credits of HTML::Template::Expr: The following people have generously submitted bug reports, patches and ideas: Peter Leonard Tatsuhiko Miyagawa Thanks! WEBSITEYou can find information about HTML::Template::Pro at:http://html-tmpl-pro.sourceforge.net You can find information about HTML::Template and other related modules at: http://html-template.sourceforge.net AUTHORSam Tregar, sam@tregar.com (Main text)I. Vlasenko, <viy@altlinux.org> (Pecularities of HTML::Template::Pro) LICENSEHTML::Template : A module for using HTML Templates with Perl Copyright (C) 2000-2002 Sam Tregar (sam@tregar.com) This module is free software; you can redistribute it and/or modify it under the terms of either: a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" which comes with this module. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details. You should have received a copy of the Artistic License with this module, in the file ARTISTIC. If not, I'll be glad to provide one. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Visit the GSP FreeBSD Man Page Interface. |