![]() |
Documentación GVHIDRA 3.1.5
|
Métodos públicos | |
Template_PHPLIB ($root=".", $unknowns="remove", $fallback="") | |
setRoot ($root) | |
setUnknowns ($unknowns="keep") | |
setFile ($handle, $filename="") | |
setBlock ($parent, $handle, $name="") | |
setVar ($varname, $value="", $append=false) | |
subst ($handle) | |
pSubst ($handle) | |
parse ($target, $handle, $append=false) | |
pParse ($target, $handle, $append=false) | |
getVars () | |
getVar ($varname) | |
getUndefined ($handle) | |
finish ($str) | |
p ($varname) | |
get ($varname) | |
_filename ($filename) | |
_varname ($varname) | |
_loadFile ($handle) | |
halt ($msg) | |
haltMsg ($msg) | |
Campos de datos | |
$debug = false | |
$file = array() | |
$file_fallbacks = array() | |
$root = "" | |
$_varKeys = array() | |
$_varVals = array() | |
$unknowns = "remove" | |
$haltOnError = "report" | |
$_lastError = "" |
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>
Definición en la línea 28 del archivo PHPLIB.php.
_filename | ( | $ | filename | ) |
Complete filename
Complete filename, i.e. testing it for slashes
private
string | filename to be completed |
Definición en la línea 456 del archivo PHPLIB.php.
{ // if (substr($filename, 0, 1) != "/") { // $filename = $this->root."/".$filename; // } if (file_exists($filename)) return $filename; if (is_array($this->file_fallbacks) && count($this->file_fallbacks) > 0) { reset($this->file_fallbacks); while (list(,$v) = each($this->file_fallbacks)) { if (file_exists($v.basename($filename))) return $v.basename($filename); } $this->halt(sprintf("filename: file %s does not exist in the fallback paths %s.",$filename,implode(",",$this->file_fallbacks))); return false; } else { $this->halt(sprintf("filename: file %s does not exist.",$filename)); return false; } return $filename; }
_loadFile | ( | $ | handle | ) |
load file defined by handle if it is not loaded yet
private
string | handle |
Definición en la línea 497 del archivo PHPLIB.php.
{ if (isset($this->_varKeys[$handle]) and !empty($this->_varVals[$handle])) { return true; } if (!isset($this->file[$handle])) { $this->halt("loadfile: $handle is not a valid handle."); return false; } $filename = $this->file[$handle]; if (function_exists("file_get_contents")) { $str = file_get_contents($filename); } else { if (!$fp = @fopen($filename,"r")) { $this->halt("loadfile: couldn't open $filename"); return false; } $str = fread($fp,filesize($filename)); fclose($fp); } if ($str=='') { $this->halt("loadfile: While loading $handle, $filename does not exist or is empty."); return false; } $this->setVar($handle, $str); return true; }
_varname | ( | $ | varname | ) |
Protect a replacement variable
private
string | name of replacement variable |
Definición en la línea 485 del archivo PHPLIB.php.
{ return "{".$varname."}"; }
finish | ( | $ | str | ) |
Finish string
public
string | string to finish |
Definición en la línea 409 del archivo PHPLIB.php.
{ switch ($this->unknowns) { case "remove": $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str); break; case "comment": $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str); break; } return $str; }
get | ( | $ | varname | ) |
Get finished variable
public public
string | variable to get |
Definición en la línea 442 del archivo PHPLIB.php.
getUndefined | ( | $ | handle | ) |
Get undefined values of a handle
public
string | handle name |
Definición en la línea 375 del archivo PHPLIB.php.
{ if (!$this->_loadFile($handle)) { $this->halt("getUndefined: unable to load $handle."); return false; } preg_match_all("/{([^ \t\r\n}]+)}/", $this->getVar($handle), $m); $m = $m[1]; if (!is_array($m)) { return false; } reset($m); while (list(, $v) = each($m)) { if (!isset($this->_varKeys[$v])) { $result[$v] = $v; } } if (isset($result) && count($result)) { return $result; } else { return false; } }
getVar | ( | $ | varname | ) |
Return one or more specific variable(s) with their values.
public
mixed | array with variable names or one variable name as a string |
Definición en la línea 349 del archivo PHPLIB.php.
{ if (!is_array($varname)) { if (isset($this->_varVals[$varname])) { return $this->_varVals[$varname]; } else { return ""; } } else { reset($varname); while (list($k, ) = each($varname)) { $result[$k] = (isset($this->_varVals[$k])) ? $this->_varVals[$k] : ""; } return $result; } }
getVars | ( | ) |
Return all defined variables and their values
public
Definición en la línea 331 del archivo PHPLIB.php.
{ reset($this->_varKeys); while (list($k, ) = each($this->_varKeys)) { $result[$k] = $this->getVar($k); } return $result; }
halt | ( | $ | msg | ) |
Error function. Halt template system with message to show
public
string | message to show |
Definición en la línea 538 del archivo PHPLIB.php.
haltMsg | ( | $ | msg | ) |
printf error message to show
public
string | message to show |
Definición en la línea 561 del archivo PHPLIB.php.
{ // PEAR::raiseError(sprintf("<b>Template Error:</b> %s<br>\n", $msg)); printf("<b>Template Error:</b> %s<br>\n", $msg); }
p | ( | $ | varname | ) |
Print variable to the browser
public
string | name of variable to print |
Definición en la línea 430 del archivo PHPLIB.php.
parse | ( | $ | target, |
$ | handle, | ||
$ | append = false |
||
) |
Parse handle into target
Parses handle $handle into $target, eventually appending handle at $target if $append is defined as TRUE.
public
string | target handle to parse into |
string | which handle should be parsed |
boolean | append it to $target or not? |
Definición en la línea 291 del archivo PHPLIB.php.
pParse | ( | $ | target, |
$ | handle, | ||
$ | append = false |
||
) |
Same as parse, but printing it.
public parse
string | target to parse into | |
string | handle which should be parsed | |
should | $handle | be appended to $target? |
Definición en la línea 319 del archivo PHPLIB.php.
pSubst | ( | $ | handle | ) |
Same as subst but printing the result
public subst
string | handle of template |
Definición en la línea 272 del archivo PHPLIB.php.
{ print $this->subst($handle); return false; }
setBlock | ( | $ | parent, |
$ | handle, | ||
$ | name = "" |
||
) |
Set a block in the appropriate template handle
By setting a block like that:
<!-- BEGIN blockname --> html code <!-- 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 defined in this block. It extracts the template $handle from $parent and places variable {$name} instead.
public
string | parent handle |
string | block name handle |
string | variable substitution name |
Definición en la línea 196 del archivo PHPLIB.php.
{ if (!$this->_loadFile($parent)) { $this->halt("setBlock: unable to load $parent."); return false; } if ($name == "") { $name = $handle; } $str = $this->getVar($parent); $reg = "/[ \t]*<!--\s+BEGIN $handle\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $handle\s+-->\s*?\n?/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str); if (isset($m[1][0])) $this->setVar($handle, $m[1][0]); $this->setVar($parent, $str); }
setFile | ( | $ | handle, |
$ | filename = "" |
||
) |
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 one template.
public
mixed | handle for a filename or array with handle/name value pairs |
string | name of template file |
Definición en la línea 155 del archivo PHPLIB.php.
setRoot | ( | $ | root | ) |
Sets the template directory
public
string | new template directory |
Definición en la línea 110 del archivo PHPLIB.php.
setUnknowns | ( | $ | unknowns = "keep" | ) |
What to do with unknown variables
three possible values:
public
string | unknowns |
Definición en la línea 135 del archivo PHPLIB.php.
{ $this->unknowns = $unknowns; }
setVar | ( | $ | varname, |
$ | value = "" , |
||
$ | append = false |
||
) |
Set corresponding substitutions for placeholders
public
string | name of a variable that is to be defined or an array of variables with value substitution as key/value pairs |
string | value of that variable |
boolean | if true, the value is appended to the variable's existing value |
Definición en la línea 224 del archivo PHPLIB.php.
{ if (!is_array($varname)) { if (!empty($varname)) 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; } else { reset($varname); while (list($k, $v) = each($varname)) { if (!empty($k)) 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; } } }
subst | ( | $ | handle | ) |
Substitute variables in handle $handle
public
string | name of handle |
Definición en la línea 254 del archivo PHPLIB.php.
Template_PHPLIB | ( | $ | root = "." , |
$ | unknowns = "remove" , |
||
$ | fallback = "" |
||
) |
Constructor
public
string | template root directory |
string | how to handle unknown variables |
array | fallback paths |
Definición en la línea 96 del archivo PHPLIB.php.
{ $this->setRoot($root); $this->setUnknowns($unknowns); if (is_array($fallback)) $this->file_fallbacks = $fallback; }
$_lastError = "" |
Definición en la línea 85 del archivo PHPLIB.php.
$_varKeys = array() |
Definición en la línea 58 del archivo PHPLIB.php.
$_varVals = array() |
Definición en la línea 64 del archivo PHPLIB.php.
$debug = false |
Definición en la línea 34 del archivo PHPLIB.php.
$file = array() |
Definición en la línea 40 del archivo PHPLIB.php.
$file_fallbacks = array() |
Definición en la línea 46 del archivo PHPLIB.php.
$haltOnError = "report" |
Definición en la línea 78 del archivo PHPLIB.php.
$root = "" |
Definición en la línea 52 del archivo PHPLIB.php.
$unknowns = "remove" |
Definición en la línea 72 del archivo PHPLIB.php.