Assalamu'alaikum wr. wb.
Hi,

A couple months ago (july) after arguing about Conditional Blocks in
Template, Kristian Köhntopp told me about some interesting new feature
in
PHP 4 (output buffering) that I think will eliminate the need to make
conditional block in template. After exploring about it I make a new
template class that use that feature and it has been worked great,
unfortunately I go busy by my work and forget to release it.

Basicly it work like the PHPLib Template without the block stuff
because you could use normal php in the template. Also you could put
some filter (regexp replace) to the output.

I hope it will be included in PHPLib collection.
And sorry for the long email :)

So here it goes:

<?php
/*
 * Template system using only php syntax
 * WARNING: Only work in PHP 4.0.0 and above
 * Copyright (c) 2001 Zakaria <z4k4ri4 at bigfoot.com>
 * This library is freely distributed under GNU LGPL
 */ 

class Pure_Template {
  var $classname = "Pure_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 $var = array();

  /* "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;

 
/***************************************************************************/
  /* public: Constructor.
   * root:     template directory.
   */
  function Pure_Template($root = ".") {
    $this->set_root($root);
  }

  /* 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_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_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->var[$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->var[$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 $zt_varname)
   * zt_varname: file varname of template where variables are to be
substituted.
   */
  function subst($zt_varname) {
    // Only file var to subst
    if ( !isset($this->file[$zt_varname]) ) {
      $this->halt("subst: $zt_varname is not file var.");
      return false;
    } else {
      $zt_file = $this->file[$zt_varname];
    }

    // import the $this->var[FOO] to $FOO
    extract($this->var, EXTR_SKIP);

    // Start output buffering
    ob_start();

    include($zt_file);

    // Flush the buffer, get the content and clear the buffer
    flush();
    $zt_out = ob_get_contents();
    ob_end_clean();

    if ($this->filter_pat) {
      $zt_out = @preg_replace($this->filter_pat, $this->filter_rep,
$zt_out);
    }

    return $zt_out;
  }
  
  /* 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() {
    return $this->var;
  }
  
  /* 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)) {
    
      if ($this->debug & 2)
        printf ("<b>get_var</b> (with scalar) <b>%s</b> = *%s*<br>\n"
            , $varname, htmlentities($this->var[$varname]));
      return(isset($this->var[$varname]) ? $this->var[$varname] : "");

    } else {
    
      reset($varname);
      while(list($k, $v) = each($varname)) {
        if ($this->debug & 2)
          printf ("<b>get_var:</b> (with array) <b>%s</b> = *%s*<br>\n"
              , $v, htmlentities($this->var[$v]));
        $result[$v] = $this->var[$v];
      }
      
      return $result;
    }
  }
  
  /* public: p(string $varname)
   * varname: name of variable to print.
   */
  function p($varname) {
    print $this->get_var($varname);
  }

  /* public: get(string $varname)
   * varname: name of variable to get.
   */
  function get($varname) {
    return $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;
  }
  
 
/***************************************************************************/
  /* 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                         
   Work: [EMAIL PROTECTED], http://asia-karsa.com
Private: [EMAIL PROTECTED]     Yahoo!: z4k4ri4
   http://pemula.linux.or.id

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to