Ever since my ids-media days, I have always worked with a web development team. There are always two major parts of the web dev team I have worked with, Front-end (designers) and the back-end (programmers). The most common hurdle that we encounter is how to be able separate the presentation layer with the logic layer. This is simply illustrated with the code below.
<table border=”0″ cellpadding=”2″ cellspacing=”2″>
<?php
while($result = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $result["name"];?></td>
<td><?php echo $result["address"];?></td>
</tr>
<?php } ?>
</table>
With the example above the backend programming with php is mixed up with the html tags which are used to present the data. The problem with this when working in a team is that the backend programmer will have to deal with the presentation layer while doing his job and when the design has to be changed or modified the designer will have to deal with the backend side thus forfeiting the idea of having a design focused developer and a backend focused programmer. This will result to have the two to compromise in order for their work to be harmonious.
In an effort to find solution for this hurdle, I tried to develop a sensible templating system that would separate the html front end from the php files. I was however stopped when I discovered the PEAR ITX Templating system. Unlike other templating systems that are beefed up with so many features that as a programmer I have no intention of using, the ITX templating system is as simple as it can be.
To start of with PEAR ITX templating system you need to have PEAR installed with your php. For info on how to do this, visit http://www.php.net.
You could also include these files to your code”
IT.PHP
IT_ERROR.PHP
ITX.PHP
Let us start with the html side”. Create a html file named index.html
<html>
<head><title>IT TEMPLATE SAMPLE</title></head>
<body>
{var_message}
</body>
</html>
* note that we inserted a token inside the body. a token is inclosed with the { } brackets. In this example we have the var_message token.
Now let us move on to the php file. Create a php file and name it as index.php.
<?php
include(“IT.php”);
$tpl = new HTML_Template_IT(“./”);
$tpl->loadTemplatefile(“index.html”);
$tpl->setVariable(“var_message “,”Hello World”);
$tpl->show();
?>
What we did here was
1. first we included IT.PHP
2. Then we initiated the template object and named it as $tpl.
$tpl = new HTML_Template_IT(“./”);
the argument for the HTML_Template_IT is “./” which means the current directory” you could put your html files on a different location and would just have to specify through this argument the location of the html file.
3. after which we loaded the Template file which is the html file we created a while ago.
$tpl->loadTemplatefile(“index.html”);
4. we then replaced the token on the template file with our desired content.
$tpl->setVariable(“var_message “,”Hello World”);
5. Lastly, we parsed the tpl object.
$tpl->show();
There you have it. when you point your browser to index.php it will display Hello World. This is the basic way of using the ITX system. ITX system also enables you to process tokens by blocks which will let you show data on a tabular format.
Let me take you back to this.
<table border=”0″ cellpadding=”2″ cellspacing=”2″>
<?php
<while $result = mysql_fetch_array($query)>
{
?>
<tr>
<td><?php echo $result["name"];?></td>
<td><?php echo $result["address"];?></td>
</tr>
<?php } ?>
</table>
let us modify this by elimating the php code and inserting tokens and a block
<table border=”0″ cellpadding=”2″ cellspacing=”2″>
<!– BEGIN row –>
<tr>
<td></td>
<td></td>
</tr>
<!– END row –>
</table>
now to process this on the php side,
while($result = mysql_fetch_array($query))
{
$tpl->setCurrentBlock(“row”);
$tpl->setVariable(“var_name”,$result["name"]);
$tpl->setVariable(“var_address”,$result["address"]);
$tpl->parseCurrentBlock(“row”);
}
That is how simple it is to process tabular data through ITX. You just have to set the template directory and the load the template file as we did a while ago then
set the current block
$tpl->setCurrentBlock(“row”);
replace the tokens inside that block with your desired output.
$tpl->setVariable(“var_name”,$result["name"]);
$tpl->setVariable(“var_address”,$result["address"]);
then parse the current block
$tpl->parseCurrentBlock(“row”);
you will just have to parse the entire template with
$tpl->show();
and your good to go!
As you can see clearly, with the PEAR ITX Templating system the presentation layer(html) is separated with the backend php file. This will enable your designer to design the page at will and the programmer to do his thing uninterrupted with the design hurdle. This website uses PEAR ITX TEMPLATING SYSTEM.