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.4 2007/10/01 15:12:07 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>
* @package HTML_Template_PHPLIB
* @author Bjoern Schotte <schotte@mayflower.de>
* @author Martin Jansen <mj@php.net> (PEAR conformance)
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: PHPLIB.php,v 1.4 2007/10/01 15:12:07 cweiske Exp $
* @link http://pear.php.net/package/HTML_Template_PHPLIB
* If set, echo assignments
* $file[handle] = 'filename';
* fallback paths that should be defined in a child class
* Relative filenames are relative to this pathname
* $_varVals[key] = 'value';
* 'remove' => remove undefined variables
* 'comment' => replace undefined variables with comments
* 'keep' => keep undefined variables
* 'report' => report error, continue,
* 'no' => ignore error quietly
* The last error message is retained here
* @param string $root Template root directory
* @param string $unknowns How to handle unknown variables
* @param array $fallback Fallback paths
* Sets the template directory
* @param string $root 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
* @param string $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 Handle for a filename or array with
* handle/name value pairs
* @param string $filename Name of template file
* @return bool True if file could be loaded
function setFile($handle, $filename = '')
$this->halt('setFile: For handle '
. $handle . ' filename is empty.');
$this->file[$handle] = $this->_filename ($filename);
if ($this->file[$handle] === false ) {
while (list ($h, $f) = each($handle)) {
$this->file[$h] = $this->_filename ($f);
if ($this->file[$h] === false ) {
* 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 Parent handle
* @param string $handle Block name handle
* @param string $name 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 $varname Name of a variable that is to be defined
* or an array of variables with value
* substitution as key/value pairs
* @param string $value Value of that variable
* @param boolean $append If true, the value is appended to the
* variable's existing value
function setVar($varname, $value = '', $append = false )
print 'scalar: set *' . $varname . '* to *'
$this->_varKeys[$varname] = $this->_varname ($varname);
($append) ? $this->_varVals[$varname] .= $value
: $this->_varVals[$varname] = $value;
while (list ($k, $v) = each($varname)) {
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 $handle Name of handle
* @return mixed String substituted content of handle
if (!$this->_loadFile ($handle)) {
$this->halt('subst: unable to load ' . $handle . '.');
$this->_varVals, $this->getVar($handle));
* Same as subst but printing the result
* @param string $handle 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 Target handle to parse into
* @param string $handle Which handle should be parsed
* @param boolean $append 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 Target to parse into
* @param string $handle Handle which should be parsed
* @param should $append If $handle shall 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 $varname 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 Handle name
* @return mixed False if an error occured or the array of 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 $str String to finish
* @return finished, i.e. substituted string
'<!-- Template variable \\1 undefined -->', $str);
* Print variable to the browser
* @param string $varname Name of variable to print
* @param string $varname Name of variable to get
* @return string string with finished variable
* Complete filename, i.e. testing it for slashes
* @param string $filename Filename to be completed
* @return string completed filename
function _filename ($filename)
if (!$this->_isAbsolute ($filename)) {
$filename = $this->root . '/' . $filename;
'filename: file %s does not exist in the fallback paths %s.',
$this->halt(sprintf('filename: file %s does not exist.', $filename));
* Tells you whether a filename is absolute or relative
* @param string $filename Filename to check
* @return boolean true if the filename is absolute
function _isAbsolute ($filename)
if (substr($filename, 0 , 1 ) == '/') {
} else if (substr($filename, 1 , 2 ) == ':\\') {
* Protect a replacement variable
* @param string $varname name of replacement variable
* @return string replaced variable
function _varname ($varname)
return '{' . $varname . '}';
* load file defined by handle if it is not loaded yet
* @param string $handle File handle
* @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 $msg message to show
$this->_lastError = $msg;
* printf error message to show
* @param string $msg message to show
* @return object PEAR error object
return PEAR ::raiseError (sprintf('<b>Template Error:</b> %s<br>'
* Returns the last error message if any
* @return boolean|stringLast error message if any
if ($this->_lastError == '') {
return $this->_lastError;
* Backwards-compatibility for HTML_Template_PHPLIB.
* Used to have this name here.
* @package HTML_Template_PHPLIB
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: PHPLIB.php,v 1.4 2007/10/01 15:12:07 cweiske Exp $
* @link http://pear.php.net/package/HTML_Template_PHPLIB
Documentation generated on Mon, 11 Mar 2019 15:10:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|