HTML::SimpleTemplate - Quick and simple template library for Perl


  use HTML::SimpleTemplate;

  # A template object referencing a particular directory
  my $adminpages=new HTML::SimpleTemplate("/templates/admin");

  # A template object referencing files in this directory
  my $localpages=new HTML::SimpleTemplate;

  # Display this template file, passing no parameters

  # Display this template file, passing the %account hash

  # Display this template file, with a-la-carte parameter
  $localpages->Display("order.tpl",{product=>"Perl mug"});




The Display method parses the specified file according to the rules specified below. It can optionally be passed a second parameter, a reference to a hash, containing the variables used in the template.

When the SimpleTemplate object is created a template path can optionally be specified. If this is absent filenames specified to Display are relative to the current directory.


        Hello $name!
        ?($name eq "George")
        ?$rude old fool!
        It's too late at night to think of witty examples!

A template is a text file which may contain certain variable references and special commands. Variables are referenced by prefixing their name with the dollar symbol, as in Perl itself. Variable names must be alphanumeric, and must be followed by a non-alphanumeric character (including spaces and newlines) so the module knows where the name ends.

There area very small number of special commands, which all involve either a ? or an ! in the first column of the line.


        The variable evaluated as true
        No it didn't

        ?($Any=~/perl evaluation that doesn't contain brackets/)
        It was
        It wasn't (else is optional by the way!)

You may nest conditionals to your hearts content, but the layout makes it confusing if you have more than two or three. Anyway that kind of complex logic belongs in Perl, not in HTML.





When you include another template all of the variables in your current template remain available.



Yes, you can call Perl inside your template. You can, of course, then call templates from inside that Perl. Very powerful, potentially very confusing...


Saving the best for last, you can now put sensible comments in your HTML - and the page viewer doesn't get to see it!

        # This section for stupid people only
        It appears you have failed to enter your name.
        # You freakin' numbskull!


Clearly you cannot embed conditionals, callbacks etc in the middle of a line. You also can't have any plain content following a command. However, for HTML, which this is primarily designed for, that just isn't an issue.

By the way, if you're creating an HTML page, it works quite nicely to put that Content-type: text/html in the template if it makes you happy having it there.


A Crawford,