HTML::Template::Expr - HTML::Template extension adding expression support


  use HTML::Template::Expr;
  my $template = HTML::Template::Expr->new(filename => 'foo.tmpl');
  $template->param(banana_count => 10);
  print $template->output();


This module provides an extension to HTML::Template which allows expressions in the template syntax. This is purely an addition - all the normal HTML::Template options, syntax and behaviors will still work. See the HTML::Template manpage for details.

Expression support includes comparisons, math operations, string operations and a mechanism to allow you add your own functions at runtime. The basic syntax is:

   <TMPL_IF EXPR="banana_count > 10">
     I've got a lot of bananas.

This will output ``I've got a lot of bananas'' if you call:

   $template->param(banana_count => 100);

In your script. <TMPL_VAR>s also work with expressions:

   I'd like to have <TMPL_VAR EXPR="banana_count * 2"> bananas.

This will output ``I'd like to have 200 bananas.'' with the same param() call as above.


Some of you may wonder if I've been replaced by a pod person. Just for the record, I still think this sort of thing should be avoided. However, I realize that there are some situations where allowing the template author some programatic leeway can be invaluable.

If you don't like it, don't use this module. Keep using plain ol' HTML::Template - I know I will! However, if you find yourself needing a little programming in your template, for whatever reason, then this module may just save you from HTML::Mason.


Variables are unquoted alphanumeric strings with the same restrictions as variable names in HTML::Template. Their values are 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. For example:

   <TMPL_VAR EXPR="10 + 20.5">

String constants must be enclosed in quotes, single or double. For example:

   <TMPL_VAR EXPR="sprintf('%d', foo)">

The parser is currently rather simple, so all compound expressions must be parenthesized. Examples:

   <TMPL_VAR EXPR="(10 + foo) / bar">
   <TMPL_IF EXPR="(foo % 10) > (bar + 1)">

If you don't like this rule please feel free to contribute a patch to improve the parser's grammar.


Here's a list of supported comparison operators:


The basic operators are supported:

There are also some mathy functions. See the FUNCTIONS section below.


Boolean logic is available:


The following functions are available to be used in expressions. See perldoc perlfunc for details.

All functions must be called using full parenthesis. For example, this is a syntax error:

   <TMPL_IF expr="defined foo">

But this is good:

   <TMPL_IF expr="defined(foo)">


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 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 recieve 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 class method can be called in mod_perl's to define widely used common functions to HTML::Template::Expr. Add something like this to your

  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 the HTML::Template manpage's FAQ for instructions.


Currently the module forces the HTML::Template global_vars option to be set. This will hopefully go away in a future version, so if you need global_vars in your templates then you should set it explicitely.

The module won't work with HTML::Template's file_cache or shared_cache modes, but normal memory caching should work. I hope to address this is a future version.

The module is inefficient, both in parsing and evaluation. I'll be working on this for future versions and patches are always welcome.


When you find a bug, join the mailing list and tell us about it ( You can join the HTML::Template mailing-list by sending a blank email to Of course, you can still email me directly ( with bugs, but I reserve 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!


The following people have generously submitted bug reports, patches and ideas:

   Peter Leonard
   Tatsuhiko Miyagawa



Sam Tregar <>


HTML::Template::Expr : HTML::Template extension adding expression support

Copyright (C) 2001 Sam Tregar (

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