Author image Masatoshi Mizuno
and 1 contributors


HTML::Template::Ex - The Perl code is operated in the template for HTML::Template.


  package MyProject;
  use CGI;
  use Jcode;
  use HTML::Template::Ex;
  my $cgi = CGI->new;
  my $self= bless { cgi=> cgi }, __PACKAGE__;
  my $template= <<END_OF_TEMPLATE;
  <head><title><tmpl_var name="title"></title></head>
  <tmpl_set name="title" value="HTML::Template::Ex">
  <h1><tmpl_var name="page_title"></h1>
  <h2><tmpl_var name="title"></h2>
  <div style="margin:10; background:#DDD;">
    my($self, $param)= @_;
    $param->{page_title}= 'My Page Title';
    return $self->{cgi}->param('name') || 'It doesn't receive it.';
  <div style="margin:10; background:#DDD;">
  <tmpl_loop name="users">
   <tmpl_var name="u_name" escape="html">
   : <tmpl_var name="email" escape="html">
  <tmpl_ex name="users">
    return [
     { u_name=> 'foo', email=> 'foo@mydomain'    },
     { u_name=> 'boo', email=> 'boo@localdomain' },
  <tmpl_var name="env_remote_addr">
  my $tmpl= HTML::Template::Ex->new($self, {
    setup_env=> 1,
    scalarref=> \$template,
    encoder  => sub { Jcode->new($_[0])->euc },
    # ... other 'HTML::Template' options.
  print STDOUT $cgi->header, $tmpl->output;


This module offers the function to evaluate the Perl code to the template that HTML::Template uses.

The character string enclosed with '<TMPL_EX> ... </TMPL_EX>' is evaluated as Perl code.

    my($self, $param)= @_;
    my $hoge= $self->to_method;
    $param->{hoge}= $hoge;
    return "";

The object passed to the constructor is passed to the first argument to the tmpl_ex tag.

The second argument is HASH reference for the parameter that HTML::Template uses.

When the ending value of each tmpl_ex block is returned, the value is buried under the position. Therefore, it is necessary to return the dead blank character to bury anything.

When HTML::Template::Ex evaluates a little code, there is a little habit when a complex thing is done though it is convenient.

One is the priority level when two or more tmpl_ex blocks are described.

There is no problem for one template. It is sequentially evaluated on. And, please pay attention to the point done earlier than HTML::Template evaluates tag about this evaluation.

The problem reads other templates from the template. It is time when it exists also in the read template the tmpl_ex block. HTML::Template::Ex is not intervened for the include of the template. Therefore, after HTML::Template evaluates tag, the tmpl_ex block include ahead will be evaluated. This sometimes causes confusion.

  <tmpl_include name="first.tmpl">  ... [3]
   .... [ 1 ]
  <tmpl_include name="middle.tmpl"> ... [4]
   .... [ 2 ]
  <tmpl_include name="end.tmpl">    ... [5]

And, the error message is a very difficult secondarily thing.

As for each tmpl_ex block, the code is individually evaluated with eval. Therefore, it is not a translation processed while looking about the entire template. Therefore, only the error where eval originates is obtained. As for this, specific in the error generation part becomes very difficult.

The thing of this problem solved only by HTML::Template::Ex is difficult. Therefore, the improvement is not scheduled in the future.

Using HTML::Template::Ex on the assumption of the thing to write a complex code doesn't come recommended though it is regrettable. Intuition is compelled every time the error occurs and debugging is compelled to reliance. Perhaps, this will be annoyed by the stress.

Still, I think that it can do the template that works more high-speed than HTML::Mason and Template ToolKit if the code can be completed. Please try and look at interesting one.

Therefore, HTML::Template::Ex selects it by the rental server etc. of the template driver. We will recommend the usage not to expect many of the template driver in the situation in which the leg is limited.

And, '<TMPL_IF >' doesn't have the meaning because it is evaluated earlier than HTML::Template though it is a thing misunderstood easily at the end.

  <tmpl_if name="hoge">

This is smoothly evaluated to diverge to both of the tmpl_ex block. Please solve 'IF' related to tmpl_ex block in tmpl_ex block.

    my($self, $param)= @_;
    if ($apram->{hoge}) {
    } else {


It is enhancing tag that introduces here added by using HTML::Template::Ex. Please see the document of HTML::Template about standard tag of HTML::Template.


The Perl code is evaluated in the template.

Please see DESCRIPTION for details concerning basic operation.

When the NAME attribute is given, the value that the tmpl_ex block returned to the parameter of the name is substituted. Please give the HIDDEN attribute at the same time to prevent the value being buried under the position of the tmpl_ex block.

  <h1><tmpl_var name="hoge"></h1>
  <tmpl_ex name="hoge" hidden="1">
    my($self)= @_;
    ..... ban, bo, bon.
    return $self->request->param('Fooo');

The ESCAPE attribute can be used.

  <tmpl_ex escape="html">
    ..... ban, bo, bon.
    return "<font>Zooooo</font>";

* After it is escaped of html, this is buried.

<TMPL_SET NAME='...' VALUE='...'>

The value is set in the parameter in the template.

<tmpl_var name='env_*[ Environment variable name. ]'>

When the setup_env option is given to the constructor, the environment variable is set up. It can be referred to by '<TMPL_VAR NAME=...>'. Please specify the environment variable name putting up 'env_' to the head of the name.

  <tmpl_var name="env_remote_addr">
  <tmpl_var name="env_path_info">


new ([OBJECT], [OPTION])

It is a constructor.

An arbitrary object is given to the first argument. The given object is passed as the first argument of each tmpl_ex block.

[OPTION] is an option to pass to HTML::Template. Please include the option of HTML::Template::Ex here.


  • setup_env

    It is set up to refer to the environment variable.

  • exec_off

    All the tag that HTML::Template::Ex evaluates is invalidated and it puts it out.

  • no_strict_exec

    This is turned off though the code of the tmpl_ex block is evaluated under the strict environment usually. For person who is not accustomed to making strict code.

  • encoder

    The CODE reference to process the character-code can be defined.

  • auto_encoder

    When charset was able to be acquired from the template, encoder is processed.


When charset was able to be acquired from the template, the value is returned.

initStyle ([STYLE])

The output style etc. when the error occurs are defined.


Contents are output.


Please refer to the document of HTML::Template for other methods.


There is causing the defective operation according to the kind of the cash used by HTML::Template option.

* If it is 'cache' option to specify at mod_perl, it operates normally usually.




Masatoshi Mizuno <>


Copyright (C) 2007 by Bee Flag, Corp. <>, All Rights Reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.