PHP Classes
elePHPant
Icontem

File: template.inc

Recommend this page to a friend!
  Classes of Herman Veluwenkamp  >  Template  >  template.inc  >  Download  
File: template.inc
Role: ???
Content type: text/plain
Description: Template Class
Class: Template
Generates HTML using templates.
Author: By
Last change:
Date: 19 years ago
Size: 3,704 bytes
 

 

Contents

Class file image Download
<?
class html_template {
  var $file_name = '';    // name of file containing template(s).
  var $data = array();    // array of data to be substituted into top level template.
  var $source = '';
  
  var $template = array();
  var $repeat = array(); // holds <REPEAT> segments of template. includes source, data, and substituted html.
  var $html = '';        // holds completed substituted HTML.
                     
  var $default_data = '&nbsp;'; // data to be displayed when nothing is found in $data array.
  
  // each occurence of (* ~ *) is replaced by alternating values of this array.
  var $toggle = array('Dark', 'Lite'); 
  // each occurence of (*+*) is replaced by an incrementing number starting at $count.
  var $count = 0; // starting value for counter. value stays the same across a row.
  var $counter = 0; // internal counter. this gets reset for every new section.
  
  var $error = ''; // holds error messages.


function display() {
  if (empty($this->html)) $this->code();
  print $this->html;
}

function initialise() {
  $this->source = join('', file($this->file_name));
  if (empty($this->source)) {
    $this->error = 'empty template file';
    return false;
  } else $this->template['source'] = $this->source;
  
  $matches = array();
  if (preg_match_all('/\<REPEAT\s+ID="(.*)"\>(.*)\<\/REPEAT\>/sUi', $this->template['source'], $match)) {
    while (list($key, $val) = each($match[0])) {
      $name = $match[1][$key];
      $this->repeat[$name]['source'] = $match[2][$key];
      $regex_repeat = '/'.str_replace('/', '\/', preg_quote($match[0][$key])).'/sUi';
      $this->template['source'] = preg_replace($regex_repeat, "(* $name *)", $this->template['source']); // replace <REPEAT> segment with ID.
      $this->repeat[$name]['data'] = array(); // initialise data array for this <REPEAT> segment.
    }
  } //else $this->html = $this->source;
  return true;
} //end of function initialise.


function code() {
  //cycle through <REPEATS> sustituting data from array.
  while (list($name, $repeat) = each($this->repeat)) {
    $this->data[$name] = $this->code_repeat($name, $repeat['source'], $this->repeat[$name]['data']);
  }
  // substitute values into top level template.
  $this->html = $this->replace($this->template['source'], $this->data);
} // end of code().


function code_repeat($name, $source, $data) { // generate HTML code for <REPEAT> segments.
  $html = '';
  $row = '';
  $i= 0;
  
  $rows = sizeof($data);
  reset($this->toggle); // start toggle at first element.
  $this->counter = $this->count; // start counter at count.

  for ($row = 0; $row < $rows; $row++) {
    $data_row = $data[$row];
    $data_row['~'] = current($this->toggle);
    $data_row['+'] = $this->counter;
    
    next($this->toggle) or reset($this->toggle);
    $this->counter++;
   
    $html .= $this->replace($source, $data_row);   
  }
  return $html;
} // end of code_repeat().


function replace($source, $data) { // replace (* ... *) in source with items in data array.
  $sprintf_search       = '/(\(\*\s*(\w+)\s+(%.+)\s*\*\))/Ue';
  $numberformat_search  = '/(\(\*\s*(\w+)\s+#\.*(\d+)\s*\*\))/Ue';
  $default_search       = '/(\(\*\s*([~\+\w]+)\s*\*\))/Ue';

  $sprintf_replace      = 'sprintf("\\3", $data["\\2"])';
  $numberformat_replace = 'number_format($data["\\2"], \\3)';
  $default_replace      = '$data["\\2"]';

  $search =  array($default_search,  $sprintf_search,  $numberformat_search);
  $replace = array($default_replace, $sprintf_replace, $numberformat_replace);

  return @preg_replace($search, $replace, $source);
}

} // end of class html_template.
?>