Examples of parser usage


How to use the parser.

The parser removes the need for cgi programs to output any of the html tags which make up an html page.

A cgi using the parser creates a template object and specifies the name of an html source file to be used. It then assigns values to tokens within the template and requests the populated template to be sent to the web server.

Example 1 is a simple example without any repeating data.
Example 2 uses repeating data to demonstrate populating lists, etc.
Example 3 shows how to dump all the tokens (and their values) which the parser has located.
Example 4 produces a form which contains list boxes populated with variable data..
Example 5 Using sub templates / inner templates



Example 1.

(All of the examples can be found in the examples directory of the tar file.)

<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 html template contains 3 tokens (firstname, surname and nickname). Each of these can be assigned values by the cgi and will be replaced by these values when the page is sent to the web server.
The cgi perl used to populate this template is below :

#!/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');

The parser will set the values of the tokens and output the populated page.

Example 2.

This uses a repeating set of data.


<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>

This template has a repeating 'block' of html within it. This allows for multiple values of a token to produce lists, etc within web pages.
See example 4 for a more complex version.

A block is delimited by a pair of tokens which are named as __x_<block id>__ The 'x' is a visual aid to highlight this is multi values. In this example there are 2 repeating blocks (__x_customer_det_ and __x_details_).

The cgi to populate this is :
#!/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');


When a token within a block is being assigned a value, you need to qualify the name of the block to which it belongs.
This example produces a couple of tables, the first contains 300 rows and the other 4.

Example 3.

This uses the 'dumpAll' function to output a table of all the tokens found within the template, and the value(s) which are currently assigned to them.
The template is :
<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

TokenValue
__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