Home arrow PHP arrow Page 3 - Using a Template Processor Class in PHP 5

Parsing template files: defining the input tags for the “TemplateProcessor” class - PHP

Welcome to part two of the series “Separating logic from presentation.” Comprised of three articles, this series walks you through the development of an extensible template processor in PHP 5, which you might find quite useful for separating the logic of your PHP applications from their visual presentation.

  1. Using a Template Processor Class in PHP 5
  2. Getting started using the “TemplateProcessor” class: a quick look at its definition
  3. Parsing template files: defining the input tags for the “TemplateProcessor” class
  4. Going one step further: seeing the “TemplateProcessor” class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
May 09, 2006

print this article



Right, I think the best way to demonstrate the functionality of my “TemplateProcessor” class is simply by feeding it a considerable variety of data, which will be eventually parsed and displayed within the corresponding template file, after performing the replacement of placeholders. Considering this, I’ll first define two dynamic files, “header.php” and “footer.php” respectively, which will compose the header and footer sections of a sample web page. Take a look at these two basic PHP files:

echo '<h1>This is the header section and was generated at the
following time '.date('H:i.s').'</h1>';

echo '<h1>This is the footer section and was generated at the
following time '.date('H:i.s').'</h1>';

Now, after listing the above PHP files, I’ll include a simple MySQL result set as part of the input of the “TemplateProcessor” class. To do this, I’ll use two additional MySQL wrapping classes, which are listed below:

// define 'MySQL' class
class MySQL{
    private $host;
    private $user;
    private $password;
    private $database;
    private $connId;
    // constructor
    function __construct($options=array()){
            throw new Exception('Connection options must be an
        foreach($options as $option=>$value){
                throw new Exception('Connection parameter cannot
be empty');
    // private 'connectDb()' method
    private function connectDb(){
            throw new Exception('Error connecting to MySQL');
            throw new Exception('Error selecting database');
    // public 'query()' method
    public function query($sql){
            throw new Exception('Error running query '.$sql.'
        return new Result($this,$result);
class Result{
    private $mysql;
    private $result;
    // constructor
    public function __construct($mysql,$result){
    // public 'fetch()' method
    public function fetch(){
        return mysql_fetch_array($this->result,MYSQL_ASSOC);
    // public 'count()' method
    public function count(){
            throw new Exception('Error counting rows');
        return $rows;
    // public 'get_insertId()' method
    public function getInsertId(){
            throw new Exception('Error getting insert ID');
        return $insId;
    // public 'seek()' method
    public function seek($row){
            throw new Exception('Invalid row parameter');
            throw new Exception('Error seeking row');
        return $row;
    // public 'getAffectedRows()' method
    public function getAffectedRows(){
            throw new Exception('Error counting affected rows');
        return $rows;
    // public 'getQueryResource()' method
    public function getQueryResource(){
        return $this->result;

Now that you saw the source code of the two MySQL wrapping classes listed above, which I’ll utilize for fetching a trivial MySQL result set, the next thing to do is define the structure of a sample template file, named “default_template.htm,” that will be parsed in turn by the “TemplateProcessor” class. Please look at the signature of this example template file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
<div id="header">{header}</div>
<div id="navbar">{navbar{subnavigationbar}{subnavigationbar2}}</div>
<div id="leftcol">{leftcontent}</div>
<div id="content">{maincontent}</div>
<div id="rightcol">{rightcontent}</div>
<div id="footer">{footer}</div>

As you can see, the sample template file shown above has some placeholders located in different sections of the web page, which will be removed and replaced with actual data. In this example, both {header} and {footer} placeholders will be replaced with the dynamic output of the “header.php” and “footer.php” files respectively, while {maincontent} will be filled with the MySQL data set that I mentioned before. In addition, the nested {navbar{subnavigationbar1}{subnavigationbar2}} placeholders will be replaced with the contents of the following navigational PHP files:

// definition for 'subnavbar1.php' file
    echo '<a href="file'.$i.'.php?fid='.$i.'">Primary
Link'.$i.'</a> | ';

// definition for 'subnavbar2.php' file
    echo '<a href="file'.$i.'.php?fid='.$i.'">Secondary
Link'.$i.'</a> | ';

Right, I think that all the above dynamic PHP files, in addition to the MySQL dataset that will be fetched in turn, are enough input data for feeding the input of the “TemplateProcessor” class and seeing how it works. Therefore, go ahead read the next section to see how the corresponding template file is parsed.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: