<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>
#!/usr/bin/perl -w
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');
<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>
#!/usr/bin/perl -w
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');
<html><head><title>parser example 3</title></head>
<body bgcolor=beige>
My name is __firstname__,__surname__ and my friends call me __nickname__.
<br><br>
And these are my hobbies:
<table border=1>
__x_hobby__
<tr>
<td>__desc__</td><td>__long_desc__</td>
</tr>
__x_hobby__
</table>
<hr>
</body>
</html>
</pre>
and is used by the following cgi :
#!/usr/bin/perl -w
use HTMLTMPL;
# Create the template object and load it.
$templ = new HTMLTMPL;
$templ->src('example3.html');
$templ->surname('Smyth Andrews');
$templ->firstname('Arthur');
$templ->nickname('Arty');
$templ->desc('Fly fishing', 'x_hobby');
$templ->long_desc('Involves a rod, shotgun and a big net', 'x_hobby');
$templ->desc('Sleeping', 'x_hobby');
$templ->long_desc('Just basically close your eyes and...', 'x_hobby');
$templ->dumpAll;
And produces output like this :
Dump of tokens and values
| Token | Value | |
|---|---|---|
| __firstname__ | Arthur | |
| __surname__ | Smyth Andrews | |
| __nickname__ | Arty | |
| __x_hobby__ | ||
| __long_desc__ | [0] | Involves a rod, shotgun and a big net |
| __desc__ | [0] | Fly fishing |
| __long_desc__ | [1] | Just basically close your eyes and... |
| __desc__ | [1] | Sleeping |