Source for file PHPLIB.php
Documentation is available at PHPLIB.php
// vim: set expandtab tabstop=4 shiftwidth=4:
// This code that was derived from the original PHPLIB Template class
// is copyright by Kristian Koehntopp, NetUSE AG and was released
// Authors: Kristian Koehntopp <kris@koehntopp.de> (original from PHPLIB)
// Bjoern Schotte <bjoern@rent-a-phpwizard.de> (PEARification)
// Martin Jansen <mj@php.net> (PEAR conformance)
// $Id: PHPLIB.php,v 1.3 2006/09/17 06:22:04 wurley Exp $
//require_once "PEAR.php";
* Converted PHPLIB Template class
* For those who want to use PHPLIB's fine template class,
* here's a PEAR conforming class with the original PHPLIB
* template code from phplib-stable CVS. Original author
* was Kristian Koehntopp <kris@koehntopp.de>
* @author Bjoern Schotte <bjoern@rent-a-phpwizard.de>
* @author Martin Jansen <mj@php.net> (PEAR conformance)
* If set, echo assignments
* $file[handle] = "filename";
* fallback paths that should be defined in a child class
public $file_fallbacks = array();
* Relative filenames are relative to this pathname
public $_varKeys = array();
* $_varVals[key] = "value";
public $_varVals = array();
* "remove" => remove undefined variables
* "comment" => replace undefined variables with comments
* "keep" => keep undefined variables
public $unknowns = "remove";
* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly
public $haltOnError = "report";
* The last error message is retained here
* @param string template root directory
* @param string how to handle unknown variables
* @param array fallback paths
if (is_array($fallback)) $this->file_fallbacks = $fallback;
* Sets the template directory
* @param string new template directory
$this->halt("setRoot: $root is not a directory.");
* What to do with unknown variables
* - "remove" will remove unknown variables
* (don't use this if you define CSS in your page)
* - "comment" will replace undefined variables with comments
* - "keep" will keep undefined variables as-is
$this->unknowns = $unknowns;
* Set appropriate template files
* With this method you set the template files you want to use.
* Either you supply an associative array with key/value pairs
* where the key is the handle for the filname and the value
* is the filename itself, or you define $handle as the file name
* handle and $filename as the filename if you want to define only
* @param mixed handle for a filename or array with handle/name value pairs
* @param string name of template file
function setFile($handle, $filename = "")
$this->halt("setFile: For handle $handle filename is empty.");
$this->file[$handle] = $this->_filename($filename);
while (list ($h, $f) = each($handle)) {
$this->file[$h] = $this->_filename($f);
* Set a block in the appropriate template handle
* By setting a block like that:
* <!-- BEGIN blockname -->
* <!-- END blockname -->
* you can easily do repeating HTML code, i.e. output
* database data nice formatted into a HTML table where
* each DB row is placed into a HTML table row which is
* It extracts the template $handle from $parent and places
* variable {$name} instead.
* @param string parent handle
* @param string block name handle
* @param string variable substitution name
function setBlock($parent, $handle, $name = "")
if (!$this->_loadFile($parent)) {
$this->halt("setBlock: unable to load $parent.");
$str = $this->getVar($parent);
$reg = "/[ \t]*<!--\s+BEGIN $handle\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $handle\s+-->\s*?\n?/sm";
if (isset ($m[1][0])) $this->setVar($handle, $m[1][0]);
* Set corresponding substitutions for placeholders
* @param string name of a variable that is to be defined or an array of variables with value substitution as key/value pairs
* @param string value of that variable
* @param boolean if true, the value is appended to the variable's existing value
function setVar($varname, $value = "", $append = false)
if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n";
$this->_varKeys[$varname] = $this->_varname($varname);
($append) ? $this->_varVals[$varname] .= $value : $this->_varVals[$varname] = $value;
while (list ($k, $v) = each($varname)) {
if ($this->debug) print "array: set *$k* to *$v*<br>\n";
$this->_varKeys[$k] = $this->_varname($k);
($append) ? $this->_varVals[$k] .= $v : $this->_varVals[$k] = $v;
* Substitute variables in handle $handle
* @param string name of handle
* @return mixed string substituted content of handle
if (!$this->_loadFile($handle)) {
$this->halt("subst: unable to load $handle.");
* Same as subst but printing the result
* @param string handle of template
* @return bool always false
print $this->subst($handle);
* Parse handle into target
* Parses handle $handle into $target, eventually
* appending handle at $target if $append is defined
* @param string target handle to parse into
* @param string which handle should be parsed
* @param boolean append it to $target or not?
* @return string parsed handle
function parse($target, $handle, $append = false)
$str = $this->subst($handle);
($append) ? $this->setVar($target, $this->getVar($target) . $str) : $this->setVar($target, $str);
while (list (, $h) = each($handle)) {
* Same as parse, but printing it.
* @param string target to parse into
* @param string handle which should be parsed
* @param should $handle be appended to $target?
function pParse($target, $handle, $append = false)
print $this->finish($this->parse($target, $handle, $append));
* Return all defined variables and their values
* @return array with all defined variables and their values
while (list ($k, ) = each($this->_varKeys)) {
$result[$k] = $this->getVar($k);
* Return one or more specific variable(s) with their values.
* @param mixed array with variable names or one variable name as a string
* @return mixed array of variable names with their values or value of one specific variable
if (isset ($this->_varVals[$varname])) {
return $this->_varVals[$varname];
while (list ($k, ) = each($varname)) {
$result[$k] = (isset ($this->_varVals[$k])) ? $this->_varVals[$k] : "";
* Get undefined values of a handle
* @param string handle name
* @return mixed false if an error occured or the undefined values
if (!$this->_loadFile($handle)) {
$this->halt("getUndefined: unable to load $handle.");
while (list (, $v) = each($m)) {
if (!isset ($this->_varKeys[$v])) {
if (isset ($result) && count($result)) {
* @param string string to finish
* @return finished, i.e. substituted string
switch ($this->unknowns) {
$str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
* Print variable to the browser
* @param string name of variable to print
* @param string variable to get
* @return string string with finished variable
* Complete filename, i.e. testing it for slashes
* @param string filename to be completed
* @return string completed filename
function _filename($filename)
// if (substr($filename, 0, 1) != "/") {
// $filename = $this->root."/".$filename;
if (is_array($this->file_fallbacks) && count($this->file_fallbacks) > 0) {
reset($this->file_fallbacks);
while (list (,$v) = each($this->file_fallbacks)) {
$this->halt(sprintf("filename: file %s does not exist in the fallback paths %s.",$filename,implode(",",$this->file_fallbacks)));
$this->halt(sprintf("filename: file %s does not exist.",$filename));
* Protect a replacement variable
* @param string name of replacement variable
* @return string replaced variable
function _varname($varname)
* load file defined by handle if it is not loaded yet
* @return bool FALSE if error, true if all is ok
function _loadFile($handle)
if (isset ($this->_varKeys[$handle]) and !empty($this->_varVals[$handle])) {
if (!isset ($this->file[$handle])) {
$this->halt("loadfile: $handle is not a valid handle.");
$filename = $this->file[$handle];
if (!$fp = @fopen($filename,"r")) {
$this->halt("loadfile: couldn't open $filename");
$this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
* Error function. Halt template system with message to show
* @param string message to show
$this->_lastError = $msg;
if ($this->haltOnError != "no") {
// return $this->haltMsg($msg);
if ($this->haltOnError == "yes") {
* printf error message to show
* @param string message to show
* @return object PEAR error object
// PEAR::raiseError(sprintf("<b>Template Error:</b> %s<br>\n", $msg));
|