Example 3
This example makes use of Fragments and IF conditions.
The Master/Fragment declaration lets you insert repeating elements into various content pages, such as headers, footers, menus and the likes. Very useful, this one. Program File # 1: <?php 
# 2:
require_once("../stp.inc");
# 3:
$STP = new parser2;
# 4:
# 5:
$params['TITLE'] = "Example 3";
# 6:
$params['HEADLINE'] = "My address book";
# 7:
# 8:
$params['ENTRIES'] = array(
# 9:   array(
"Name"=>"John",
#10:         
"Phone"=>"28877112"),
#11:   array(
"Name"=>"Jill",
#12:         
"Phone"=>"4785521"),
#13:   array(
"Name"=>"My dog",
#14:         
"Phone"=>"04487 445512")
#15:   );
#16:
#17:
$frame "example3.tmpl";
#18:
$frags['Header'] = "top.tmpl";
#19:
$frags['Footer'] = "bottom.tmpl";
#20:
$STP->setParams($params);
#21:
$STP->Assemble($frame$frags);
#22:
$STP->parseAndEcho();
#23:
?>
HTML template file # 0: <?
# 1:
<!--INSERT_Header-->
# 2:   <
h1 align="center"><#HEADLINE#></h1>
# 3:   
<center>
# 4:   <
table border="1" cellpadding="3" cellspacing="3">
# 5:     <
tr>
# 6:       <
th align="left">Name</th>
# 7:       <
th align="left">Phone</th>
# 8:     </
tr>
# 9:   <
#FOR ENTRIES#>
#10:   
<#IF Name (ne 'My dog')#>
#11:     
<tr>
#12:       <
td><#Name#></td>
#13:       
<td><#Phone#></td>
#14:     
</tr>
#15:   <
#/IF Name#>
#16:   
<#/FOR ENTRIES#>
#17:   
</table>
#18:   </
center>
#19: <!--
INSERT_Footer-->
Content of top.tmpl #0: <?
#1:
<html>
#2:   <
head>
#3:   <
title><#TITLE#></title>
#4:   
</head>
#5:   <
body>
Content of bottom.tmpl #0: <?
#2:     
</body>
#3: </
html>
Our php file is pretty much the same as in example 2 with some exceptions.
This here will define the master template.
$frame = "example3.tmpl";
Additionally we define two fragments:
$frags['Header'] = "top.tmpl";
$frags['Footer'] = "bottom.tmpl";
To deliver these to the parser we do not use setTemplate.
Instead, we use
$STP->Assemble($frame, $frags);
This time, the more interesting part is in the HTML templates.
The master template starts like this
<!--INSERT_Header-->
This will look for the element called "Header" in the $frag array and if found insert the file defined in exactly this space. In our loop from example 2 now we have a condition:
  <#IF Name (ne 'My dog')#>
    <tr>
      <td><#Name#></td>
      <td><#Phone#></td>
    </tr>
  <#/IF Name#>
This means that the whole table row will be sent to the browser only if the value "Name" is not "My dog". On the bottom of the page we insert the page footer the same way we did with the head:
<!--INSERT_Footer-->

See the result

Note:
Fragments will be parsed for tags the same way as the master template.

Another Note:
Unfortunately it is currently not possible to INSERT a fragment into another fragment.

My Sites