Expr(3pm)
π Expr(3pm) β HTML::Template::Expr π
okay bestie, so this is Expr(3pm), part of the User Contributed Perl Documentation family. it's the little extension that lets your HTML::Template templates do math and logic and generally have a whole personality. let's get into it π¦
β¨ NAME β¨
HTML::Template::Expr β it's an HTML::Template extension that adds expression support. that's the whole gig. π
π SYNOPSIS π
use HTML::Template::Expr;
my $template = HTML::Template::Expr->new(filename => 'foo.tmpl');
$template->param(banana_count => 10);
print $template->output();
look at that. clean. iconic. πΈ
π DESCRIPTION π
so basically this module is an extension to HTML::Template that lets you write expressions right inside the template syntax. and the tea is: it's purely an addition β every single normal HTML::Template option, syntax and behavior still works exactly like it always did. see HTML::Template for the deets.
what does expression support actually get you? comparisons, math operations, string operations, plus a whole mechanism where you can add your own functions at runtime. the basic syntax is giving:
<TMPL_IF EXPR="banana_count > 10">
I've got a lot of bananas.
</TMPL_IF>
that's gonna output I've got a lot of bananas if you call:
$template->param(banana_count => 100);
in your script. and yes, <TMPL_VAR>s work with expressions too, bestie:
I'd like to have <TMPL_VAR EXPR="banana_count * 2"> bananas.
with that same param() call as above, this outputs I'd like to have 200 bananas. π obsessed.
π MOTIVATION π
some of you may be wondering if the author got replaced by a pod person. for the record: he still thinks this sort of thing should be avoided. HOWEVER β there are some situations where letting the template author have a lil programmatic leeway is genuinely invaluable.
if you don't like it, don't use this module. keep using plain ol' HTML::Template β he knows he will! but if you find yourself needing a little programming in your template, for whatever reason, then this module may just save you from HTML::Mason. slay. π
π BASIC SYNTAX π
Variables are unquoted alphanumeric strings with the same restrictions as variable names in HTML::Template. their values get set through param(), just like normal HTML::Template variables. for example, these two lines are equivalent:
<TMPL_VAR EXPR="foo">
<TMPL_VAR NAME="foo">
Numbers are unquoted strings of numbers and may have a single . to indicate a floating point number. like:
<TMPL_VAR EXPR="10 + 20.5">
String constants must be enclosed in quotes, single or double. like:
<TMPL_VAR EXPR="sprintf('%d', foo)">
you can string together operators to produce complex booleans:
<TMPL_IF EXPR="(foo || bar || baz || (bif && bing) || (bananas > 10))">
I'm in a complex situation.
</TMPL_IF>
β οΈ the parser is pretty simple, so you may need to use parenthesis to get the precedence you want. no cap.
β COMPARISON β
here's the list of supported comparison operators, bestie:
- Numeric Comparisons
<>==!=>=<=<=>
- String Comparisons
gtlteqnegelecmp
π₯ MATHEMATICS π₯
the basic operators are supported:
+-*/%
there are also some mathy functions β see the FUNCTIONS section below. π
π§ LOGIC π§
boolean logic is available:
&&(synonym:and)||(synonym:or)
π FUNCTIONS π
the following functions are available to be used in expressions. see perldoc perlfunc for details.
sprintfsubstr(2 and 3 arg versions only)lclcfirstucucfirstlengthdefinedabsatan2cosexphexintlogoctrandsinsqrtsrand
π¨ ALL functions must be called using full parenthesis. so for example, this is a syntax error:<TMPL_IF expr="defined foo">but this is good:<TMPL_IF expr="defined(foo)">
π«Ά DEFINING NEW FUNCTIONS π«Ά
to define a new function, pass a functions option to new:
$t = HTML::Template::Expr->new(filename => 'foo.tmpl',
functions =>
{ func_name => \&func_handler });
or, you can use the register_function class method to register the function globally:
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 his teeth hurt. (his words, not mine.)
π MOD_PERL TIP π
the 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::Expr;
HTML::Template::Expr->register_function(foozicate => sub { ... });
HTML::Template::Expr->register_function(barify => sub { ... });
HTML::Template::Expr->register_function(baznate => sub { ... });
you might also want to pre-compile some commonly used templates and cache them. see HTML::Template's FAQ for instructions. β¨
π CAVEATS π
okay this is the part where we get real:
- currently the module forces the HTML::Template
global_varsoption to be set. this will hopefully go away in a future version, so if you needglobal_varsin your templates then you should set it explicitly. - the module won't work with HTML::Template's
file_cacheorshared_cachemodes, but normal memory caching should work. he hopes to address this in a future version. - the module is inefficient, both in parsing and evaluation. he'll be working on this for future versions and patches are always welcome. π«Ά
π BUGS π
he is aware of no bugs β if you find one, join the mailing list and tell them about it. you can join the HTML::Template mailing-list by visiting:
http://lists.sourceforge.net/lists/listinfo/html-template-users
of course, you can still email him directly (sam@tregar.com) with bugs, but he reserves the right to forward bug reports to the mailing list. π
π¨ 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!
πΈ CREDITS πΈ
the following people have generously submitted bug reports, patches and ideas:
Peter Leonard
Tatsuhiko Miyagawa
Don Brodale
thanks! π«Ά
π AUTHOR π
Sam Tregar <sam@tregar.com>
π LICENSE π
HTML::Template::Expr : HTML::Template extension adding expression support
Copyright (C) 2001 Sam Tregar (sam@tregar.com)
this module is free software; you can redistribute it and/or modify it under the terms of either:
- the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or
- 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, he'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
and that's the whole page bestie!! πβ¨ this was rendered by perl v5.32.0 on 2021-01-05, and yeah it's Expr(3pm) β go forth and put expressions in your templates, but like, responsibly. mwah π
π¦ where this page comes from
- debian bookworm: libhtml-template-expr-perl 0.07-3.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libhtml-template-expr-perl 0.07-3.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libhtml-template-expr-perl 0.07-3.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhtml-template-expr-perl 0.07-3.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libhtml-template-expr-perl 0.07-3.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libhtml-template-expr-perl 0.07-3.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms