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 <schotte@mayflower.de> (PEARification)
// Martin Jansen <mj@php.net> (PEAR conformance)
// $Id: PHPLIB.php,v 1.1 2006/11/01 15:23:52 cweiske Exp $
* 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 <schotte@mayflower.de>
* @author Martin Jansen <mj@php.net> (PEAR conformance)
class HTML_Template_PHPLIB
* If set, echo assignments
* $file[handle] = 'filename';
* fallback paths that should be defined in a child class
var $file_fallbacks = array ();
* Relative filenames are relative to this pathname
* $_varVals[key] = 'value';
* '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 $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
function HTML_Template_PHPLIB ($root = '.', $unknowns = 'remove', $fallback= '')
$this->setUnknowns ($unknowns);
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
function setUnknowns ($unknowns = 'keep')
$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 ]);
$this->setVar ($parent, $str);
* 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 . '.');
return @str_replace($this->_varKeys, $this->_varVals, $this->getVar ($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)) {
$this->setVar ($target, $str);
* 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
function getVar ($varname)
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
function getUndefined ($handle)
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 variable \\1 undefined -->', $str);
* Print variable to the browser
* @param string name of variable to print
print $this->finish ($this->getVar ($varname));
* @param string variable to get
* @return string string with finished variable
return $this->finish ($this->getVar ($varname));
* 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)) {
'filename: file %s does not exist in the fallback paths %s.',
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)
return '{' . $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.');
$this->setVar ($handle, $str);
* 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);
* printf error message to show
* @param string message to show
* @return object PEAR error object
return PEAR ::raiseError (sprintf('<b>Template Error:</b> %s<br>' . "\n", $msg));
* Backwards-compatibility for HTML_Template_PHPLIB.
* Used to have this name here.
class Template_PHPLIB extends HTML_Template_PHPLIB
Documentation generated on Mon, 11 Mar 2019 15:00:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|