Home arrow PHP arrow Page 2 - Building a Template Parser Class with PHP, Part I

PHP: The first templating system available - PHP

It is easy to create a templating system in PHP; in fact, there are a number of templating system packages. But what if you're putting together a relatively small website, and don't really need one of those full-fledged systems? In this first part of a two-part article, you will learn how to create a simple but extensible PHP class for parsing templates.

TABLE OF CONTENTS:
  1. Building a Template Parser Class with PHP, Part I
  2. PHP: The first templating system available
  3. Defining the structure of the PHP class
  4. Completing the class: the "parseFile()" and "display()" methods
  5. Implementing the class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 85
March 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

 

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: