Example 3
In this example we will work with alternating colors, evaluated PHP and constants.
Program File # 1: <?php 
# 2:
require_once("../stp.inc");
# 3:
define("G_HOSTNAME"$_SERVER['HTTP_HOST']);
# 4:
# 5:
$STP = new parser2;
# 6:
# 7:
# 8:
$params['HEADLINE'] = "My address book";
# 9:
#10:
$params['ENTRIES'] = array(
#11:   array(
"Name"=>"John",
#12:         
"Phone"=>"28877112"),
#13:   array(
"Name"=>"Jill",
#14:         
"Phone"=>"4785521"),
#15:   array(
"Name"=>"My dog",
#16:         
"Phone"=>"04487 445512"),
#17:   array(
"Name"=>"My cat",
#18:         
"Phone"=>"04487 445513")
#19:   );
#20:
#21:
$STP->setParams($params);
#22:
$STP->setTemplate("example4.tmpl");
#23:
$STP->parseAndEchoPHP();
#24:
?>
HTML template file # 0: <?
# 1:
<#ATTR cc0000,00cc00#>
# 2:
<html>
# 3:   <
head>
# 4:   <
title>Example 2</title>
# 5:   </
head>
# 6:   
# 7:   <
body>
# 8:   <
h1 align="center"><#HEADLINE#></h1>
# 9:   
<center>
#10:   
Welcome at <#G_HOSTNAME#>! <br>
#11:   
It is <?php echo date("H:i:s" on <?php echo date("Y-m-d"?>
#12:
  <p />
#13:   <table border="1" cellpadding="3" cellspacing="3">
#14:     <tr>
#15:       <th align="left">Name</th>
#16:       <th align="left">Phone</th>
#17:     </tr>
#18:   <#FOR ENTRIES#>
#19:     <tr Bgstyle="color: #<#ATTR#>">
#20:       <td><#Name#></td>
#21:       <td><#Phone#></td>
#22:     </tr>
#23:   <#/FOR ENTRIES#>
#24:   </table>
#25:   </center>
#26:   </body>
#27: </html>
#29: ?>
Again, our php file bases on example 2 ; the differences:
First, we declare the constant G_HOSTNAME and fill it with $_SERVER['HTTP_HOST'].
define("G_HOSTNAME", $_SERVER['HTTP_HOST']);
Then, instead of calling parseAndEcho() we call
$STP->parseAndEchoPHP();
This will evaluate any one-line code in our template. Again, more changes can be found in the HTML template.
To define the colors for the color change we set at the beginning
<#ATTR cc0000,00cc00#>
Now, all we have to do to change the colors is write
<tr Bgcolor="#<#ATTR#>">
into the FOR-loop. Please note the use of
<#G_HOSTNAME#>
that will expand to the name of your server domain

We tell the user what time and date he entered by

It is <?php echo date("H:i:s") ?> on <?php echo date("Y-m-d") ?>
Of course you could also define this in the php file; actually, you are encouraged to write as much as possible in to the php file and not into the template. We wanted to separate code and HTML, right?
However, sometimes there is no way around putting some php into a template.
See the result

Note:
It is currently not possible (and not intended either) to put more than one consecutive line of php code into a template.

My Sites