The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

HTMLTMPL - Merges runtime data with static HTML template file.

SYNOPSIS

How to merge data with a template.

The template :

 <html><head><title>parser Example 1</title></head>
 <body bgcolor=beige>
 My name is __firstname__ __surname__ but my friends call me __nickname__.
 <hr>
 </body>
 </html>

The code :

 use HTMLTMPL;

 # Create a template object and load the template source.
 $templ = new HTMLTMPL;
 $templ->src('example1.html');

 # Set values for tokens within the page
 $templ->surname('Smyth');
 $templ->firstname('Arthur');
 $templ->nickname('Art!');

 # Send the merged page and data to the web server as a standard text/html mime
 #   type document
 $templ->output('Content-Type: text/html');

Produces this output :

 <html><head><title>parser Example 1</title></head>
 <body bgcolor=beige>
 My name is Arthur Smyth but my friends call me Art!.
 <hr>
 </body>
 </html>

DESCRIPTION

In an ideal web system, the HTML used to build a web page would be kept distinct from the application logic populating the web page. This module tries to achieve this by taking over the chore of merging runtime data with a static html template.

The HTMLTMPL module can address the following template scenarios :

  • Single values assigned to tokens

  • Multiple values assigned to tokens (as in html table rows)

  • Single pages built from multiple templates (ie: header, footer, body)

  • html tables with runtime determined number of columns

An html template consists of 2 parts; the boilerplate and the tokens (place holders) where the variable data will sit.

A token has the format __tokenName__ and can be placed anywhere within the template file. If it occurs in more than one location, when the data is merged with the template, all occurences of the token will be replaced.

 <p>
 My name is __userName__ and I am aged __age__.
 My friends often call me __nickName__ although my name is __userName__.

When an html table is being populated, it will be necessary to output several values for each token. This will result in multiple rows in the table. However, this will only work if the tokens appear within a repeating block.

To mark a section of the template as repeating, it needs to be enclosed within a matching pair of repeating block tokens. These have the format __x_blockName__. They must always come in pairs.

 and I have the following friends
 <table>
 __x_friends__
 <tr>
     <td>__friendName__</td><td>__friendNickName__</td>
 </tr>
 __x_friends__
 </table>

METHODS

src($)

The single parameter specifies the name of the template file to use.

srcString($)

If the template is within a string rather than a file, use this method to populate the template object.

output(@)

Merges the data already passed to the HTMLTMPL instance with the template file specified in src(). The optional parameter is output first, followed by a blank line. These form the HTTP headers.

htmlString()

Returns a string of html produced by merging the data passed to the HTMLTMPL instance with the template specified in the src() method. No http headers are sent to the output string.

listAllTokens()

Returns an array ref. The array contains the names of all tokens found within the template specifed to src() method.

getAlltokens()

If called in a scalar context, returns a comma seperated list of tokens found within the template. If called in array context, returns array of tokens found within the template.

dumpAll()

Sends to stdout a web page containing an html table. This table lists all tokens found in the src() template, and all values currently assigned to the tokens.

getBlock($)

Returns an HTMLTMPL object which represents the repeating / invisible block of html named in the parameter.

tokenName($)

Assigns to the 'tokenName' token the value specified as parameter.

tokenName($$)

Assigns to the 'tokenName' token, within the repeating block specified in 2nd parameter, the value specified as the first parameter.

EXAMPLES

Example 1.

A simple template with single values assigned to each token.

The template :

 <html><head><title>parser Example 1</title></head>
 <body bgcolor=beige>
 My name is __firstname__ __surname__ but my friends call me __nickname__.
 <hr>
 </body>
 </html>

The code :

 use HTMLTMPL;

 # Create a template object and load the template source.
 $templ = new HTMLTMPL;
 $templ->src('example1.html');

 # Set values for tokens within the page
 $templ->surname('Smyth');
 $templ->firstname('Arthur');
 $templ->nickname('Art!');

 # Send the merged page and data to the web server as a standard text/html mime
 #   type document
 $templ->output('Content-Type: text/html');

Produces this output :

 <html><head><title>parser Example 1</title></head>
 <body bgcolor=beige>
 My name is Arthur Smyth but my friends call me Art!.
 <hr>
 </body>
 </html>

Example 2

Produces an html table with a variable number of rows.

The template :

 <html><head><title>Example 2 - blocks</title></head>
 <body bgcolor=beige>
 <table border=1>
 __x_details__
 <tr>
        <td>__id__</td>
        <td>__name__</td>
        <td>__desc__</td>
 </tr>
 __x_details__
 </table>
 <ul>
 __x_customer_det__
        <li>__customer__</li>
 __x_customer_det__
 </ul>
 <br>
 <hr>
 </body>
 </html>

The code :

 use HTMLTMPL;

 # Create the template object and load it.
 $templ = new HTMLTMPL;
 $templ->src('example2.html');

 # Simulate obtaining data from database, etc and populate 300 blocks.

 for ($i=0; $i<300; $i++)
 {
     # Ensure that the token is qualified by the name of the block and load
     #       values for the tokens.
     $templ->id($i, 'x_details');
     $templ->name("the name is $i", 'x_details');
     $templ->desc("the desc for $i", 'x_details');
 }

 for ($i=0; $i<4; $i++)
 {
     $templ->customer("And more $i", 'x_customer_det');
 }

 #    Send the completed html document to the web server.
 $templ->output('Content-Type: text/html');

Example 5.

Uses 2 seperate templates to produce a single web page :

The overall page template :

 <html>
 <head><title>Example 5 - sub templates</title></head>
 <body bgcolor=blue>

 Surname : __surname__
 First Name : __firstname__
 My friends (both of them) call me : __nickname__

 Now to include a sub template...
 __guts__

 And this is the end of the outer template.
 <hr>
 </body>
 </html>

The subtemplate which will be slotted into the 'guts' token position :

 <table border=1>
 <tr>
     <td>__widget__</td>
     <td>__wodget__</td>
 </tr>
 </table>

The code :

 use HTMLTMPL;

 # Create a template object and load the template source.
 my($templ) = new HTMLTMPL;
 $templ->src('example5.html');


 # Set values for tokens within the page
 $templ->surname('Smyth');
 $templ->firstname('Arthur');
 $templ->nickname('Art!');

 my $subTmpl = new HTMLTMPL;
 $subTmpl->src('example5a.html');
 $subTmpl->widget('this is widget');
 $subTmpl->wodget('this is wodget');

 $templ->guts($subTmpl->htmlString);

 # Send the merged page and data to the web server as a standard text/html mime
 #       type document
 $templ->output('Content-Type: text/html');

Example 6.

In this example the number of columns in the html table is not known until runtime. It uses an 'invisible' block (obtained by calling getBlock() ) to produce a single column <td></td> pair. Multiple values can then be assigned to this to produce multiple columns.

The template :

 <html>
 <head><title>Example 6 - variable number of table cols</title></head>
 <body>
 A table with variable number of columns.

 <table border=1>
 __x_row__
 <tr>
     <td colspan=__maxCols__>This is item number : __itemNo__</td>
 </tr>
 <tr>
 __i_col__
     <td>__cell_data__</td>
 __i_col__
 </tr>
 __x_row__
 </table>
 and this is the end.
 </body>
 </html>

The code :

 use HTMLTMPL;

 my $tmpl = new HTMLTMPL;

 $tmpl->src('example6.html');

 foreach my $y (1..3)
 {
     $tmpl->itemNo($y, 'x_row');
     $tmpl->maxCols(3, 'x_row');

     my $row = $tmpl->getBlock('i_col');

     foreach my $x (qw(A B C))
     {
         $row->cell_data("$x - $y");
     }

     $tmpl->i_col($row->htmlString, 'x_row');
 }

 $tmpl->output('Content-Type: text/html');

HISTORY

 March 1999     Version 0.90    beta
 March 2000     Version 1.20    Added invisible template blocks

AUTHOR

Ian Steel. ian@bilstone.co.uk