PHP
  Home arrow PHP arrow Page 2 - Building a Template Parser Class with PHP, Part I
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building a Template Parser Class with PHP, Part I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 73
    2005-03-22


    Table of Contents:
  • Building a Template Parser Class with PHP, Part I
  • PHP: The first templating system available
  • Defining the structure of the PHP class
  • Completing the class: the "parseFile()" and "display()" methods
  • Implementing the class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building a Template Parser Class with PHP, Part I - PHP: The first templating system available
    ( Page 2 of 5 )

     

    In just a minute, you may think that I listened to too much strange music at my sister's wedding, but I really didn't. Please consider the following code:

     

    <?php

    // lets's define some variables

    $title='This page is pretty musical!';

    $content='There are unsmiling faces and bright plastic chains, and a wheel in perpetual motion.';

    $author='The Alan Parsons Project.';

    // now let's replace variable values in the document

    ?>

     

    <html>

    <head>

    <title><?php echo $title?></title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    </head>

    <body>

    <div>

    <p><?php echo $content?></p>

    <p>The author of this song is: <?php echo $author?></p>

    </div>

    </body>

    </html>

     

    The above example shows us that PHP is itself a templating system. Of course, this approach is not recommended for many reasons. Here we’re mixing up PHP code with HTML markup, which you do not want to do if your goal is to keep applications in different layers.

     

    To improve the situation and maintain applications in separated layers, we might define our template in the following way:

     

    <html>

    <head>

    <title>$title</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    </head>

    <body>

    <div>

    <p>$content</p>

    <p>The author of this song is: $author</p>

    </div>

    </body>

    </html>

     

    And next, the PHP code to replace the variables with the corresponding values:

     

    <?php

     

    // lets's define some variables

     

    $title='This page is pretty musical';

    $content='There are unsmiling faces and bright plastic chains, and a wheel in perpetual motion.';

    $author='The Alan Parsons Project.';

     

    // now let's replace their values in the document

    ob_start();

    include('templates/template.htm');

    $pageHTML=addslashes(ob_get_contents());

    ob_end_clean();

     

    // parse the template to replace variables with their values

    eval("\$pageHTML=\"$pageHTML\";");

     

    // send parsed file to the browser

    echo $pageHTML;

     

    ?>

     

    As you can see, the situation is much more acceptable. We have a single HTML template file, where we’ve defined some variables as placeholders. Then, starting an output buffer, we grab the content of the included template file, escaping any characters that would break a PHP string, and finally dumping it to a variable. Using "eval()" we evaluate a string as if it were PHP code. This means that the variables in our template are replaced by the values previously assigned. We’re getting closer to creating a simple template parser, but still the above approach is not good enough. Still, it is a basic technique for parsing template files.

     

    However, we need to have more flexibility at the moment of defining the placeholders, looking for an object-oriented solution. This way our code is more reusable and encapsulated. Taking this into account, we’re going to define a simple PHP template parser class for processing template files and send the finished page to the browser, or wherever you want to submit it. Let’s take a look at the basic structure of the PHP class.

     



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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek