2
// vim: set expandtab tabstop=4 shiftwidth=4:
3
// This code that was derived from the original PHPLIB Template class
4
// is copyright by Kristian Koehntopp, NetUSE AG and was released
7
// Authors: Kristian Koehntopp <kris@koehntopp.de> (original from PHPLIB)
8
// Bjoern Schotte <bjoern@rent-a-phpwizard.de> (PEARification)
9
// Martin Jansen <mj@php.net> (PEAR conformance)
11
// $Id: PHPLIB.php,v 1.3 2006/09/17 06:22:04 wurley Exp $
14
//require_once "PEAR.php";
17
* Converted PHPLIB Template class
19
* For those who want to use PHPLIB's fine template class,
20
* here's a PEAR conforming class with the original PHPLIB
21
* template code from phplib-stable CVS. Original author
22
* was Kristian Koehntopp <kris@koehntopp.de>
24
* @author Bjoern Schotte <bjoern@rent-a-phpwizard.de>
25
* @author Martin Jansen <mj@php.net> (PEAR conformance)
31
* If set, echo assignments
34
public $debug = false;
37
* $file[handle] = "filename";
40
public $file = array();
43
* fallback paths that should be defined in a child class
46
public $file_fallbacks = array();
49
* Relative filenames are relative to this pathname
55
* $_varKeys[key] = "key"
58
public $_varKeys = array();
61
* $_varVals[key] = "value";
64
public $_varVals = array();
67
* "remove" => remove undefined variables
68
* "comment" => replace undefined variables with comments
69
* "keep" => keep undefined variables
72
public $unknowns = "remove";
75
* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly
78
public $haltOnError = "report";
81
* The last error message is retained here
85
public $_lastError = "";
92
* @param string template root directory
93
* @param string how to handle unknown variables
94
* @param array fallback paths
96
function Template_PHPLIB($root = ".", $unknowns = "remove", $fallback="")
98
$this->setRoot($root);
99
$this->setUnknowns($unknowns);
100
if (is_array($fallback)) $this->file_fallbacks = $fallback;
104
* Sets the template directory
107
* @param string new template directory
110
function setRoot($root)
112
if (!is_dir($root)) {
113
$this->halt("setRoot: $root is not a directory.");
123
* What to do with unknown variables
125
* three possible values:
127
* - "remove" will remove unknown variables
128
* (don't use this if you define CSS in your page)
129
* - "comment" will replace undefined variables with comments
130
* - "keep" will keep undefined variables as-is
133
* @param string unknowns
135
function setUnknowns($unknowns = "keep")
137
$this->unknowns = $unknowns;
141
* Set appropriate template files
143
* With this method you set the template files you want to use.
144
* Either you supply an associative array with key/value pairs
145
* where the key is the handle for the filname and the value
146
* is the filename itself, or you define $handle as the file name
147
* handle and $filename as the filename if you want to define only
151
* @param mixed handle for a filename or array with handle/name value pairs
152
* @param string name of template file
155
function setFile($handle, $filename = "")
157
if (!is_array($handle)) {
159
if ($filename == "") {
160
$this->halt("setFile: For handle $handle filename is empty.");
164
$this->file[$handle] = $this->_filename($filename);
169
while (list($h, $f) = each($handle)) {
170
$this->file[$h] = $this->_filename($f);
176
* Set a block in the appropriate template handle
178
* By setting a block like that:
180
* <!-- BEGIN blockname -->
182
* <!-- END blockname -->
184
* you can easily do repeating HTML code, i.e. output
185
* database data nice formatted into a HTML table where
186
* each DB row is placed into a HTML table row which is
187
* defined in this block.
188
* It extracts the template $handle from $parent and places
189
* variable {$name} instead.
192
* @param string parent handle
193
* @param string block name handle
194
* @param string variable substitution name
196
function setBlock($parent, $handle, $name = "")
198
if (!$this->_loadFile($parent)) {
199
$this->halt("setBlock: unable to load $parent.");
207
$str = $this->getVar($parent);
208
$reg = "/[ \t]*<!--\s+BEGIN $handle\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $handle\s+-->\s*?\n?/sm";
209
preg_match_all($reg, $str, $m);
210
$str = preg_replace($reg, "{" . "$name}", $str);
212
if (isset($m[1][0])) $this->setVar($handle, $m[1][0]);
213
$this->setVar($parent, $str);
217
* Set corresponding substitutions for placeholders
220
* @param string name of a variable that is to be defined or an array of variables with value substitution as key/value pairs
221
* @param string value of that variable
222
* @param boolean if true, the value is appended to the variable's existing value
224
function setVar($varname, $value = "", $append = false)
226
if (!is_array($varname)) {
228
if (!empty($varname))
229
if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n";
231
$this->_varKeys[$varname] = $this->_varname($varname);
232
($append) ? $this->_varVals[$varname] .= $value : $this->_varVals[$varname] = $value;
237
while (list($k, $v) = each($varname)) {
239
if ($this->debug) print "array: set *$k* to *$v*<br>\n";
241
$this->_varKeys[$k] = $this->_varname($k);
242
($append) ? $this->_varVals[$k] .= $v : $this->_varVals[$k] = $v;
248
* Substitute variables in handle $handle
251
* @param string name of handle
252
* @return mixed string substituted content of handle
254
function subst($handle)
256
if (!$this->_loadFile($handle)) {
257
$this->halt("subst: unable to load $handle.");
261
return @str_replace($this->_varKeys, $this->_varVals, $this->getVar($handle));
265
* Same as subst but printing the result
269
* @param string handle of template
270
* @return bool always false
272
function pSubst($handle)
274
print $this->subst($handle);
279
* Parse handle into target
281
* Parses handle $handle into $target, eventually
282
* appending handle at $target if $append is defined
286
* @param string target handle to parse into
287
* @param string which handle should be parsed
288
* @param boolean append it to $target or not?
289
* @return string parsed handle
291
function parse($target, $handle, $append = false)
293
if (!is_array($handle)) {
294
$str = $this->subst($handle);
296
($append) ? $this->setVar($target, $this->getVar($target) . $str) : $this->setVar($target, $str);
300
while (list(, $h) = each($handle)) {
301
$str = $this->subst($h);
302
$this->setVar($target, $str);
310
* Same as parse, but printing it.
314
* @param string target to parse into
315
* @param string handle which should be parsed
316
* @param should $handle be appended to $target?
319
function pParse($target, $handle, $append = false)
321
print $this->finish($this->parse($target, $handle, $append));
326
* Return all defined variables and their values
329
* @return array with all defined variables and their values
333
reset($this->_varKeys);
335
while (list($k, ) = each($this->_varKeys)) {
336
$result[$k] = $this->getVar($k);
343
* Return one or more specific variable(s) with their values.
346
* @param mixed array with variable names or one variable name as a string
347
* @return mixed array of variable names with their values or value of one specific variable
349
function getVar($varname)
351
if (!is_array($varname)) {
352
if (isset($this->_varVals[$varname])) {
353
return $this->_varVals[$varname];
360
while (list($k, ) = each($varname)) {
361
$result[$k] = (isset($this->_varVals[$k])) ? $this->_varVals[$k] : "";
369
* Get undefined values of a handle
372
* @param string handle name
373
* @return mixed false if an error occured or the undefined values
375
function getUndefined($handle)
377
if (!$this->_loadFile($handle)) {
378
$this->halt("getUndefined: unable to load $handle.");
382
preg_match_all("/{([^ \t\r\n}]+)}/", $this->getVar($handle), $m);
389
while (list(, $v) = each($m)) {
390
if (!isset($this->_varKeys[$v])) {
395
if (isset($result) && count($result)) {
406
* @param string string to finish
407
* @return finished, i.e. substituted string
409
function finish($str)
411
switch ($this->unknowns) {
413
$str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);
417
$str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
425
* Print variable to the browser
428
* @param string name of variable to print
432
print $this->finish($this->getVar($varname));
436
* Get finished variable
438
* @access public public
439
* @param string variable to get
440
* @return string string with finished variable
442
function get($varname)
444
return $this->finish($this->getVar($varname));
450
* Complete filename, i.e. testing it for slashes
453
* @param string filename to be completed
454
* @return string completed filename
456
function _filename($filename)
458
// if (substr($filename, 0, 1) != "/") {
459
// $filename = $this->root."/".$filename;
462
if (file_exists($filename)) return $filename;
463
if (is_array($this->file_fallbacks) && count($this->file_fallbacks) > 0) {
464
reset($this->file_fallbacks);
465
while (list(,$v) = each($this->file_fallbacks)) {
466
if (file_exists($v.basename($filename))) return $v.basename($filename);
468
$this->halt(sprintf("filename: file %s does not exist in the fallback paths %s.",$filename,implode(",",$this->file_fallbacks)));
471
$this->halt(sprintf("filename: file %s does not exist.",$filename));
479
* Protect a replacement variable
482
* @param string name of replacement variable
483
* @return string replaced variable
485
function _varname($varname)
487
return "{".$varname."}";
491
* load file defined by handle if it is not loaded yet
494
* @param string handle
495
* @return bool FALSE if error, true if all is ok
497
function _loadFile($handle)
499
if (isset($this->_varKeys[$handle]) and !empty($this->_varVals[$handle])) {
503
if (!isset($this->file[$handle])) {
504
$this->halt("loadfile: $handle is not a valid handle.");
508
$filename = $this->file[$handle];
509
if (function_exists("file_get_contents")) {
510
$str = file_get_contents($filename);
512
if (!$fp = @fopen($filename,"r")) {
513
$this->halt("loadfile: couldn't open $filename");
517
$str = fread($fp,filesize($filename));
522
$this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
526
$this->setVar($handle, $str);
532
* Error function. Halt template system with message to show
535
* @param string message to show
540
$this->_lastError = $msg;
542
if ($this->haltOnError != "no") {
543
// return $this->haltMsg($msg);
544
$this->haltMsg($msg);
547
if ($this->haltOnError == "yes") {
548
die("<b>Halted.</b>");
555
* printf error message to show
558
* @param string message to show
559
* @return object PEAR error object
561
function haltMsg($msg)
563
// PEAR::raiseError(sprintf("<b>Template Error:</b> %s<br>\n", $msg));
564
printf("<b>Template Error:</b> %s<br>\n", $msg);