my template parser

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
dall
Forum Newbie
Posts: 5
Joined: Sun Feb 04, 2007 7:59 am

my template parser

Post by dall »

Here my template class, please add your comments, critique, thanks.

Code: Select all

<?php

    /**
    * Template class
    *
    * @package     cms
    * @version     0.0.1
    * @author      dall
    * @date        2006-02-05
    * 
    */ 

class Template {

    var $filename;
    var $tags;
    var $file_path;
    var $output;
    var $data;
    var $expire;
    var $is_cached;
    var $cache_filename;
    var $cache_path;
    
    // constructor
    function Template() 
    {
        $tags = array();
    }
    
    // load template
    function load($filename)
    {
        $this->filename = $filename;
        $this->file_path = MODULES_PATH.$this->filename.'/'.'templates/'.$this->filename.'.php';

        $this->fetch_file($this->filename);
        $this->headers();
        return $this->output;
    }
       
    // load cached template
    function load_cache($filename, $expire = 900)
    {
        $this->cache_filename = md5($filename);
        $this->cache_path = CACHE_PATH.$this->cache_filename.'.php';
        
        $this->expire = $expire;
        if($this->cached()) {
            $fp = @fopen($this->cache_path, 'r');
            $this->output = fread($fp, filesize($this->cache_path));
            fclose($fp);
            $this->headers();
            return $this->output;
        } else {
            $data = $this->fetch_file($filename);
            if($fp = @fopen($this->cache_path, 'w+')) {
                fwrite($fp, $data);
                fclose($fp);
            }
            else {
                die('Template->load: Cant write cache file "'.$this->file_path);
            }
            $this->headers();
            return $this->output;
        }
    }
    
    // load file contents
    function fetch_file($filename)
    {
        $this->filename = $filename;
        $this->file_path = MODULES_PATH.$this->filename.'/'.'templates/'.$this->filename.'.php';
        
        if(file_exists($this->file_path))
        {
            extract($this->tags);
            ob_start();
            require_once ($this->file_path);
            $this->output = ob_get_contents();
            ob_end_clean();
            return $this->output;
        }
        else {
            die('Template->load: Template file "'.$this->file_path.'" not found');
        }
    }
    
    // file is cached?
    function cached()
    {
       if($this->is_cached) return true;
       if(!$this->cache_filename) return false;

       if(!file_exists($this->cache_path)) return false;

       if(!($mtime = filemtime($this->cache_path))) return false;
       
       if(($mtime + $this->expire) < time()) {
           @unlink($this->cache_path);
           return false;
       }
       else {
           $this->is_cached = true;
           return true;
       }
    }
    
    // update cache
    function update_cache($filename)
    {
        $this->cache_filename = md5($filename);
        $this->cache_path = CACHE_PATH.$this->cache_filename.'.php';
        
        if(!file_exists($this->cache_path))
        {
            die('Template->update_cache: cache file dont exist');
        }
        
        
        @unlink($this->cache_path);
    }
    
    // set template variable
    function set($name, $value) {
        $this->tags[$name] = $value;
    }
    
    // set template variable (array)
    function set_array($tags, $clear = false) {
        if($clear) {
            $this->tags = $tags;
        }
        else {
            if(is_array($tags)) $this->tags = array_merge($this->tags, $tags);
        }
    }
    
    // gzip data, set headers
    function headers()
    {
        if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
        {
            ob_start("ob_gzhandler");
        } else {
            ob_start();
        }
        header('Content-Type: text/html; charset=UTF-8');
    }
}

?>
Last edited by dall on Mon Feb 05, 2007 2:30 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Shouldn't the default value for $tags in the load method be an empty array()?

While "/" often works across many platforms, DIRECTORY_SEPARATOR is PHP's own constant that should probably be used.

$data in parse() is potentially being misused.

The content type specification in display() may not be optimal. What if you write XML or XUL templates?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might want to add:

Code: Select all

function set($tag, $value) {
     if ($tag) {
          $this->tags[$tag] = $value;
     }
}
Then you can pass the template object around and still set values.
(#10850)
dall
Forum Newbie
Posts: 5
Joined: Sun Feb 04, 2007 7:59 am

Post by dall »

yes thanks, updated code.
dall
Forum Newbie
Posts: 5
Joined: Sun Feb 04, 2007 7:59 am

Post by dall »

how to remove undefined vars in template?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

In PHP4, this is typically done by having a getter method. If the variable was not found, false is usually returned

Code: Select all

function get($var)
{
   if (array_key_exists($var, $this->data)) 
   {
      return $this->data[$var];
   }

   return false;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dall wrote:how to remove undefined vars in template?
Probably a regular expression.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I'd argue that the function update_cache() has the wrong name. It's not updating the version of the page in the cache, it's just wiping out the old version. Obviously the plan is to create a new version next time the code calls load_cache(), but nonetheless if I called update_cache() I would expect an updated version of the file to be generated in the cache. What you have should really be called something like delete_cache().
Post Reply