Home arrow PHP arrow Page 3 - Completing an Extensible Website Engine with PHP 5

Injecting web page contents into the template file - PHP

Are you one of those web developers searching for a comprehensive approach to constructing an expandable website engine with PHP 5? If your answer is a resounding yes, then this series might appeal to you. Welcome to the final part of the series “Building an extensible website engine with PHP 5.” In two tutorials, this series teaches you how to build a versatile website system which allows you to generate dynamic web documents by using a few simple PHP classes.

  1. Completing an Extensible Website Engine with PHP 5
  2. Pulling web page contents from a database table
  3. Injecting web page contents into the template file
  4. The website engine in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
October 31, 2006

print this article



As I said in the previous section, to complete the pertinent website engine, it’s necessary to add an additional component to it, which will be responsible for parsing the respective template file. In this way, both the data and presentation layers will be interconnected to each other by the couple of MySQL processing classes shown previously, as well as by the template processor that I'm just about to create.

With reference to the template processor class, please take a look at its source code, which is listed below:

class TemplateProcessor {
    private $output='';// set default value for overall class
    private $rowTag='p';// set default value for database row tag
    private $tags=array();// set default value for tags
    private $templateFile='templates/default_template.htm';// set
default value for template file
    private $cacheFile='cache/default_cache.cache';// set default
value for cache file
    private $expiry=1;// set default value for cache expiration
    public function __construct($tags=array()){
            throw new Exception('Invalid number of tags');
            // read data from cache file
            // read template file
            // process template file
            // clean up empty tags
            // write compressed data to cache file
        // send gzip encoding http header
    // check cache validity
    private function isCacheValid(){
        // determine if cache file is valid or not
            return true;
        return false;
    // process template file
    private function processTemplate($tags){
        foreach($tags as $tag=>$data){
            // if data is array, traverse recursive array of tags
            // if data is a file, fetch processed file
            // if data is a MySQL result set, obtain a formatted
list of database rows
            elseif(@get_resource_type($data)=='mysql result'){
                    foreach($row as $col){
            // if data contains the '[code]' elimiter, parse data
as PHP code
        // clean up empty tags
    // process input file
    private function processFile($file){
          return $contents;
    // write compressed data to cache file
    private function writeCache(){
            throw new Exception('Error writing data to cache
    // read compressed data from cache file
    private function readCache(){
            throw new Exception('Error reading data from cache
        return $cacheContents;
    // return overall output
    public function getHTML(){
          return $this->output;
    // return compressed output
    private function getCompressedHTML(){
        // check if browser supports gzip encoding
            // start output buffer
            // echo page contents to output buffer
            echo $this->output;
            // compress (X)HTML output with gzip
            // clean up output buffer
            // return compressed (X)HTML content
            return $this->output;
        return false;
    // send gzip encoding http header
    private function sendEncodingHeader(){
        header('Content-Encoding: gzip');

As shown above, the “TemplateProcessor” class has some handy methods for replacing all the placeholders contained inside the “default_template.thm” file with actual web page data. Aside from the functionality that I just mentioned, this class is also capable of caching the parsed template during a given period of time, as well as sending parsed contents as compressed data via the “Gzip” compression algorithm.

However, it’s important to notice here that the “TemplateProcessor” class is itself the other critical part of the website engine. When used in conjunction with the previous MySQL processing classes, it is capable of generating true dynamic web pages even when the website’s whole structure is static (remember that each web document is created basically from the same “default_template.htm” file).

All right, now you’ve seen separately how each part of the website engine does its thing. On one hand you learned how to fetch the contents of each web page, while on the other hand you saw how these contents are injected straight into the previously defined template file. But how can these parts work together to implement a fully-functional website engine?

Well, in the next section, I’ll use all the classes that I defined before so you can see how this PHP 5-based website engine can generate web pages on the fly.

To learn how this will be achieved, all you have to do is click on the link shown below and keep reading. We’re almost finished!

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