.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 >0, 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 .\" ======================================================================== .\" .IX Title "HTML::Template::SYNTAX 3pm" .TH HTML::Template::SYNTAX 3pm 2024-01-10 "perl v5.38.2" "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::SYNTAX \- syntax of html template language for HTML::Template .SH SYNOPSIS .IX Header "SYNOPSIS" This help is only on syntax of html template files. For perl interface of HTML::Template::Pro you should see "SYNOPSIS" in HTML::Template::PerlInterface. .PP First you make a template \- this is just a normal HTML file with a few extra tags, the simplest being .PP For example, test.tmpl: .PP .Vb 8 \& \& Test Template \& \& My Home Directory is \&

\& My Path is set to \& \& .Ve .PP Now define the value for HOME and PATH, for example, in perl it will look like .PP .Vb 2 \& $template\->param(HOME => $ENV{HOME}); \& $template\->param(PATH => $ENV{PATH}); .Ve .PP and process the template. If all is well in the universe this should show something like this in your browser: .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 HTML templates simple and natural. It extends standard HTML with a few new HTML-esque tags \- , , , , and . (HTML::Template::Pro also supports tag.) 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. .PP This module is licensed under the (L)GPL or perl license. See the LICENSE 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 the HTML::Template website: .PP .Vb 1 \& http://html\-template.sourceforge.net .Ve .SH MOTIVATION .IX Header "MOTIVATION" It is true that there are a number of packages out there to do HTML templates. On the one hand you have things like HTML::Embperl which allows you freely mix Perl with HTML. 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 HTML, 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 HTML 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 HTML templates results in CGIs containing lots of HTML since the HTML itself cannot represent loops. The introduction of loop statements in the HTML 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 HTML, it just augments them to interact a little better. And it's pretty fast. .SH "THE TAGS" .IX Header "THE TAGS" .SS "GENERAL TAG SYNTAX" .IX Subsection "GENERAL TAG SYNTAX" A generic HTML::Template tag that is supported by HTML::Template::Pro looks like . Tags are case-insensitve: is acceptable. Single quotes can be used, quotes can be omitted, and option name could be often guessed as in . .PP template tags could be decorated as html comments .PP Also, as HTML::Template::Pro extension (starting from version 0.90), template tags could also be decorated as xml .PP See NOTES. .SS TMPL_VAR .IX Subsection "TMPL_VAR" .Vb 1 \& .Ve .PP The tag is very simple. For each tag in the template you call \f(CW$template\fR\->param(PARAMETER_NAME => "VALUE"). When the template is output the is replaced with the VALUE text you specified. If you don't set a parameter it just gets skipped in the output. .PP Optionally you can use the "ESCAPE=HTML" option in the tag to indicate that you want the value to be HTML-escaped before being returned from output (the old ESCAPE=1 syntax is still supported). This means that the ", <, >, and & characters get translated into ", <, > and & respectively. This is useful when you want to use a TMPL_VAR in a context where those characters would cause trouble. Example: .PP .Vb 1 \& "> .Ve .PP If you called \f(CWparam()\fR with a value like sam"my you'll get in trouble with HTML's idea of a double-quote. On the other hand, if you use ESCAPE=HTML, like this: .PP .Vb 1 \& "> .Ve .PP You'll get what you wanted no matter what value happens to be passed in for param. You can also write ESCAPE="HTML", ESCAPE='HTML' and ESCAPE='1'. .PP "ESCAPE=0" and "ESCAPE=NONE" turn off escaping, which is the default behavior. .PP There is also the "ESCAPE=URL" option which may be used for VARs that populate a URL. It will do URL escaping, like replacing ' ' with '+' and '/' with '%2F'. .PP There is also the "ESCAPE=JS" option which may be used for VARs that need to be placed within a Javascript string. All \en, \er, ' and " characters are escaped. .PP You can assign a default value to a variable with the DEFAULT attribute. For example, this will output "the devil gave me a taco" if the "who" variable is not set. .PP .Vb 1 \& The gave me a taco. .Ve .SS TMPL_LOOP .IX Subsection "TMPL_LOOP" .Vb 1 \& ... .Ve .PP The tag is a bit more complicated than . The tag allows you to delimit a section of text and give it a name. Inside this named loop you place s. Now you pass to \&\f(CWparam()\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 .Vb 1 \& In the template: \& \& \& Name:
\& Job:

\& \& \& \& In the script: \& \& $template\->param(EMPLOYEE_INFO => [ \& { name => \*(AqSam\*(Aq, job => \*(Aqprogrammer\*(Aq }, \& { name => \*(AqSteve\*(Aq, job => \*(Aqsoda jerk\*(Aq }, \& ] \& ); \& print $template\->output(); \& \& \& The output in a browser: \& \& Name: Sam \& Job: programmer \& \& Name: Steve \& Job: soda jerk .Ve .PP As you can see above the takes a list of variable assignments and then iterates over the loop body producing output. .PP Often you'll want to generate a 's contents programmatically. Here's an example of how this can be done (many other ways are possible!): .PP .Vb 3 \& # 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 4 \& \& Word:
\& Number:

\& .Ve .PP It would produce output like: .PP .Vb 2 \& Word: I \& Number: 1 \& \& Word: Am \& Number: 2 \& \& Word: Cool \& Number: 3 .Ve .PP s within s are fine and work as you would expect. If the syntax for the \f(CWparam()\fR call has you stumped, here's an example of a param call with one nested loop: .PP .Vb 9 \& $template\->param(LOOP => [ \& { name => \*(AqBobby\*(Aq, \& nicknames => [ \& { name => \*(Aqthe big bad wolf\*(Aq }, \& { name => \*(AqHe\-Man\*(Aq }, \& ], \& }, \& ], \& ); .Ve .PP Basically, each 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 , the only variables that are usable are the ones from the . The variables in the outer blocks are not visible within a template loop. For the computer-science geeks among you, a introduces a new scope much like a perl subroutine call. If you want your variables to be global you can use \&'global_vars' option to \fBnew()\fR described below. .SS TMPL_INCLUDE .IX Subsection "TMPL_INCLUDE" .Vb 2 \& \& .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 HTML_TEMPLATE_ROOT is tried, if it exists. Next, the "path" option is consulted, first as-is and then with HTML_TEMPLATE_ROOT prepended if available. As a final attempt, the filename is passed to \fBopen()\fR directly. See below for more information on HTML_TEMPLATE_ROOT and the "path" option to \fBnew()\fR. .PP As a protection against infinitly recursive includes, an arbitrary limit of 10 levels deep is imposed. You can alter this limit with the "max_includes" option. See the entry for the "max_includes" option below for more details. .PP For the see "INCLUDE extension to Expr" for more details. .SS TMPL_IF .IX Subsection "TMPL_IF" .Vb 1 \& ... .Ve .PP The 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 TMPL_VAR. .PP Example Template: .PP .Vb 3 \& \& Some text that only gets displayed if BOOL is true! \& .Ve .PP Now if you call \f(CW$template\fR\->param(BOOL => 1) then the above block will be included by output. .PP blocks can include any valid HTML::Template construct \- VARs and LOOPs and other IF/ELSE blocks. Note, however, that intersecting a and a is invalid. .PP .Vb 5 \& Not going to work: \& \& \& \& .Ve .PP If the name of a TMPL_LOOP is used in a TMPL_IF, the IF block will output if the loop has at least one row. Example: .PP .Vb 3 \& \& This will output if the loop is not empty. \& \& \& \& .... \& .Ve .PP WARNING: Much of the benefit of HTML::Template is in decoupling your Perl and HTML. If you introduce numerous cases where you have TMPL_IFs and matching Perl \fBif()\fRs, you will create a maintenance problem in keeping the two synchronized. I suggest you adopt the practice of only using TMPL_IF if you can do so without requiring a matching \fBif()\fR in your Perl code. .SS TMPL_ELSIF .IX Subsection "TMPL_ELSIF" .Vb 4 \& ... \& ... \& ... \& ... .Ve .PP WARNING: TMPL_ELSIF is a HTML::Template::Pro extension! It is not supported in HTML::Template (as of 2.9). .SS TMPL_ELSE .IX Subsection "TMPL_ELSE" .Vb 1 \& ... ... .Ve .PP You can include an alternate block in your TMPL_IF block by using TMPL_ELSE. NOTE: You still end the block with , not ! .PP .Vb 1 \& Example: \& \& \& Some text that is included only if BOOL is true \& \& Some text that is included only if BOOL is false \& .Ve .SS TMPL_UNLESS .IX Subsection "TMPL_UNLESS" .Vb 1 \& ... .Ve .PP This tag is the opposite of . The block is output if the CONTROL_PARAMETER is set false or not defined. You can use with just as you can with . .PP .Vb 1 \& Example: \& \& \& Some text that is output only if BOOL is FALSE. \& \& Some text that is output only if BOOL is TRUE. \& .Ve .PP If the name of a TMPL_LOOP is used in a TMPL_UNLESS, the UNLESS block output if the loop has zero rows. .PP .Vb 3 \& \& This will output if the loop is empty. \& \& \& \& .... \& .Ve .SS NOTES .IX Subsection "NOTES" HTML::Template's tags are meant to mimic normal HTML tags. However, they are allowed to "break the rules". Something like: .PP .Vb 1 \& .Ve .PP is not really valid HTML, but it is a perfectly valid use and will work as planned. .PP The "NAME=" in the tag is optional, although for extensibility's sake I recommend using it. Example \- "" is acceptable. .PP If you're a fanatic about valid HTML and would like your templates to conform to valid HTML syntax, you may optionally type template tags in the form of HTML comments. This may be of use to HTML authors who would like to validate their templates' HTML syntax prior to HTML::Template processing, or who use DTD-savvy editing tools. .PP .Vb 1 \& .Ve .PP In order to realize a dramatic savings in bandwidth, the standard (non-comment) tags will be used throughout this documentation. .SH "EXPR EXTENSION" .IX Header "EXPR EXTENSION" This module supports an extension to HTML::Template which allows expressions in the template syntax which was implemented in HTML::Template::Expr. See HTML::Template::Expr for details. .PP Expression support includes comparisons, math operations, string operations and a mechanism to allow you add your own functions at runtime. The basic syntax is: .PP .Vb 3 \& \& I\*(Aqve got a lot of bananas. \& .Ve .PP This will output "I've got a lot of bananas" if you call: .PP .Vb 1 \& $template\->param(banana_count => 100); .Ve .PP In your script. s also work with expressions: .PP .Vb 1 \& I\*(Aqd like to have bananas. .Ve .PP This will output "I'd like to have 200 bananas." with the same \fBparam()\fR call as above. .SH "BASIC SYNTAX" .IX Header "BASIC SYNTAX" .SS Variables .IX Subsection "Variables" Variables are unquoted alphanumeric strings with the same restrictions as variable names in HTML::Template. Their values are set through \&\fBparam()\fR, just like normal HTML::Template variables. For example, these two lines are equivalent: .PP .Vb 1 \& \& \& .Ve .SS "Emiliano Bruni extension to Expr" .IX Subsection "Emiliano Bruni extension to Expr" original HTML::Template allows almost arbitrary chars in parameter names, but original HTML::Template::Expr (as to 0.04) allows variables in the \&'EXPR' tag to be only m![A\-Za\-z_][A\-Za\-z0\-9_]*!. .PP With this extension, arbitrary chars can be used in variable name inside the 'EXPR' tag if bracketed in ${}, as, for example, EXPR="${foo.bar} eq 'a'". Note that old bracketing into {} is considered obsolete, as it will clash with JSON assignments like A = { "key" => "val" }. .PP COMPATIBILITY WARNING. Currently, this extension is not present in HTML::Template::Expr (as of 0.04). .SS "INCLUDE extension to Expr" .IX Subsection "INCLUDE extension to Expr" With this extension, you can write something like or , or even .PP SECURITY WARNING. Using of this extension with untrasted values of variables is a potential security leak (as in with USER_INPUT='/etc/passwd'). Omit it unless you know what you are doing. .PP COMPATIBILITY WARNING. Currently, this extension is not present in HTML::Template::Expr (as of 0.04). .SS Constants .IX Subsection "Constants" Numbers are unquoted strings of numbers and may have a single "." to indicate a floating point number. For example: .PP .Vb 1 \& .Ve .PP String constants must be enclosed in quotes, single or double. For example: .PP .Vb 1 \& .Ve .PP Note that the original parser of HTML::Template::Expr is currently (0.04) rather simple, so if you need backward compatibility all compound expressions must be parenthesized. .PP Backward compatible examples: .PP .Vb 1 \& \& \& .Ve .PP Nevertheless, in HTML::Template::Pro, you can safely write things like .PP .Vb 1 \& .Ve .PP with proper priority of operations. .PP Pattern in a regular expression must be enclosed with "/": .PP .Vb 1 \& .Ve .SH COMPARISON .IX Header "COMPARISON" Here's a list of supported comparison operators: .IP \(bu 4 Numeric Comparisons .RS 4 .IP \(bu 4 < .IP \(bu 4 > .IP \(bu 4 == .IP \(bu 4 != .IP \(bu 4 >= .IP \(bu 4 <= .IP \(bu 4 <=> .RE .RS 4 .RE .IP \(bu 4 String Comparisons .RS 4 .IP \(bu 4 gt .IP \(bu 4 lt .IP \(bu 4 eq .IP \(bu 4 ne .IP \(bu 4 ge .IP \(bu 4 le .IP \(bu 4 cmp .RE .RS 4 .RE .SH MATHEMATICS .IX Header "MATHEMATICS" The basic operators are supported: .IP \(bu 4 + .IP \(bu 4 \&\- .IP \(bu 4 * .IP \(bu 4 / .IP \(bu 4 % .IP \(bu 4 ^ (not supported in HTML::Template::Expr) .PP There are also some mathy functions. See the FUNCTIONS section below. .SH LOGIC .IX Header "LOGIC" Boolean logic is available: .IP \(bu 4 && (synonym: and) .IP \(bu 4 || (synonym: or) .SH "REGULAR EXPRESSION SUPPORT" .IX Header "REGULAR EXPRESSION SUPPORT" regexp support is added to HTML::Template::Expr and HTML::Template::Pro by Stanislav Yadykin . Currently it is not included in official distribution of HTML::Template::Expr. .PP Standard regexp syntax: .IP \(bu 4 =~ .IP \(bu 4 !~ .SH FUNCTIONS .IX Header "FUNCTIONS" The following functions are available to be used in expressions. See perldoc perlfunc for details. .IP \(bu 4 sprintf .IP \(bu 4 substr (2 and 3 arg versions only) .IP \(bu 4 lc .IP \(bu 4 lcfirst .IP \(bu 4 uc .IP \(bu 4 ucfirst .IP \(bu 4 length .IP \(bu 4 defined .IP \(bu 4 abs .IP \(bu 4 atan2 .IP \(bu 4 cos .IP \(bu 4 exp .IP \(bu 4 hex .IP \(bu 4 int .IP \(bu 4 log .IP \(bu 4 oct .IP \(bu 4 rand .IP \(bu 4 sin .IP \(bu 4 sqrt .IP \(bu 4 srand .PP All functions must be called using full parenthesis. For example, this is a syntax error: .PP .Vb 1 \& .Ve .PP But this is good: .PP .Vb 1 \& .Ve .SH "EXPR: DEFINING NEW FUNCTIONS" .IX Header "EXPR: DEFINING NEW FUNCTIONS" You may also define functions of your own. See HTML::Template::PerlInterface for details. .SH AUTHOR .IX Header "AUTHOR" Sam Tregar, sam@tregar.com (Main text) .PP I. Vlasenko, (Pecularities of HTML::Template::Pro) .SH LICENSE .IX Header "LICENSE" .Vb 2 \& HTML::Template : A module for using HTML Templates with Perl \& Copyright (C) 2000\-2008 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 2, 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\*(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