[phplib-dev] Faster Template From: Zakaria (zak_mail <email protected>)
Date: 06/29/00

Assalamu'alaikum wr. wb.

Well not much, but in some of my heavily used template app I could gain 0.12s.
I change it from the latest CVS (at least Jun 27 13:45 +7 GMT)

Change log:
o) Use str_replace loop instead of preg_replace
o) Fix "template/template/file.ihtml" Bug
o) set_block parse the block instead implode_block from get_var
   to fix "Could not have empty block Bug"
o) Add ability to add another filter (preg_replace) after subst
o) Template variable pattern in class variable
o) Add preprocessing <!--IF var-->...<!--ELSE var-->...<!--ENDIF var-->
   which is disabled by default
o) Change $root default to empty array
o) Remove $block, $varkeys, varname(), implode_block()

Okay that's it.
I hope I didn't break anything

Here's the complete template.inc because the diff is almost as big as this.
===========================================================================
<?php
/*
 * Session Management for PHP3
 *
 * (C) Copyright 1999 NetUSE GmbH
 * Kristian Koehntopp
 *
 * $Id: template.inc,v 1.3 2000/06/05 08:26:56 kk Exp $
 *
 */

class Template {
  var $classname = "Template";

  /* if set, echo assignments */
  /* 1 = debug set, 2 = debug get, 4 = debug internals */
  var $debug = false;

  /* $file[varname] = "filename"; */
  var $file = array();
  
  /* Template searches for files in one of these directories */
  var $root = array();

  /* Place to hold variabel values */
  var $varvals = array();

  /* "remove" => remove undefined variables
   * "comment" => replace undefined variables with comments
   * "keep" => keep undefined variables
   */
  var $unknowns = "remove";
  
  /* "yes" => halt
   * "report" => report error, continue
   * "no" => ignore error quietly
   */
  var $halt_on_error = "yes";
  
  /* last error message is retained here */
  var $last_error = "";

  /* filter to apply to subst */
  var $filter_pat;
  var $filter_rep;

  /* variable pattern */
  var $var_pat = '[-_a-zA-Z0-9]+';

  /* use preprocessing or not */
  var $use_preproc = false;

  /***************************************************************************/
  /* public: Constructor.
   * root: template directory.
   * unknowns: how to handle unknown variables.
   */
  function Template($root = ".", $unknowns = "remove") {
    $this->set_root($root);
    $this->set_unknowns($unknowns);
  }

  /* public: setroot(pathname $root)
   * root: array with template directories.
   */
  function set_root($root) {
    if (!is_array($root)) {
      if (!is_dir($root)) {
        $this->halt("set_root (scalar): $root is not a directory.");
        return false;
      }
      
      $this->root[] = $root;

    } else {
      reset($root);
      while(list($k, $v) = each($root)) {
        if (!is_dir($v)) {
          $this->halt("set_root (array):"
              . " $v (entry $k of root) is not a directory.");
          return false;
        }
        
        $this->root[] = $v;
      }
    }

    return true;
  }

  /* public: set_unknowns(enum $unknowns)
   * unknowns: "remove", "comment", "keep"
   */
  function set_unknowns($unknowns = "remove") {
    $this->unknowns = $unknowns;
  }

  /* public: set_file(array $filelist)
   * filelist: array of varname, filename pairs.
   *
   * public: set_file(string $varname, string $filename)
   * varname: varname for a filename,
   * filename: name of template file
   */
  function set_file($varname, $filename = "") {
    if (!is_array($varname)) {
    
      if ($filename == "") {
        $this->halt("set_file: For varname $varname filename is empty.");
        return false;
      }
      $this->file[$varname] = $this->filename($filename);

    } else {

      reset($varname);
      while(list($h, $f) = each($varname)) {
        if ($f == "") {
          $this->halt("set_file: For varname $h filename is empty.");
          return false;
        }
        $this->file[$h] = $this->filename($f);
      }
    }

    return true;
  }

  /* public: set_block(string $parent, string $varname, string $name = "")
   * extract the template $varname from $parent
   * and place variabel {$name} instead.
   */
  function set_block($parent, $varname, $name = "") {
    if ($name == "")
      $name = $varname;

    /* get parent variable */
    $str = $this->get_var($parent);
   
    /* find the subblock we are looking for and extract it */
    $reg = '/<!--\s*BEGIN\s+' . $varname
      . '\s*-->(.*?)<!--\s*END\s+' . $varname . '\s*-->/sm';
    if (!preg_match($reg, $str, $m)) {
      $this->halt("set_block - no match for $varname variable");
    } else {
      /* implode the subblock to the requested alias */
      $str = str_replace($m[0], '{'.$name.'}', $str);

      if ($this->debug & 4) {
        print("<b>set_block:</b> extract <b>$varname</b> from <b>$parent</b>"
            . ", leaving {"."$name}<br>\n");
      }
        
      /* update variables */
      $this->set_var($varname, $m[1]);
      $this->set_var($parent, $str);
    }

    return true;
  }
  
  /* public: set_var(array $values)
   * values: array of variable name, value pairs.
   *
   * public: set_var(string $varname, string $value)
   * varname: name of a variable that is to be defined
   * value: value of that variable
   */
  function set_var($varname, $value = "") {
    if (!is_array($varname)) {
      if (!empty($varname)) {
        if ($this->debug & 1)
          printf("<b>set_var:</b> (with scalar) <b>%s</b> = *%s*<br>\n"
              , $varname, htmlentities($value));
        $this->varvals[$varname] = $value;
      }
    } else {
      reset($varname);
      while(list($k, $v) = each($varname)) {
        if (!empty($k)) {
          if ($this->debug & 1)
            printf("<b>set_var:</b> (with array) <b>%s</b> = *%s*<br>\n"
                , $k, htmlentities($v));
          $this->varvals[$k] = $v;
        }
      }
    }
  }

  /* public: set_filter(string $pattern, string $replacer)
   * public: set_filter(array $pattern, array $replacer)
   *
   * pattern: pattern to replace
   * replacer: the replacer
   */
  function set_filter($pattern, $replacer) {
    $this->filter_pat = $pattern;
    $this->filter_rep = $replacer;
  }

  /* public: clear_filter()
   * Clear the filter
   */
  function clear_filter() {
    $this->filter_pat = '';
    $this->filter_rep = '';
  }

  /***************************************************************************/
  /* public: subst(string $varname)
   * varname: varname of template where variables are to be substituted.
   */
  function subst($varname) {
    $str = $this->get_var($varname);
    if ($this->use_preproc) $this->preproc($str);
    reset($this->varvals);
    while ( list($k, $v) = each($this->varvals) ) {
      $str = str_replace('{'.$k.'}', $v, $str);
    }

    if ($this->filter_pat) {
      $str =  <email protected>($this->filter_pat, $this->filter_rep, $str);
    }

    return $str;
  }
  
  /* public: psubst(string $varname)
   * varname: varname of template where variables are to be substituted.
   */
  function psubst($varname) {
    print $this->subst($varname);
    
    return false;
  }

  /* public: parse(string $target, string $varname, boolean append)
   * public: parse(string $target, array $varname, boolean append)
   * target: varname of variable to generate
   * varname: varname of template to substitute
   * append: append to target varname
   */
  function parse($target, $varname, $append = false) {
    if (!is_array($varname)) {
      $str = $this->subst($varname);
      if ($append) {
        $this->set_var($target, $this->get_var($target) . $str);
      } else {
        $this->set_var($target, $str);
      }
    } else {
      reset($varname);
      while(list($i, $h) = each($varname)) {
        $str = $this->subst($h);
        $this->set_var($target, $str);
      }
    }
    
    return $str;
  }
  
  function pparse($target, $varname, $append = false) {
    print $this->parse($target, $varname, $append);
    return false;
  }
  
  /* public: get_vars()
   * return all variables as an array (mostly for debugging)
   */
  function get_vars() {
    reset($this->varvals);
    while(list($k,) = each($this->varvals)) {
      $result[$k] = $this->get_var($k);
    }
    
    return $result;
  }
  
  /* public: get_var(string varname)
   * varname: name of variable.
   *
   * public: get_var(array varname)
   * varname: array of variable names
   */
  function get_var($varname) {
    if (!is_array($varname)) {
    
      /* check for empty variable */
      if (empty($this->varvals[$varname])) {
        /* if so, check if it should be loaded */
        if (isset($this->file[$varname]))
          $this->loadfile($varname);
      }
      if ($this->debug & 2)
        printf ("<b>get_var</b> (with scalar) <b>%s</b> = *%s*<br>\n"
            , $varname, htmlentities($this->varvals[$varname]));
      return(isset($this->varvals[$varname]) ? $this->varvals[$varname] : "");

    } else {
    
      reset($varname);
      while(list($k, $v) = each($varname)) {
        /* check for empty variable */
        if (empty($this->varvals[$varname])) {
          /* if so, check if it should be loaded */
          if ($this->file[$v])
            $this->loadfile($v);
        }
        if ($this->debug & 2)
          printf ("<b>get_var:</b> (with array) <b>%s</b> = *%s*<br>\n"
              , $v, htmlentities($this->varvals[$v]));
        $result[$v] = $this->varvals[$v];
      }
      
      return $result;
    }
  }
  
  /* public: get_undefined($varname)
   * varname: varname of a template.
   */
  function get_undefined($varname) {
    $str = $this->get_var($varname);
    preg_match_all('/\{('.$this->var_pat.')\}/', $str, $m);
    $m = $m[1];
    if (!is_array($m))
      return false;

    reset($m);
    while(list(, $v) = each($m)) {
      if (!isset($this->varvals[$v]))
        $result[$v] = $v;
    }
    
    if (count($result))
      return $result;
    else
      return false;
  }

  /* public: finish(string $str)
   * str: string to finish.
   */
  function finish($str) {
    switch ($this->unknowns) {
      case "keep":
      break;
      
      case "remove":
        $str = preg_replace('/\{'.$this->var_pat.'\}/', "", $str);
      break;

      case "comment":
        $str = preg_replace('/\{'.$this->var_pat.'\}/'
            , "<!-- Template $varname: Variable \\1 undefined -->", $str);
      break;
    }
    
    return $str;
  }

  /* public: p(string $varname)
   * varname: name of variable to print.
   */
  function p($varname) {
    print $this->finish($this->get_var($varname));
  }

  /* public: get(string $varname)
   * varname: name of variable to get.
   */
  function get($varname) {
    return $this->finish($this->get_var($varname));
  }
    
  /***************************************************************************/
  /* private: filename($filename)
   * filename: name to be completed.
   */
  function filename($filename) {
    /* short path for absolute filenames */
    if (substr($filename, 0, 1) == "/" || preg_match("/[a-z]:/i",$filename) ) {
      if (file_exists($filename))
        return $filename;
      else {
        $this->halt("filename (absolute): $filename does not exist.");
        return false;
      }
    }

    /* search path for a matching file */
    reset($this->root);
    while(list($k, $v) = each($this->root)) {
      $f = "$v/$filename";
      
      if (file_exists($f))
        return $f;
    }

    $this->halt("filename (relative): file $filename does not exist anywhere"
        . " in " . implode(" ", $this->root));
    return false;
  }
  
  /* private: loadfile(string $varname)
   * varname: load file defined by varname, if it is not loaded yet.
   */
  function loadfile($varname) {
    if (!isset($this->file[$varname])) {
      $this->halt("loadfile: $varname is not a valid varname.");
      return false;
    }
    $filename = $this->file[$varname];

    $str = implode("",  <email protected>($filename));
    if (empty($str)) {
      $this->halt("loadfile: While loading $varname, $filename does not exist"
          . " or is empty.");
      return false;
    }

    if ($this->debug & 4)
      printf("<b>loadfile:</b> loaded $filename into $varname<br>\n");
    $this->set_var($varname, $str);
    return true;
  }

  /***************************************************************************/
  /* private: preproc(&$str)
   * str: str to be preprocessed
   *
   * Handle <!-- IF var -->....<!-- ELSE var -->...<!-- ENDIF var -->
   */
  function preproc(&$str) {
    $if_pat = '/<!--\s*IF\s+('.$this->var_pat.')\s*-->(.*?)'
      . '<!--\s*ENDIF\s+\1\s*-->/s';

    while ( preg_match($if_pat, $str, $match) ) {
      $pat = $match[0];
      $varname = $match[1];
      $ifcontent = $match[2];

      // Handle else part
      $else_pat = '/^(.*)<!--\s*ELSE\s+'.$varname.'\s*-->(.*)$/s';

      if ( preg_match($else_pat, $ifcontent, $part) ) {
        $ifcontent = $part[1];
        $elsecontent = $part[2];

        if ( $this->get_var($varname) ) {
          $this->preproc($ifcontent);
          $rep = $ifcontent;
        } else {
          $this->preproc($elsecontent);
          $rep = $elsecontent;
        }
      } else {
        if ( $this->get_var($varname) ) {
          $this->preproc($ifcontent);
          $rep = $ifcontent;
        } else {
          $rep = '';
        }
      }
      // Replace the string
      $str = str_replace($pat, $rep, $str);
    }
  }

  /***************************************************************************/
  /* public: halt(string $msg)
   * msg: error message to show.
   */
  function halt($msg) {
    $this->last_error = $msg;
    
    if ($this->halt_on_error != "no")
      $this->haltmsg($msg);
    
    if ($this->halt_on_error == "yes")
      die("<b>Halted.</b>");
    
    return false;
  }
  
  /* public, override: haltmsg($msg)
   * msg: error message to show.
   */
  function haltmsg($msg) {
    printf("<b>Template Error:</b> %s<br>\n", $msg);
  }
}
?>
===========================================================================

Wassallam,

-- Zakaria
    
PT. Asia Karsa Indah z4k4ri4 <email protected>
Advanced Technologies z4k4ri4 <email protected>
Jl. Raya Kalimalang 4B, Jakarta zakaria <email protected>
Telp : (62-21) 8649318 http://www.asia-karsa.com
Fax : (62-21) 8649316 http://linux.or.id/pemula

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>