<?php

require_once('utility.phps');

class 
Template  {

    
/**
     *  absolute path to template directory
     *  @private
     */
    
var $template_path '';

    
/**
     *  absolute path to compiled templates
     *  @private
     */
    
var $compiled_path '';
    
    
/**
     *  time in seconds to utilize cached content
     *  @private
     */
    
var $cachetime 36000;

    
/**
     *  force compile (set to true while developing)
     *  @private
     */
    
var $force_compile true;

//  -----------------------------------------
//
//  Do not modify variables beyond this point
//
//  -----------------------------------------
    
    /**
     *  string holds tempate file
     *  @private
     */
    
var $template '';
    
    
/**
     *  array of data objects
     *  @private
     */
    
var $data = array();

    
/**
     *  object compiler
     *  @private
     */
    
var $compiler '';

    
/**
     *  md5 hash of main template (we won't bother about included files at this point)
     *  @private
     */
    
var $template_hash '';
    
    
/**
     *  relative location of template file
     *  @private
     */
    
var $template_name '';

    
/**
     *  attach a template
     *  @param  string  template file
     *  @return null
     *  @public
     **/
    
    
function load($template)    {
        
$file $this->_join($this->template_path$template);
        if (
is_readable($file) )    {
            
$this->template         =   implode("\n"file($file) );
            
$this->template_name    =   $template;
            
$this->template_hash    =   md5($this->template);
        } else {
            
trigger_error("File: '{$file}' does not exist, or is not readable"E_USER_ERROR);
        }
    }

    
/**
     *  attach data to a template section
     *  @param  string field or array of key/value pairs or Record objects
     *  @param  array optional array of key value pairs or array of Record objects
     *  @return null
     *  @public
     **/

    
function attach($field$data null) {
    
//  scenario 1: attach('field', $data);
        
if (is_array($data) )   {
            
$this->data[$field] = $data;
        }
    
//  scenario 2: attach($data)
        
if (is_array($field) )   {
            
$this->data array_merge($this->data$field);
        }
    }

    
/**
     *  final display of view
     *  @return null
     *  @public
     **/
    
    
function display()  {
        if (
$this->_is_compiled() and $this->force_compile == false) {
            
$this->_display();
        } else {
        
//  no existing template, build it!
            
require_once('compiler.php');
            
$this->compiler =& new Compiler();
            
$this->compiler->compile($this);
            
$this->_display();
        }
    }

    
/**
     *  join a absolute path and a relative path gracefully
     *  @param  string absolute path
     *  @param  string relative path
     *  @return string absoulte path
     *  @private
     **/
    
    
function _join($dir$file) {
        
$dir    =   str_replace('\\''/'$dir);
        
$file   =   str_replace('\\''/'$file);
        if (
$dir and substr($dir, -1) != '/')    {
            
$dir    =   $dir.'/';
        }
        if (
substr($file01) == '/') {
            
$file   =   substr($file1);
        }
        if (
substr($file02) == './')  {
            
$file   =   substr($file2);
        } 
        return 
$dir.$file;
    }

    
/**
     *  figure out of template is pre-compiled
     *  @return boolean
     *  @private
     **/
    
    
function _is_compiled()  {
        
$cachefile  =   $this->_join($this->compiled_path$this->template_name);
        if (
file_exists($cachefile) )    {
            
$last_modified filemtime($cachefile);
            if ( (
time() - $last_modified) > $this->cachetime) {
            
//  re-compile cachefile
                
return false;
            } else {
            
//  use cachefile
                
return true;
            }
        }
        return 
false;
    }

    
/**
     *  output template
     *  @return null
     *  @private
     **/
    
    
function _display() {
        
$tpl    =   $this->_join($this->compiled_path$this->template_name);
        require_once(
$tpl);
        
$func   =   'tpl'.$TEMPLATE_HASH;
        
$func(new Record($this->data) );
    }
}

?>