LevSelector.com New York
home > Perl CGI::Application module

Perl HTML::Template module
- www.perldoc.com/cpan/HTML/Template.html
 
intro home - top of the page -

HTML::Template is one of ~ 30 template modules on CPAN. It really helps to cut difficulty of designing and maintaining of sophisticated corporate projects. It enforces an important divide - design and programming.

In web design there are 2 processes which are usually done by different groups of people:
   - Design of server-side processing scripts - done by programmers
   - Design of HTML/Graphics front-end - done by designers

There are 2 common approaches to bridge these 2 activites:
  1. Include html into script. Print html from script. Problem - can't use WYSIWYG
  2. Include script into html (A la ASP, PHP, JSP).

Both of those approaches create problems with maintenance.  For example, if designer changed the look - programmer now have to manually update html embeded into script, or check and correct script embeded into html.

HTML::Template tries to decrease the friction between programmers and designers by using templates - but minimizing code-related entries in HTML (or design-related parts in the code). Only few special tags are allowed to be inserted into html.
  - Programmer uses very limited set of tags and CAN NOT embed actual programming code into html.
  - Designer can use Dreamweaver or other WYSIWYG development tool or HTML validator - the embeded tags will stay intact. He is instructed to watch for those few tags - which is easy, because there are just few of them - and they are simple.

In practice with HTML::Template, a designer can frequently update templates with out the need for a programmer to debug the code.

There are only 5 types of tags:

1. <TMPL_VAR NAME="PARAMETER_NAME">
2. <TMPL_LOOP NAME="LOOP_NAME"> </TMPL_LOOP>
3. <TMPL_INCLUDE NAME="filename.tmpl">
4. <TMPL_IF NAME="CONTROL_PARAMETER_NAME"> </TMPL_IF>  <TMPL_ELSE>
5. <TMPL_UNLESS NAME="CONTROL_PARAMETER_NAME"> </TMPL_UNLESS>

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.

Example, test.tmpl:
  <html><head>
<title>Test Template</title>
<head><body>
My Home Directory is
<TMPL_VAR NAME=HOME>
<p>
My Path is set to
<TMPL_VAR NAME=PATH>
</body><html>

Now create a small CGI program:
use HTML::Template;
my $template = HTML::Template->new(filename => 'test.tmpl');
  # fill in some parameters
$template->param(
      HOME => $ENV{HOME},
      PATH => $ENV{PATH},
  );
print "Content-Type: text/html\n\n";
print $template->output;