📄 template.php
字号:
} reset($this->varkeys); while(list($k, $v) = each($this->varkeys)) { $result[$k] = $this->get_var($k); } return $result; } /****************************************************************************** * This function returns the value of the variable named by $varname. * If $varname references a file and that file has not been loaded yet, the * variable will be reported as empty. * * When called with an array of variable names this function will return a a * hash of variable values keyed by their names. * * Returns: a string or an array containing the value of $varname. * * usage: get_var(string $varname) * or * usage: get_var(array $varname) * * @param $varname if a string, the name the name of the variable to get the value of, or if an array a list of variables to return the value of * @access public * @return string or array */ function get_var($varname) { if (!is_array($varname)) { if (isset($this->varvals[$varname])) { $str = $this->varvals[$varname]; } else { $str = ""; } if ($this->debug & 2) { printf ("<b>get_var</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varname, htmlentities($str)); } return $str; } else { reset($varname); while(list($k, $v) = each($varname)) { if (isset($this->varvals[$v])) { $str = $this->varvals[$v]; } else { $str = ""; } if ($this->debug & 2) { printf ("<b>get_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $v, htmlentities($str)); } $result[$v] = $str; } return $result; } } /****************************************************************************** * This function returns a hash of unresolved variable names in $varname, keyed * by their names (that is, the hash has the form $a[$name] = $name). * * Returns: a hash of varname/varname pairs or false on error. * * usage: get_undefined(string $varname) * * @param $varname a string containing the name the name of the variable to scan for unresolved variables * @access public * @return array */ function get_undefined($varname) { if ($this->debug & 4) { echo "<p><b>get_undefined:</b> varname = $varname</p>\n"; } if (!$this->loadfile($varname)) { $this->halt("get_undefined: unable to load $varname."); return false; } preg_match_all("/{([^ \t\r\n}]+)}/", $this->get_var($varname), $m); $m = $m[1]; if (!is_array($m)) { return false; } reset($m); while(list($k, $v) = each($m)) { if (!isset($this->varkeys[$v])) { if ($this->debug & 4) { echo "<p><b>get_undefined:</b> undefined: $v</p>\n"; } $result[$v] = $v; } } if (count($result)) { return $result; } else { return false; } } /****************************************************************************** * This function returns the finished version of $str. That is, the policy * regarding unresolved variable names will be applied to $str. * * Returns: a finished string derived from $str and $this->unknowns. * * usage: finish(string $str) * * @param $str a string to which to apply the unresolved variable policy * @access public * @return string * @see set_unknowns */ function finish($str) { switch ($this->unknowns) { case "keep": break; case "remove": $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str); break; case "comment": $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template variable \\1 undefined -->", $str); break; } return $str; } /****************************************************************************** * This function prints the finished version of the value of the variable named * by $varname. That is, the policy regarding unresolved variable names will be * applied to the variable $varname then it will be printed. * * usage: p(string $varname) * * @param $varname a string containing the name of the variable to finish and print * @access public * @return void * @see set_unknowns * @see finish */ function p($varname) { print $this->finish($this->get_var($varname)); } /****************************************************************************** * This function returns the finished version of the value of the variable named * by $varname. That is, the policy regarding unresolved variable names will be * applied to the variable $varname and the result returned. * * Returns: a finished string derived from the variable $varname. * * usage: get(string $varname) * * @param $varname a string containing the name of the variable to finish * @access public * @return void * @see set_unknowns * @see finish */ function get($varname) { return $this->finish($this->get_var($varname)); } /****************************************************************************** * When called with a relative pathname, this function will return the pathname * with $this->root prepended. Absolute pathnames are returned unchanged. * * Returns: a string containing an absolute pathname. * * usage: filename(string $filename) * * @param $filename a string containing a filename * @access private * @return string * @see set_root */ function filename($filename) { if ($this->debug & 4) { echo "<p><b>filename:</b> filename = $filename</p>\n"; } if (substr($filename, 0, 1) != "/") { $filename = $this->root."/".$filename; } if (!file_exists($filename)) { $this->halt("filename: file $filename does not exist."); } return $filename; } /****************************************************************************** * This function will construct a regexp for a given variable name with any * special chars quoted. * * Returns: a string containing an escaped variable name. * * usage: varname(string $varname) * * @param $varname a string containing a variable name * @access private * @return string */ function varname($varname) { return preg_quote("{".$varname."}"); } /****************************************************************************** * If a variable's value is undefined and the variable has a filename stored in * $this->file[$varname] then the backing file will be loaded and the file's * contents will be assigned as the variable's value. * * Note that the behaviour of this function changed slightly after the 7.2d * release. Where previously a variable was reloaded from file if the value * was empty, now this is not done. This allows a variable to be loaded then * set to "", and also prevents attempts to load empty variables. Files are * now only loaded if $this->varvals[$varname] is unset. * * Returns: true on success, false on error. * * usage: loadfile(string $varname) * * @param $varname a string containing the name of a variable to load * @access private * @return boolean * @see set_file */ function loadfile($varname) { if ($this->debug & 4) { echo "<p><b>loadfile:</b> varname = $varname</p>\n"; } if (!isset($this->file[$varname])) { // $varname does not reference a file so return if ($this->debug & 4) { echo "<p><b>loadfile:</b> varname $varname does not reference a file</p>\n"; } return true; } if (isset($this->varvals[$varname])) { // will only be unset if varname was created with set_file and has never been loaded // $varname has already been loaded so return if ($this->debug & 4) { echo "<p><b>loadfile:</b> varname $varname is already loaded</p>\n"; } return true; } $filename = $this->file[$varname]; /* use @file here to avoid leaking filesystem information if there is an error */ $str = implode("", @file($filename)); if (empty($str)) { $this->halt("loadfile: While loading $varname, $filename does not exist or is empty."); return false; } if ($this->debug & 4) { printf("<b>loadfile:</b> loaded $filename into $varname<br>\n"); } $this->set_var($varname, $str); return true; } /****************************************************************************** * This function is called whenever an error occurs and will handle the error * according to the policy defined in $this->halt_on_error. Additionally the * error message will be saved in $this->last_error. * * Returns: always returns false. * * usage: halt(string $msg) * * @param $msg a string containing an error message * @access private * @return void * @see $halt_on_error */ function halt($msg) { $this->last_error = $msg; if ($this->halt_on_error != "no") { $this->haltmsg($msg); } if ($this->halt_on_error == "yes") { die("<b>Halted.</b>"); } return false; } /****************************************************************************** * This function prints an error message. * It can be overridden by your subclass of Template. It will be called with an * error message to display. * * usage: haltmsg(string $msg) * * @param $msg a string containing the error message to display * @access public * @return void * @see halt */ function haltmsg($msg) { printf("<b>Template Error:</b> %s<br>\n", $msg); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -