Example 2
This shows the use of loops and setting several parameters at once.. Program File # 1: <?php 
# 2:
require_once("../stp.inc");
# 3:
$STP = new parser2;
# 4:
# 5:
$params['HEADLINE'] = "Myaddress book";
# 6:
# 7:
$params['ENTRIES'] = array(
# 8:   array(
"Name"=>"John",
# 9:         
"Phone"=>"28877112"),
#10:   array(
"Name"=>"Jill",
#11:         
"Phone"=>"4785521"),
#12:   array(
"Name"=>"My dog",
#13:         
"Phone"=>"04487 445512")
#14:   );
#15:
#16:
$STP->setParams($params);
#17:
$STP->setTemplate("example2.tmpl");
#18:
$STP->parseAndEcho();
#19:
?>
HTML template file # 0: <?
# 1:
<html>
# 2:   <
head>
# 3:   <
title>Example 2</title>
# 4:   </
head>
# 5:   
# 6:   <
body>
# 7:   <
h1 align="center"><#HEADLINE#></h1>
# 8:   
<center>
# 9:   <
table border="1" cellpadding="3" cellspacing="3">
#10:     <
tr>
#11:       <
th align="left">Name</th>
#12:       <
th align="left">Phone</th>
#13:     </
tr>
#14:   <
#FOR ENTRIES#>
#15:     
<tr>
#16:       <
td><#Name#></td>
#17:       
<td><#Phone#></td>
#18:     
</tr>
#19:   <
#/FOR ENTRIES#>
#20:   
</table>
#21:   </
center>
#22:   </
body>
#23: </
html>
Let's leave out what is explained in the first example and start:
This time we define an array holding all the parameters we want to pass.
The first item will be the title for the address book
$params['HEADLINE'] = "Myaddress book";
Next, we make an entry that is a multi dimensional array.
The first level is numerical and goes from 1 to 3 ( or rather 0 to 2).
This holds the second level array that consists of key-value pairs for "Name" and "Phone Number"
$params['ENTRIES'] = array(
  array("Name"=>"John",
        "Phone"=>"28877112"),
  array("Name"=>"Jill",
        "Phone"=>"4785521"),
  array("Name"=>"My dog",
        "Phone"=>"04487 445512")
  );
Now we pass all this to the parser:
$STP->setParams($params);
And output like in the first example.

In our template file example2.tmpl the parser will now find the tag <#HEADLINE#> and fill it with the item called "HEADLINE" from our parameter array.
Now the parser comes across the <#FOR ENTRIES#>. It looks if there is an array called "ENTRIES" and will start looping through it. On every iteration it will replace the values <#Name#> and <#Phone#> with the respective values from our second level arrays.
See the result

Important:
If the parser does not find a parameter that matches a given tag this tag will be stripped.

My Sites