Server IP : 103.119.228.120 / Your IP : 3.16.82.208 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "HTML::Template 3" .TH HTML::Template 3 "2013-10-22" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" HTML::Template \- Perl module to use HTML\-like templating language .SH "SYNOPSIS" .IX Header "SYNOPSIS" First you make a template \- this is just a normal \s-1HTML\s0 file with a few extra tags, the simplest being \f(CW\*(C`<TMPL_VAR>\*(C'\fR .PP For example, test.tmpl: .PP .Vb 8 \& <html> \& <head><title>Test Template</title></head> \& <body> \& My Home Directory is <TMPL_VAR NAME=HOME> \& <p> \& My Path is set to <TMPL_VAR NAME=PATH> \& </body> \& </html> .Ve .PP Now you can use it in a small \s-1CGI\s0 program: .PP .Vb 2 \& #!/usr/bin/perl \-w \& use HTML::Template; \& \& # open the html template \& my $template = HTML::Template\->new(filename => \*(Aqtest.tmpl\*(Aq); \& \& # 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\en\en", $template\->output; .Ve .PP If all is well in the universe this should show something like this in your browser when visiting the \s-1CGI:\s0 .PP .Vb 2 \& My Home Directory is /home/some/directory \& My Path is set to /bin;/usr/bin .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module attempts to make using \s-1HTML\s0 templates simple and natural. It extends standard \s-1HTML\s0 with a few new HTML-esque tags \- \f(CW\*(C`<TMPL_VAR>\*(C'\fR \&\f(CW\*(C`<TMPL_LOOP>\*(C'\fR, \f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR, \f(CW\*(C`<TMPL_IF>\*(C'\fR, \f(CW\*(C`<TMPL_ELSE>\*(C'\fR and \f(CW\*(C`<TMPL_UNLESS>\*(C'\fR. The file written with \s-1HTML\s0 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 \s-1HTML \-\s0 from the data, which you generate in the Perl script. .PP This module is licensed under the same terms as Perl. See the \s-1LICENSE\s0 section below for more details. .SH "TUTORIAL" .IX Header "TUTORIAL" If you're new to HTML::Template, I suggest you start with the introductory article available on Perl Monks: .PP .Vb 1 \& http://www.perlmonks.org/?node_id=65642 .Ve .SH "FAQ" .IX Header "FAQ" Please see HTML::Template::FAQ .SH "MOTIVATION" .IX Header "MOTIVATION" It is true that there are a number of packages out there to do \s-1HTML\s0 templates. On the one hand you have things like HTML::Embperl which allows you freely mix Perl with \s-1HTML. \s0 On the other hand lie home-grown variable substitution solutions. Hopefully the module can find a place between the two. .PP One advantage of this module over a full HTML::Embperl\-esque solution is that it enforces an important divide \- design and programming. By limiting the programmer to just using simple variables and loops in the \s-1HTML,\s0 the template remains accessible to designers and other non-perl people. The use of HTML-esque syntax goes further to make the format understandable to others. In the future this similarity could be used to extend existing \s-1HTML\s0 editors/analyzers to support HTML::Template. .PP An advantage of this module over home-grown tag-replacement schemes is the support for loops. In my work I am often called on to produce tables of data in html. Producing them using simplistic \s-1HTML\s0 templates results in programs containing lots of \s-1HTML\s0 since the \s-1HTML\s0 itself cannot represent loops. The introduction of loop statements in the \s-1HTML\s0 simplifies this situation considerably. The designer can layout a single row and the programmer can fill it in as many times as necessary \- all they must agree on is the parameter names. .PP For all that, I think the best thing about this module is that it does just one thing and it does it quickly and carefully. It doesn't try to replace Perl and \s-1HTML,\s0 it just augments them to interact a little better. And it's pretty fast. .SH "THE TAGS" .IX Header "THE TAGS" .SS "\s-1TMPL_VAR\s0" .IX Subsection "TMPL_VAR" .Vb 1 \& <TMPL_VAR NAME="PARAMETER_NAME"> .Ve .PP The \f(CW\*(C`<TMPL_VAR>\*(C'\fR tag is very simple. For each \f(CW\*(C`<TMPL_VAR>\*(C'\fR tag in the template you call: .PP .Vb 1 \& $template\->param(PARAMETER_NAME => "VALUE") .Ve .PP When the template is output the \f(CW\*(C`<TMPL_VAR>\*(C'\fR is replaced with the \&\s-1VALUE\s0 text you specified. If you don't set a parameter it just gets skipped in the output. .PP You can also specify the value of the parameter as a code reference in order to have \*(L"lazy\*(R" variables. These sub routines will only be referenced if the variables are used. See \*(L"\s-1LAZY VALUES\*(R"\s0 for more information. .PP \fIAttributes\fR .IX Subsection "Attributes" .PP The following \*(L"attributes\*(R" can also be specified in template var tags: .IP "\(bu" 4 escape .Sp This allows you to escape the value before it's put into the output. Th .Sp This is useful when you want to use a \s-1TMPL_VAR\s0 in a context where those characters would cause trouble. For example: .Sp .Vb 1 \& <input name=param type=text value="<TMPL_VAR PARAM>"> .Ve .Sp If you called \f(CW\*(C`param()\*(C'\fR with a value like \f(CW\*(C`sam"my\*(C'\fR you'll get in trouble with \s-1HTML\s0's idea of a double-quote. On the other hand, if you use \&\f(CW\*(C`escape=html\*(C'\fR, like this: .Sp .Vb 1 \& <input name=param type=text value="<TMPL_VAR PARAM ESCAPE=HTML>"> .Ve .Sp You'll get what you wanted no matter what value happens to be passed in for param. .Sp The following escape values are supported: .RS 4 .IP "\(bu" 4 html .Sp Replaces the following characters with their \s-1HTML\s0 entity equivalent: \&\f(CW\*(C`&\*(C'\fR, \f(CW\*(C`"\*(C'\fR, \f(CW\*(C`\*(Aq\*(C'\fR, \f(CW\*(C`<\*(C'\fR, \f(CW\*(C`>\*(C'\fR .IP "\(bu" 4 js .Sp Escapes (with a backslash) the following characters: \f(CW\*(C`\e\*(C'\fR, \f(CW\*(C`\*(Aq\*(C'\fR, \f(CW\*(C`"\*(C'\fR, \&\f(CW\*(C`\en\*(C'\fR, \f(CW\*(C`\er\*(C'\fR .IP "\(bu" 4 url .Sp \&\s-1URL\s0 escapes any \s-1ASCII\s0 characters except for letters, numbers, \f(CW\*(C`_\*(C'\fR, \f(CW\*(C`.\*(C'\fR and \f(CW\*(C`\-\*(C'\fR. .IP "\(bu" 4 none .Sp Performs no escaping. This is the default, but it's useful to be able to explicitly turn off escaping if you are using the \f(CW\*(C`default_escape\*(C'\fR option. .RE .RS 4 .RE .IP "\(bu" 4 default .Sp With this attribute you can assign a default value to a variable. For example, this will output \*(L"the devil gave me a taco\*(R" if the \f(CW\*(C`who\*(C'\fR variable is not set. .Sp .Vb 1 \& <TMPL_VAR WHO DEFAULT="the devil"> gave me a taco. .Ve .SS "\s-1TMPL_LOOP\s0" .IX Subsection "TMPL_LOOP" .Vb 1 \& <TMPL_LOOP NAME="LOOP_NAME"> ... </TMPL_LOOP> .Ve .PP The \f(CW\*(C`<TMPL_LOOP>\*(C'\fR tag is a bit more complicated than \f(CW\*(C`<TMPL_VAR>\*(C'\fR. The \f(CW\*(C`<TMPL_LOOP>\*(C'\fR tag allows you to delimit a section of text and give it a name. Inside this named loop you place \f(CW\*(C`<TMPL_VAR>\*(C'\fRs. Now you pass to \f(CW\*(C`param()\*(C'\fR a list (an array ref) of parameter assignments (hash refs) for this loop. The loop iterates over the list and produces output from the text block for each pass. Unset parameters are skipped. Here's an example: .PP In the template: .PP .Vb 4 \& <TMPL_LOOP NAME=EMPLOYEE_INFO> \& Name: <TMPL_VAR NAME=NAME> <br> \& Job: <TMPL_VAR NAME=JOB> <p> \& </TMPL_LOOP> .Ve .PP In your Perl code: .PP .Vb 4 \& $template\->param( \& EMPLOYEE_INFO => [{name => \*(AqSam\*(Aq, job => \*(Aqprogrammer\*(Aq}, {name => \*(AqSteve\*(Aq, job => \*(Aqsoda jerk\*(Aq}] \& ); \& print $template\->output(); .Ve .PP The output is: .PP .Vb 2 \& Name: Sam \& Job: programmer \& \& Name: Steve \& Job: soda jerk .Ve .PP As you can see above the \f(CW\*(C`<TMPL_LOOP>\*(C'\fR takes a list of variable assignments and then iterates over the loop body producing output. .PP Often you'll want to generate a \f(CW\*(C`<TMPL_LOOP>\*(C'\fR's contents programmatically. Here's an example of how this can be done (many other ways are possible!): .PP .Vb 4 \& # a couple of arrays of data to put in a loop: \& my @words = qw(I Am Cool); \& my @numbers = qw(1 2 3); \& my @loop_data = (); # initialize an array to hold your loop \& \& while (@words and @numbers) { \& my %row_data; # get a fresh hash for the row data \& \& # fill in this row \& $row_data{WORD} = shift @words; \& $row_data{NUMBER} = shift @numbers; \& \& # the crucial step \- push a reference to this row into the loop! \& push(@loop_data, \e%row_data); \& } \& \& # finally, assign the loop data to the loop param, again with a reference: \& $template\->param(THIS_LOOP => \e@loop_data); .Ve .PP The above example would work with a template like: .PP .Vb 3 \& <TMPL_LOOP NAME="THIS_LOOP"> \& Word: <TMPL_VAR NAME="WORD"> \& Number: <TMPL_VAR NAME="NUMBER"> \& \& </TMPL_LOOP> .Ve .PP It would produce output like: .PP .Vb 2 \& Word: I \& Number: 1 \& \& Word: Am \& Number: 2 \& \& Word: Cool \& Number: 3 .Ve .PP \&\f(CW\*(C`<TMPL_LOOP>\*(C'\fRs within \f(CW\*(C`<TMPL_LOOP>\*(C'\fRs are fine and work as you would expect. If the syntax for the \f(CW\*(C`param()\*(C'\fR call has you stumped, here's an example of a param call with one nested loop: .PP .Vb 8 \& $template\->param( \& LOOP => [ \& { \& name => \*(AqBobby\*(Aq, \& nicknames => [{name => \*(Aqthe big bad wolf\*(Aq}, {name => \*(AqHe\-Man\*(Aq}], \& }, \& ], \& ); .Ve .PP Basically, each \f(CW\*(C`<TMPL_LOOP>\*(C'\fR gets an array reference. Inside the array are any number of hash references. These hashes contain the name=>value pairs for a single pass over the loop template. .PP Inside a \f(CW\*(C`<TMPL_LOOP>\*(C'\fR, the only variables that are usable are the ones from the \f(CW\*(C`<TMPL_LOOP>\*(C'\fR. The variables in the outer blocks are not visible within a template loop. For the computer-science geeks among you, a \f(CW\*(C`<TMPL_LOOP>\*(C'\fR introduces a new scope much like a perl subroutine call. If you want your variables to be global you can use \&\f(CW\*(C`global_vars\*(C'\fR option to \f(CW\*(C`new()\*(C'\fR described below. .SS "\s-1TMPL_INCLUDE\s0" .IX Subsection "TMPL_INCLUDE" .Vb 1 \& <TMPL_INCLUDE NAME="filename.tmpl"> .Ve .PP This tag includes a template directly into the current template at the point where the tag is found. The included template contents are used exactly as if its contents were physically included in the master template. .PP The file specified can be an absolute path (beginning with a '/' under Unix, for example). If it isn't absolute, the path to the enclosing file is tried first. After that the path in the environment variable \&\f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR is tried, if it exists. Next, the \*(L"path\*(R" option is consulted, first as-is and then with \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR prepended if available. As a final attempt, the filename is passed to \f(CW\*(C`open()\*(C'\fR directly. See below for more information on \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR and the \f(CW\*(C`path\*(C'\fR option to \f(CW\*(C`new()\*(C'\fR. .PP As a protection against infinitely recursive includes, an arbitrary limit of 10 levels deep is imposed. You can alter this limit with the \&\f(CW\*(C`max_includes\*(C'\fR option. See the entry for the \f(CW\*(C`max_includes\*(C'\fR option below for more details. .SS "\s-1TMPL_IF\s0" .IX Subsection "TMPL_IF" .Vb 1 \& <TMPL_IF NAME="PARAMETER_NAME"> ... </TMPL_IF> .Ve .PP The \f(CW\*(C`<TMPL_IF>\*(C'\fR tag allows you to include or not include a block of the template based on the value of a given parameter name. If the parameter is given a value that is true for Perl \- like '1' \- then the block is included in the output. If it is not defined, or given a false value \- like '0' \- then it is skipped. The parameters are specified the same way as with \f(CW\*(C`<TMPL_VAR>\*(C'\fR. .PP Example Template: .PP .Vb 3 \& <TMPL_IF NAME="BOOL"> \& Some text that only gets displayed if BOOL is true! \& </TMPL_IF> .Ve .PP Now if you call \f(CW\*(C`$template\->param(BOOL => 1)\*(C'\fR then the above block will be included by output. .PP \&\f(CW\*(C`<TMPL_IF> </TMPL_IF>\*(C'\fR blocks can include any valid HTML::Template construct \- \f(CW\*(C`VAR\*(C'\fRs and \f(CW\*(C`LOOP\*(C'\fRs and other \f(CW\*(C`IF\*(C'\fR/\f(CW\*(C`ELSE\*(C'\fR blocks. Note, however, that intersecting a \f(CW\*(C`<TMPL_IF>\*(C'\fR and a \f(CW\*(C`<TMPL_LOOP>\*(C'\fR is invalid. .PP .Vb 5 \& Not going to work: \& <TMPL_IF BOOL> \& <TMPL_LOOP SOME_LOOP> \& </TMPL_IF> \& </TMPL_LOOP> .Ve .PP If the name of a \f(CW\*(C`<TMPL_LOOP>\*(C'\fR is used in a \f(CW\*(C`<TMPL_IF>\*(C'\fR, the \f(CW\*(C`IF\*(C'\fR block will output if the loop has at least one row. Example: .PP .Vb 3 \& <TMPL_IF LOOP_ONE> \& This will output if the loop is not empty. \& </TMPL_IF> \& \& <TMPL_LOOP LOOP_ONE> \& .... \& </TMPL_LOOP> .Ve .PP \&\s-1WARNING:\s0 Much of the benefit of HTML::Template is in decoupling your Perl and \s-1HTML. \s0 If you introduce numerous cases where you have \&\f(CW\*(C`TMPL_IF\*(C'\fRs and matching Perl \f(CW\*(C`if\*(C'\fRs, you will create a maintenance problem in keeping the two synchronized. I suggest you adopt the practice of only using \f(CW\*(C`TMPL_IF\*(C'\fR if you can do so without requiring a matching \f(CW\*(C`if\*(C'\fR in your Perl code. .SS "\s-1TMPL_ELSE\s0" .IX Subsection "TMPL_ELSE" .Vb 1 \& <TMPL_IF NAME="PARAMETER_NAME"> ... <TMPL_ELSE> ... </TMPL_IF> .Ve .PP You can include an alternate block in your \f(CW\*(C`<TMPL_IF>\*(C'\fR block by using \&\f(CW\*(C`<TMPL_ELSE>\*(C'\fR. \s-1NOTE:\s0 You still end the block with \f(CW\*(C`</TMPL_IF>\*(C'\fR, not \f(CW\*(C`</TMPL_ELSE>\*(C'\fR! .PP .Vb 6 \& Example: \& <TMPL_IF BOOL> \& Some text that is included only if BOOL is true \& <TMPL_ELSE> \& Some text that is included only if BOOL is false \& </TMPL_IF> .Ve .SS "\s-1TMPL_UNLESS\s0" .IX Subsection "TMPL_UNLESS" .Vb 1 \& <TMPL_UNLESS NAME="PARAMETER_NAME"> ... </TMPL_UNLESS> .Ve .PP This tag is the opposite of \f(CW\*(C`<TMPL_IF>\*(C'\fR. The block is output if the \&\f(CW\*(C`PARAMETER_NAME\*(C'\fR is set false or not defined. You can use \&\f(CW\*(C`<TMPL_ELSE>\*(C'\fR with \f(CW\*(C`<TMPL_UNLESS>\*(C'\fR just as you can with \f(CW\*(C`<TMPL_IF>\*(C'\fR. .PP .Vb 6 \& Example: \& <TMPL_UNLESS BOOL> \& Some text that is output only if BOOL is FALSE. \& <TMPL_ELSE> \& Some text that is output only if BOOL is TRUE. \& </TMPL_UNLESS> .Ve .PP If the name of a \f(CW\*(C`<TMPL_LOOP>\*(C'\fR is used in a \f(CW\*(C`<TMPL_UNLESS>\*(C'\fR, the \f(CW\*(C`<UNLESS>\*(C'\fR block output if the loop has zero rows. .PP .Vb 3 \& <TMPL_UNLESS LOOP_ONE> \& This will output if the loop is empty. \& </TMPL_UNLESS> \& \& <TMPL_LOOP LOOP_ONE> \& .... \& </TMPL_LOOP> .Ve .SS "\s-1NOTES\s0" .IX Subsection "NOTES" HTML::Template's tags are meant to mimic normal \s-1HTML\s0 tags. However, they are allowed to \*(L"break the rules\*(R". Something like: .PP .Vb 1 \& <img src="<TMPL_VAR IMAGE_SRC>"> .Ve .PP is not really valid \s-1HTML,\s0 but it is a perfectly valid use and will work as planned. .PP The \f(CW\*(C`NAME=\*(C'\fR in the tag is optional, although for extensibility's sake I recommend using it. Example \- \f(CW\*(C`<TMPL_LOOP LOOP_NAME>\*(C'\fR is acceptable. .PP If you're a fanatic about valid \s-1HTML\s0 and would like your templates to conform to valid \s-1HTML\s0 syntax, you may optionally type template tags in the form of \s-1HTML\s0 comments. This may be of use to \s-1HTML\s0 authors who would like to validate their templates' \s-1HTML\s0 syntax prior to HTML::Template processing, or who use DTD-savvy editing tools. .PP .Vb 1 \& <!\-\- TMPL_VAR NAME=PARAM1 \-\-> .Ve .PP In order to realize a dramatic savings in bandwidth, the standard (non-comment) tags will be used throughout this documentation. .SH "METHODS" .IX Header "METHODS" .SS "new" .IX Subsection "new" Call \f(CW\*(C`new()\*(C'\fR to create a new Template object: .PP .Vb 4 \& my $template = HTML::Template\->new( \& filename => \*(Aqfile.tmpl\*(Aq, \& option => \*(Aqvalue\*(Aq, \& ); .Ve .PP You must call \f(CW\*(C`new()\*(C'\fR with at least one \f(CW\*(C`name =\*(C'\fR value> pair specifying how to access the template text. You can use \f(CW\*(C`filename => \*(Aqfile.tmpl\*(Aq\*(C'\fR to specify a filename to be opened as the template. Alternately you can use: .PP .Vb 4 \& my $t = HTML::Template\->new( \& scalarref => $ref_to_template_text, \& option => \*(Aqvalue\*(Aq, \& ); .Ve .PP and .PP .Vb 4 \& my $t = HTML::Template\->new( \& arrayref => $ref_to_array_of_lines, \& option => \*(Aqvalue\*(Aq, \& ); .Ve .PP 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. .PP You can also read the template from an already opened filehandle, either traditionally as a glob or as a FileHandle: .PP .Vb 1 \& my $t = HTML::Template\->new(filehandle => *FH, option => \*(Aqvalue\*(Aq); .Ve .PP The four \f(CW\*(C`new()\*(C'\fR calling methods can also be accessed as below, if you prefer. .PP .Vb 1 \& my $t = HTML::Template\->new_file(\*(Aqfile.tmpl\*(Aq, option => \*(Aqvalue\*(Aq); \& \& my $t = HTML::Template\->new_scalar_ref($ref_to_template_text, option => \*(Aqvalue\*(Aq); \& \& my $t = HTML::Template\->new_array_ref($ref_to_array_of_lines, option => \*(Aqvalue\*(Aq); \& \& my $t = HTML::Template\->new_filehandle($fh, option => \*(Aqvalue\*(Aq); .Ve .PP And as a final option, for those that might prefer it, you can call new as: .PP .Vb 4 \& my $t = HTML::Template\->new( \& type => \*(Aqfilename\*(Aq, \& source => \*(Aqfile.tmpl\*(Aq, \& ); .Ve .PP Which works for all three of the source types. .PP If the environment variable \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR is set and your filename doesn't begin with \*(L"/\*(R", then the path will be relative to the value of c<\s-1HTML_TEMPLATE_ROOT\s0>. .PP \&\fBExample\fR \- if the environment variable \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR is set to \&\fI/home/sam\fR and I call \f(CW\*(C`HTML::Template\->new()\*(C'\fR with filename set to \*(L"sam.tmpl\*(R", HTML::Template will try to open \fI/home/sam/sam.tmpl\fR to access the template file. You can also affect the search path for files with the \f(CW\*(C`path\*(C'\fR option to \f(CW\*(C`new()\*(C'\fR \- see below for more information. .PP You can modify the Template object's behavior with \f(CW\*(C`new()\*(C'\fR. The options are available: .PP \fIError Detection Options\fR .IX Subsection "Error Detection Options" .IP "\(bu" 4 die_on_bad_params .Sp If set to 0 the module will let you call: .Sp .Vb 1 \& $template\->param(param_name => \*(Aqvalue\*(Aq) .Ve .Sp even if 'param_name' doesn't exist in the template body. Defaults to 1. .IP "\(bu" 4 force_untaint .Sp If set to 1 the module will not allow you to set unescaped parameters with tainted values. If set to 2 you will have to untaint all parameters, including ones with the escape attribute. This option makes sure you untaint everything so you don't accidentally introduce e.g. cross-site-scripting (\s-1XSS\s0) vulnerabilities. Requires taint mode. Defaults to 0. .IP "\(bu" 4 strict \- if set to 0 the module will allow things that look like they might be TMPL_* tags to get by without dieing. Example: .Sp .Vb 1 \& <TMPL_HUH NAME=ZUH> .Ve .Sp Would normally cause an error, but if you call new with \f(CW\*(C`strict => 0\*(C'\fR HTML::Template will ignore it. Defaults to 1. .IP "\(bu" 4 vanguard_compatibility_mode .Sp If set to 1 the module will expect to see \f(CW\*(C`<TMPL_VAR>\*(C'\fRs that look like \f(CW\*(C`%NAME%\*(C'\fR in addition to the standard syntax. Also sets \&\f(CW\*(C`die_on_bad_params =\*(C'\fR 0>. If you're not at Vanguard Media trying to use an old format template don't worry about this one. Defaults to 0. .PP \fICaching Options\fR .IX Subsection "Caching Options" .IP "\(bu" 4 cache .Sp If set to 1 the module will cache in memory the parsed templates based on the filename parameter, the modification date of the file and the options passed to \f(CW\*(C`new()\*(C'\fR. 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 \&\f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR tags, but again, only if the template is opened with filename parameter. .Sp This is mainly of use in a persistent environment like Apache/mod_perl. It has absolutely no benefit in a normal \s-1CGI\s0 environment since the script is unloaded from memory after every request. For a cache that does work for a non-persistent environment see the \f(CW\*(C`shared_cache\*(C'\fR option below. .Sp My simplistic testing shows that using cache yields a 90% performance increase under mod_perl. Cache defaults to 0. .IP "\(bu" 4 shared_cache .Sp If set to 1 the module will store its cache in shared memory using the IPC::SharedCache module (available from \s-1CPAN\s0). The effect of this will be to maintain a single shared copy of each parsed template for all instances of HTML::Template on the same machine to use. This can be a significant reduction in memory usage in an environment with a single machine but multiple servers. 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 non-persistent environment (like normal \s-1CGI\s0) \- normal caching is only useful in a persistent environment like Apache/mod_perl. .Sp By default HTML::Template uses the \s-1IPC\s0 key '\s-1TMPL\s0' as a shared root segment (0x4c504d54 in hex), but this can be changed by setting the \&\f(CW\*(C`ipc_key\*(C'\fR \f(CW\*(C`new()\*(C'\fR parameter to another 4\-character or integer key. Other options can be used to affect the shared memory cache correspond to IPC::SharedCache options \- \f(CW\*(C`ipc_mode\*(C'\fR, \f(CW\*(C`ipc_segment_size\*(C'\fR and \&\f(CW\*(C`ipc_max_size\*(C'\fR. See IPC::SharedCache for a description of how these work \- in most cases you shouldn't need to change them from the defaults. .Sp For more information about the shared memory cache system used by HTML::Template see IPC::SharedCache. .IP "\(bu" 4 double_cache .Sp If set to 1 the module will use a combination of \f(CW\*(C`shared_cache\*(C'\fR 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 \f(CW\*(C`shared_cache\*(C'\fR apply to \f(CW\*(C`double_cache\*(C'\fR as well. Defaults to 0. .IP "\(bu" 4 blind_cache .Sp 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 \f(CW\*(C`blind_cache\*(C'\fR performing only 1 to 2 percent faster than cache under mod_perl. .Sp \&\fB\s-1NOTE\s0\fR: Combining this option with shared_cache can result in stale templates stuck permanently in shared memory! .IP "\(bu" 4 file_cache .Sp 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 \f(CW\*(C`shared_cache\*(C'\fR, it will work in a non-persistent environments (like \s-1CGI\s0). Default is 0. .Sp If you set this option you must set the \f(CW\*(C`file_cache_dir\*(C'\fR option. See below for details. .Sp \&\fB\s-1NOTE\s0\fR: Storable uses \f(CW\*(C`flock()\*(C'\fR to ensure safe access to cache files. Using \f(CW\*(C`file_cache\*(C'\fR on a system or filesystem (like \s-1NFS\s0) without \&\f(CW\*(C`flock()\*(C'\fR support is dangerous. .IP "\(bu" 4 file_cache_dir .Sp Sets the directory where the module will store the cache files if \&\f(CW\*(C`file_cache\*(C'\fR 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. .IP "\(bu" 4 file_cache_dir_mode .Sp Sets the file mode for newly created \f(CW\*(C`file_cache\*(C'\fR directories and subdirectories. Defaults to \*(L"0700\*(R" for security but this may be inconvenient if you do not have access to the account running the webserver. .IP "\(bu" 4 double_file_cache .Sp If set to 1 the module will use a combination of \f(CW\*(C`file_cache\*(C'\fR and normal \f(CW\*(C`cache\*(C'\fR mode for the best possible caching. The file_cache_* options that work with file_cache apply to \f(CW\*(C`double_file_cache\*(C'\fR as well. Defaults to 0. .IP "\(bu" 4 cache_lazy_vars .Sp The option tells HTML::Template to cache the values returned from code references used for \f(CW\*(C`TMPL_VAR\*(C'\fRs. See \*(L"\s-1LAZY VALUES\*(R"\s0 for details. .IP "\(bu" 4 cache_lazy_loops .Sp The option tells HTML::Template to cache the values returned from code references used for \f(CW\*(C`TMPL_LOOP\*(C'\fRs. See \*(L"\s-1LAZY VALUES\*(R"\s0 for details. .PP \fIFilesystem Options\fR .IX Subsection "Filesystem Options" .IP "\(bu" 4 path .Sp You can set this variable with a list of paths to search for files specified with the \f(CW\*(C`filename\*(C'\fR option to \f(CW\*(C`new()\*(C'\fR and for files included with the \f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR tag. This list is only consulted when the filename is relative. The \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR environment variable is always tried first if it exists. Also, if \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR is set then an attempt will be made to prepend \f(CW\*(C`HTML_TEMPLATE_ROOT\*(C'\fR onto paths in the path array. In the case of a \f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR file, the path to the including file is also tried before path is consulted. .Sp Example: .Sp .Vb 4 \& my $template = HTML::Template\->new( \& filename => \*(Aqfile.tmpl\*(Aq, \& path => [\*(Aq/path/to/templates\*(Aq, \*(Aq/alternate/path\*(Aq], \& ); .Ve .Sp \&\fB\s-1NOTE\s0\fR: the paths in the path list must be expressed as \s-1UNIX\s0 paths, separated by the forward-slash character ('/'). .IP "\(bu" 4 search_path_on_include .Sp 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 \f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR 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. .IP "\(bu" 4 utf8 .Sp Setting this to true tells HTML::Template to treat your template files as \&\s-1UTF\-8\s0 encoded. This will apply to any file's passed to \f(CW\*(C`new()\*(C'\fR or any included files. It won't do anything special to scalars templates passed to \f(CW\*(C`new()\*(C'\fR since you should be doing the encoding on those yourself. .Sp .Vb 4 \& my $template = HTML::Template\->new( \& filename => \*(Aqumlauts_are_awesome.tmpl\*(Aq, \& utf8 => 1, \& ); .Ve .Sp Most templates are either \s-1ASCII \s0(the default) or \s-1UTF\-8\s0 encoded Unicode. But if you need some other encoding other than these 2, look at the \f(CW\*(C`open_mode\*(C'\fR option. .Sp \&\fB\s-1NOTE\s0\fR: The \f(CW\*(C`utf8\*(C'\fR and \f(CW\*(C`open_mode\*(C'\fR options cannot be used at the same time. .IP "\(bu" 4 open_mode .Sp You can set this option to an opening mode with which all template files will be opened. .Sp For example, if you want to use a template that is \s-1UTF\-16\s0 encoded unicode: .Sp .Vb 4 \& my $template = HTML::Template\->new( \& filename => \*(Aqfile.tmpl\*(Aq, \& open_mode => \*(Aq<:encoding(UTF\-16)\*(Aq, \& ); .Ve .Sp That way you can force a different encoding (than the default \s-1ASCII\s0 or \s-1UTF\-8\s0), \s-1CR/LF\s0 properties etc. on the template files. See PerlIO for details. .Sp \&\fB\s-1NOTE\s0\fR: this only works in perl 5.7.1 and above. .Sp \&\fB\s-1NOTE\s0\fR: you have to supply an opening mode that actually permits reading from the file handle. .Sp \&\fB\s-1NOTE\s0\fR: The \f(CW\*(C`utf8\*(C'\fR and \f(CW\*(C`open_mode\*(C'\fR options cannot be used at the same time. .PP \fIDebugging Options\fR .IX Subsection "Debugging Options" .IP "\(bu" 4 debug .Sp If set to 1 the module will write random debugging information to \s-1STDERR.\s0 Defaults to 0. .IP "\(bu" 4 stack_debug .Sp If set to 1 the module will use Data::Dumper to print out the contents of the parse_stack to \s-1STDERR. \s0 Defaults to 0. .IP "\(bu" 4 cache_debug .Sp If set to 1 the module will send information on cache loads, hits and misses to \s-1STDERR. \s0 Defaults to 0. .IP "\(bu" 4 shared_cache_debug .Sp If set to 1 the module will turn on the debug option in IPC::SharedCache. Defaults to 0. .IP "\(bu" 4 memory_debug .Sp If set to 1 the module will send information on cache memory usage to \s-1STDERR. \s0 Requires the GTop module. Defaults to 0. .PP \fIMiscellaneous Options\fR .IX Subsection "Miscellaneous Options" .IP "\(bu" 4 associate .Sp This option allows you to inherit the parameter values from other objects. The only requirement for the other object is that it have a \f(CW\*(C`param()\*(C'\fR method that works like HTML::Template's \f(CW\*(C`param()\*(C'\fR. A good candidate would be a \s-1CGI\s0 query object. Example: .Sp .Vb 5 \& my $query = CGI\->new; \& my $template = HTML::Template\->new( \& filename => \*(Aqtemplate.tmpl\*(Aq, \& associate => $query, \& ); .Ve .Sp Now, \f(CW\*(C`$template\->output()\*(C'\fR will act as though .Sp .Vb 1 \& $template\->param(form_field => $cgi\->param(\*(Aqform_field\*(Aq)); .Ve .Sp had been specified for each key/value pair that would be provided by the \&\f(CW\*(C`$cgi\->param()\*(C'\fR method. Parameters you set directly take precedence over associated parameters. .Sp 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: .Sp .Vb 4 \& my $template = HTML::Template\->new( \& filename => \*(Aqtemplate.tmpl\*(Aq, \& associate => [$query, $other_obj], \& ); .Ve .Sp \&\fB\s-1NOTE\s0\fR: The parameter names are matched in a case-insensitive manner. If you have two parameters in a \s-1CGI\s0 object like '\s-1NAME\s0' and 'Name' one will be chosen randomly by associate. This behavior can be changed by the \f(CW\*(C`case_sensitive\*(C'\fR option. .IP "\(bu" 4 case_sensitive .Sp 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 \f(CW\*(C`case_sensitive\*(C'\fR option: .Sp .Vb 8 \& my $template = HTML::Template\->new( \& filename => \*(Aqtemplate.tmpl\*(Aq, \& case_sensitive => 1 \& ); \& $template\->param( \& FieldA => \*(Aqfoo\*(Aq, \& fIELDa => \*(Aqbar\*(Aq, \& ); .Ve .Sp This option defaults to off. .Sp \&\fB\s-1NOTE\s0\fR: with \f(CW\*(C`case_sensitive\*(C'\fR and \f(CW\*(C`loop_context_vars\*(C'\fR the special loop variables are available in lower-case only. .IP "\(bu" 4 loop_context_vars .Sp When this parameter is set to true (it is false by default) extra variables that depend on the loop's context are made available inside a loop. These are: .RS 4 .IP "\(bu" 4 _\|_first_\|_ .Sp Value that is true for the first iteration of the loop and false every other time. .IP "\(bu" 4 _\|_last_\|_ .Sp Value that is true for the last iteration of the loop and false every other time. .IP "\(bu" 4 _\|_inner_\|_ .Sp Value that is true for the every iteration of the loop except for the first and last. .IP "\(bu" 4 _\|_outer_\|_ .Sp Value that is true for the first and last iterations of the loop. .IP "\(bu" 4 _\|_odd_\|_ .Sp Value that is true for the every odd iteration of the loop. .IP "\(bu" 4 _\|_even_\|_ .Sp Value that is true for the every even iteration of the loop. .IP "\(bu" 4 _\|_counter_\|_ .Sp An integer (starting from 1) whose value increments for each iteration of the loop. .IP "\(bu" 4 _\|_index_\|_ .Sp An integer (starting from 0) whose value increments for each iteration of the loop. .RE .RS 4 .Sp Just like any other \f(CW\*(C`TMPL_VAR\*(C'\fRs these variables can be used in \&\f(CW\*(C`<TMPL_IF>\*(C'\fR, \f(CW\*(C`<TMPL_UNLESS>\*(C'\fR and \f(CW\*(C`<TMPL_ELSE>\*(C'\fR to control how a loop is output. .Sp Example: .Sp .Vb 4 \& <TMPL_LOOP NAME="FOO"> \& <TMPL_IF NAME="_\|_first_\|_"> \& This only outputs on the first pass. \& </TMPL_IF> \& \& <TMPL_IF NAME="_\|_odd_\|_"> \& This outputs every other pass, on the odd passes. \& </TMPL_IF> \& \& <TMPL_UNLESS NAME="_\|_odd_\|_"> \& This outputs every other pass, on the even passes. \& </TMPL_UNLESS> \& \& <TMPL_IF NAME="_\|_inner_\|_"> \& This outputs on passes that are neither first nor last. \& </TMPL_IF> \& \& This is pass number <TMPL_VAR NAME="_\|_counter_\|_">. \& \& <TMPL_IF NAME="_\|_last_\|_"> \& This only outputs on the last pass. \& </TMPL_IF> \& </TMPL_LOOP> .Ve .Sp One use of this feature is to provide a \*(L"separator\*(R" similar in effect to the perl function \f(CW\*(C`join()\*(C'\fR. Example: .Sp .Vb 4 \& <TMPL_LOOP FRUIT> \& <TMPL_IF _\|_last_\|_> and </TMPL_IF> \& <TMPL_VAR KIND><TMPL_UNLESS _\|_last_\|_>, <TMPL_ELSE>.</TMPL_UNLESS> \& </TMPL_LOOP> .Ve .Sp Would output something like: .Sp .Vb 1 \& Apples, Oranges, Brains, Toes, and Kiwi. .Ve .Sp Given an appropriate \f(CW\*(C`param()\*(C'\fR call, of course. \fB\s-1NOTE\s0\fR: A loop with only a single pass will get both \f(CW\*(C`_\|_first_\|_\*(C'\fR and \f(CW\*(C`_\|_last_\|_\*(C'\fR set to true, but not \f(CW\*(C`_\|_inner_\|_\*(C'\fR. .RE .IP "\(bu" 4 no_includes .Sp Set this option to 1 to disallow the \f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR tag in the template file. This can be used to make opening untrusted templates \&\fBslightly\fR less dangerous. Defaults to 0. .IP "\(bu" 4 max_includes .Sp 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. .IP "\(bu" 4 die_on_missing_include .Sp If true, then HTML::Template will die if it can't find a file for a \&\f(CW\*(C`<TMPL_INCLUDE>\*(C'\fR. This defaults to true. .IP "\(bu" 4 global_vars .Sp Normally variables declared outside a loop are not available inside a loop. This option makes \f(CW\*(C`<TMPL_VAR>\*(C'\fRs like global variables in Perl \- they have unlimited scope. This option also affects \f(CW\*(C`<TMPL_IF>\*(C'\fR and \f(CW\*(C`<TMPL_UNLESS>\*(C'\fR. .Sp Example: .Sp .Vb 1 \& 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> .Ve .Sp Normally this wouldn't work as expected, since \f(CW\*(C`<TMPL_VAR NORMAL>\*(C'\fR's value outside the loop is not available inside the loop. .Sp 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 \f(CW\*(C`OUTER_VAR\*(C'\fR in the correct iteration: .Sp .Vb 7 \& <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> .Ve .Sp One side-effect of \f(CW\*(C`global_vars\*(C'\fR is that variables you set with \&\f(CW\*(C`param()\*(C'\fR that might otherwise be ignored when \f(CW\*(C`die_on_bad_params\*(C'\fR is off will stick around. This is necessary to allow inner loops to access values set for outer loops that don't directly use the value. .Sp \&\fB\s-1NOTE\s0\fR: \f(CW\*(C`global_vars\*(C'\fR is not \f(CW\*(C`global_loops\*(C'\fR (which does not exist). That means that loops you declare at one scope are not available inside other loops even when \f(CW\*(C`global_vars\*(C'\fR is on. .IP "\(bu" 4 filter .Sp 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. .Sp In the most simple usage, you simply assign a code reference to the filter parameter. This subroutine will receive a single argument \- a reference to a string containing the template file text. Here is an example that accepts templates with tags that look like \&\f(CW\*(C`!!!ZAP_VAR FOO!!!\*(C'\fR and transforms them into HTML::Template tags: .Sp .Vb 4 \& 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 => \*(Aqzap.tmpl\*(Aq, \& filter => $filter, \& ); .Ve .Sp More complicated usages are possible. You can request that your filter receives 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 \f(CW\*(C`sub\*(C'\fR key and the desired argument format using the \f(CW\*(C`format\*(C'\fR key. The available formats are \f(CW\*(C`scalar\*(C'\fR and \f(CW\*(C`array\*(C'\fR. Using the \f(CW\*(C`array\*(C'\fR format will incur a performance penalty but may be more convenient in some situations. .Sp .Vb 7 \& my $template = HTML::Template\->new( \& filename => \*(Aqzap.tmpl\*(Aq, \& filter => { \& sub => $filter, \& format => \*(Aqarray\*(Aq, \& } \& ); .Ve .Sp 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. .Sp .Vb 10 \& my $template = HTML::Template\->new( \& filename => \*(Aqzap.tmpl\*(Aq, \& filter => [ \& { \& sub => \e&decompress, \& format => \*(Aqscalar\*(Aq, \& }, \& { \& sub => \e&remove_spaces, \& format => \*(Aqarray\*(Aq, \& }, \& ] \& ); .Ve .Sp The specified filters will be called for any \f(CW\*(C`TMPL_INCLUDE\*(C'\fRed files just as they are for the main template file. .IP "\(bu" 4 default_escape .Sp Set this parameter to a valid escape type (see the \f(CW\*(C`escape\*(C'\fR option) and HTML::Template will apply the specified escaping to all variables unless they declare a different escape in the template. .SS "config" .IX Subsection "config" A package method that is used to set/get the global default configuration options. For instance, if you want to set the \f(CW\*(C`utf8\*(C'\fR flag to always be on for every template loaded by this process you would do: .PP .Vb 1 \& HTML::Template\->config(utf8 => 1); .Ve .PP Or if you wanted to check if the \f(CW\*(C`utf8\*(C'\fR flag was on or not, you could do: .PP .Vb 4 \& my %config = HTML::Template\->config; \& if( $config{utf8} ) { \& ... \& } .Ve .PP Any configuration options that are valid for \f(CW\*(C`new()\*(C'\fR are acceptable to be passed to this method. .SS "param" .IX Subsection "param" \&\f(CW\*(C`param()\*(C'\fR can be called in a number of ways .IP "1 \- To return a list of parameters in the template :" 4 .IX Item "1 - To return a list of parameters in the template :" .Vb 1 \& my @parameter_names = $self\->param(); .Ve .IP "2 \- To return the value set to a param :" 4 .IX Item "2 - To return the value set to a param :" .Vb 1 \& my $value = $self\->param(\*(AqPARAM\*(Aq); .Ve .IP "3 \- To set the value of a parameter :" 4 .IX Item "3 - To set the value of a parameter :" .Vb 2 \& # For simple TMPL_VARs: \& $self\->param(PARAM => \*(Aqvalue\*(Aq); \& \& # 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 \*(Aqvalue\*(Aq }); \& \& # And TMPL_LOOPs: \& $self\->param(LOOP_PARAM => [{PARAM => VALUE_FOR_FIRST_PASS}, {PARAM => VALUE_FOR_SECOND_PASS}]); .Ve .IP "4 \- To set the value of a number of parameters :" 4 .IX Item "4 - To set the value of a number of parameters :" .Vb 5 \& # For simple TMPL_VARs: \& $self\->param( \& PARAM => \*(Aqvalue\*(Aq, \& PARAM2 => \*(Aqvalue\*(Aq \& ); \& \& # And with some TMPL_LOOPs: \& $self\->param( \& PARAM => \*(Aqvalue\*(Aq, \& PARAM2 => \*(Aqvalue\*(Aq, \& 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}], \& ); .Ve .IP "5 \- To set the value of a a number of parameters using a hash-ref :" 4 .IX Item "5 - To set the value of a a number of parameters using a hash-ref :" .Vb 8 \& $self\->param( \& { \& PARAM => \*(Aqvalue\*(Aq, \& PARAM2 => \*(Aqvalue\*(Aq, \& 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}], \& } \& ); .Ve .Sp An error occurs if you try to set a value that is tainted if the \f(CW\*(C`force_untaint\*(C'\fR option is set. .SS "clear_params" .IX Subsection "clear_params" Sets all the parameters to undef. Useful internally, if nowhere else! .SS "output" .IX Subsection "output" \&\f(CW\*(C`output()\*(C'\fR returns the final result of the template. In most situations you'll want to print this, like: .PP .Vb 1 \& print $template\->output(); .Ve .PP When output is called each occurrence of \f(CW\*(C`<TMPL_VAR NAME=name>\*(C'\fR is replaced with the value assigned to \*(L"name\*(R" via \f(CW\*(C`param()\*(C'\fR. If a named parameter is unset it is simply replaced with ''. \f(CW\*(C`<TMPL_LOOP>\*(C'\fRs are evaluated once per parameter set, accumulating output on each pass. .PP Calling \f(CW\*(C`output()\*(C'\fR is guaranteed not to change the state of the HTML::Template object, in case you were wondering. This property is mostly important for the internal implementation of loops. .PP You may optionally supply a filehandle to print to automatically as the template is generated. This may improve performance and lower memory consumption. Example: .PP .Vb 1 \& $template\->output(print_to => *STDOUT); .Ve .PP The return value is undefined when using the \f(CW\*(C`print_to\*(C'\fR option. .SS "query" .IX Subsection "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 \f(CW\*(C`name\*(C'\fR option: .PP .Vb 3 \& if ($template\->query(name => \*(Aqfoo\*(Aq)) { \& # do something if a variable of any type named FOO is in the template \& } .Ve .PP This same usage returns the type of the parameter. The type is the same as the tag minus the leading '\s-1TMPL_\s0'. So, for example, a \f(CW\*(C`TMPL_VAR\*(C'\fR parameter returns '\s-1VAR\s0' from \f(CW\*(C`query()\*(C'\fR. .PP .Vb 3 \& if ($template\->query(name => \*(Aqfoo\*(Aq) eq \*(AqVAR\*(Aq) { \& # do something if FOO exists and is a TMPL_VAR \& } .Ve .PP Note that the variables associated with \f(CW\*(C`TMPL_IF\*(C'\fRs and \f(CW\*(C`TMPL_UNLESS\*(C'\fRs will be identified as '\s-1VAR\s0' unless they are also used in a \f(CW\*(C`TMPL_LOOP\*(C'\fR, in which case they will return '\s-1LOOP\s0'. .PP \&\f(CW\*(C`query()\*(C'\fR also allows you to get a list of parameters inside a loop (and inside loops inside loops). Example loop: .PP .Vb 8 \& <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> .Ve .PP And some query calls: .PP .Vb 2 \& # returns \*(AqLOOP\*(Aq \& $type = $template\->query(name => \*(AqEXAMPLE_LOOP\*(Aq); \& \& # returns (\*(Aqbop\*(Aq, \*(Aqbee\*(Aq, \*(Aqexample_inner_loop\*(Aq) \& @param_names = $template\->query(loop => \*(AqEXAMPLE_LOOP\*(Aq); \& \& # both return \*(AqVAR\*(Aq \& $type = $template\->query(name => [\*(AqEXAMPLE_LOOP\*(Aq, \*(AqBEE\*(Aq]); \& $type = $template\->query(name => [\*(AqEXAMPLE_LOOP\*(Aq, \*(AqBOP\*(Aq]); \& \& # and this one returns \*(AqLOOP\*(Aq \& $type = $template\->query(name => [\*(AqEXAMPLE_LOOP\*(Aq, \*(AqEXAMPLE_INNER_LOOP\*(Aq]); \& \& # and finally, this returns (\*(Aqinner_bee\*(Aq, \*(Aqinner_bop\*(Aq) \& @inner_param_names = $template\->query(loop => [\*(AqEXAMPLE_LOOP\*(Aq, \*(AqEXAMPLE_INNER_LOOP\*(Aq]); \& \& # for non existent parameter names you get undef this returns undef. \& $type = $template\->query(name => \*(AqDWEAZLE_ZAPPA\*(Aq); \& \& # calling loop on a non\-loop parameter name will cause an error. This dies: \& $type = $template\->query(loop => \*(AqDWEAZLE_ZAPPA\*(Aq); .Ve .PP As you can see above the \f(CW\*(C`loop\*(C'\fR option returns a list of parameter names and both \f(CW\*(C`name\*(C'\fR and \f(CW\*(C`loop\*(C'\fR take array refs in order to refer to parameters inside loops. It is an error to use \f(CW\*(C`loop\*(C'\fR with a parameter that is not a loop. .PP Note that all the names are returned in lowercase and the types are uppercase. .PP Just like \f(CW\*(C`param()\*(C'\fR, \f(CW\*(C`query()\*(C'\fR with no arguments returns all the parameter names in the template at the top level. .SH "LAZY VALUES" .IX Header "LAZY VALUES" As mentioned above, both \f(CW\*(C`TMPL_VAR\*(C'\fR and \f(CW\*(C`TMPL_LOOP\*(C'\fR values can be code references. These code references are only executed if the variable or loop is used in the template. This is extremely useful if you want to make a variable available to template designers but it can be expensive to calculate, so you only want to do so if you have to. .PP Maybe an example will help to illustrate. Let's say you have a template like this: .PP .Vb 3 \& <tmpl_if we_care> \& <tmpl_if life_universe_and_everything> \& </tmpl_if> .Ve .PP If \f(CW\*(C`life_universe_and_everything\*(C'\fR is expensive to calculate we can wrap it's calculation in a code reference and HTML::Template will only execute that code if \f(CW\*(C`we_care\*(C'\fR is also true. .PP .Vb 1 \& $tmpl\->param(life_universe_and_everything => sub { calculate_42() }); .Ve .PP Your code reference will be given a single argument, the HTML::Template object in use. In the above example, if we wanted \f(CW\*(C`calculate_42()\*(C'\fR to have this object we'd do something like this: .PP .Vb 1 \& $tmpl\->param(life_universe_and_everything => sub { calculate_42(shift) }); .Ve .PP This same approach can be used for \f(CW\*(C`TMPL_LOOP\*(C'\fRs too: .PP .Vb 5 \& <tmpl_if we_care> \& <tmpl_loop needles_in_haystack> \& Found <tmpl_var _\|_counter>! \& </tmpl_loop> \& </tmpl_if> .Ve .PP And in your Perl code: .PP .Vb 1 \& $tmpl\->param(needles_in_haystack => sub { find_needles() }); .Ve .PP The only difference in the \f(CW\*(C`TMPL_LOOP\*(C'\fR case is that the subroutine needs to return a reference to an \s-1ARRAY,\s0 not just a scalar value. .SS "Multiple Calls" .IX Subsection "Multiple Calls" It's important to recognize that while this feature is designed to save processing time when things aren't needed, if you're not careful it can actually increase the number of times you perform your calculation. HTML::Template calls your code reference each time it seems your loop in the template, this includes the times that you might use the loop in a conditional (\f(CW\*(C`TMPL_IF\*(C'\fR or \f(CW\*(C`TMPL_UNLESS\*(C'\fR). For instance: .PP .Vb 9 \& <tmpl_if we care> \& <tmpl_if needles_in_haystack> \& <tmpl_loop needles_in_haystack> \& Found <tmpl_var _\|_counter>! \& </tmpl_loop> \& <tmpl_else> \& No needles found! \& </tmpl_if> \& </tmpl_if> .Ve .PP This will actually call \f(CW\*(C`find_needles()\*(C'\fR twice which will be even worse than you had before. One way to work around this is to cache the return value yourself: .PP .Vb 2 \& my $needles; \& $tmpl\->param(needles_in_haystack => sub { defined $needles ? $needles : $needles = find_needles() }); .Ve .SH "BUGS" .IX Header "BUGS" I am aware of no bugs \- if you find one, join the mailing list and tell us about it. You can join the HTML::Template mailing-list by visiting: .PP .Vb 1 \& http://lists.sourceforge.net/lists/listinfo/html\-template\-users .Ve .PP Of course, you can still email me directly (\f(CW\*(C`sam@tregar.com\*(C'\fR) with bugs, but I reserve the right to forward bug reports to the mailing list. .PP When submitting bug reports, be sure to include full details, including the \s-1VERSION\s0 of the module, a test script and a test template demonstrating the problem! .PP If you're feeling really adventurous, HTML::Template has a publically available Git repository. See below for more information in the \&\s-1PUBLIC GIT REPOSITORY\s0 section. .SH "CREDITS" .IX Header "CREDITS" This module was the brain child of my boss, Jesse Erlbaum (\f(CW\*(C`jesse@vm.com\*(C'\fR) at Vanguard Media (http://vm.com) . The most original idea in this module \- the \f(CW\*(C`<TMPL_LOOP>\*(C'\fR \- was entirely his. .PP Fixes, Bug Reports, Optimizations and Ideas have been generously provided by: .IP "\(bu" 4 Richard Chen .IP "\(bu" 4 Mike Blazer .IP "\(bu" 4 Adriano Nagelschmidt Rodrigues .IP "\(bu" 4 Andrej Mikus .IP "\(bu" 4 Ilya Obshadko .IP "\(bu" 4 Kevin Puetz .IP "\(bu" 4 Steve Reppucci .IP "\(bu" 4 Richard Dice .IP "\(bu" 4 Tom Hukins .IP "\(bu" 4 Eric Zylberstejn .IP "\(bu" 4 David Glasser .IP "\(bu" 4 Peter Marelas .IP "\(bu" 4 James William Carlson .IP "\(bu" 4 Frank D. Cringle .IP "\(bu" 4 Winfried Koenig .IP "\(bu" 4 Matthew Wickline .IP "\(bu" 4 Doug Steinwand .IP "\(bu" 4 Drew Taylor .IP "\(bu" 4 Tobias Brox .IP "\(bu" 4 Michael Lloyd .IP "\(bu" 4 Simran Gambhir .IP "\(bu" 4 Chris Houser <chouser@bluweb.com> .IP "\(bu" 4 Larry Moore .IP "\(bu" 4 Todd Larason .IP "\(bu" 4 Jody Biggs .IP "\(bu" 4 T.J. Mather .IP "\(bu" 4 Martin Schroth .IP "\(bu" 4 Dave Wolfe .IP "\(bu" 4 uchum .IP "\(bu" 4 Kawai Takanori .IP "\(bu" 4 Peter Guelich .IP "\(bu" 4 Chris Nokleberg .IP "\(bu" 4 Ralph Corderoy .IP "\(bu" 4 William Ward .IP "\(bu" 4 Ade Olonoh .IP "\(bu" 4 Mark Stosberg .IP "\(bu" 4 Lance Thomas .IP "\(bu" 4 Roland Giersig .IP "\(bu" 4 Jere Julian .IP "\(bu" 4 Peter Leonard .IP "\(bu" 4 Kenny Smith .IP "\(bu" 4 Sean P. Scanlon .IP "\(bu" 4 Martin Pfeffer .IP "\(bu" 4 David Ferrance .IP "\(bu" 4 Gyepi Sam .IP "\(bu" 4 Darren Chamberlain .IP "\(bu" 4 Paul Baker .IP "\(bu" 4 Gabor Szabo .IP "\(bu" 4 Craig Manley .IP "\(bu" 4 Richard Fein .IP "\(bu" 4 The Phalanx Project .IP "\(bu" 4 Sven Neuhaus .IP "\(bu" 4 Michael Peters .IP "\(bu" 4 Jan Dubois .IP "\(bu" 4 Moritz Lenz .PP Thanks! .SH "WEBSITE" .IX Header "WEBSITE" You can find information about HTML::Template and other related modules at: .PP .Vb 1 \& http://html\-template.sourceforge.net .Ve .SH "PUBLIC GIT REPOSITORY" .IX Header "PUBLIC GIT REPOSITORY" HTML::Template now has a publicly accessible Git repository provided by GitHub (github.com). You can access it by going to https://github.com/mpeters/html\-template. Give it a try! .SH "AUTHOR" .IX Header "AUTHOR" Sam Tregar, \f(CW\*(C`sam@tregar.com\*(C'\fR .SH "CO-MAINTAINER" .IX Header "CO-MAINTAINER" Michael Peters, \f(CW\*(C`mpeters@plusthree.com\*(C'\fR .SH "LICENSE" .IX Header "LICENSE" .Vb 2 \& HTML::Template : A module for using HTML Templates with Perl \& Copyright (C) 2000\-2011 Sam Tregar (sam@tregar.com) \& \& This module is free software; you can redistribute it and/or modify it \& under the same terms as Perl itself, which means using 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. If not, I\*(Aqll 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 .Ve