?? template.php
字號:
<?php/* * Session Management for PHP3 * * (C) Copyright 1999-2000 NetUSE GmbH * Kristian Koehntopp * * $Id: template.inc,v 1.12 2002/07/11 22:29:51 richardarcher Exp $ * *//* * Change log since version 7.2c * * Bug fixes to version 7.2c compiled by Richard Archer <rha@juggernaut.com.au>: * (credits given to first person to post a diff to phplib mailing list) * * Normalised all comments and whitespace (rha) * replaced "$handle" with "$varname" and "$h" with "$v" throughout (from phplib-devel) * added braces around all one-line if statements in: get_undefined, loadfile and halt (rha) * set_var was missing two sets of braces (rha) * added a couple of "return true" statements (rha) * set_unknowns had "keep" as default instead of "remove" (from phplib-devel) * set_file failed to check for empty strings if passed an array of filenames (phplib-devel) * remove @ from call to preg_replace in subst -- report errors if there are any (NickM) * set_block unnecessarily required a newline in the template file (Marc Tardif) * pparse now calls this->finish to replace undefined vars (Layne Weathers) * get_var now checks for unset varnames (NickM & rha) * get_var when passed an array used the array key instead of the value (rha) * get_vars now uses a call to get_var rather than this->varvals to prevent undefined var warning (rha) * in finish, the replacement string referenced an unset variable (rha) * loadfile would try to load a file if the varval had been set to "" (rha) * in get_undefined, only match non-whitespace in variable tags as in finish (Layne Weathers & rha) * more elegant fix to the problem of subst stripping '$n', '\n' and '\\' strings (rha) * * * Changes in functionality which go beyond bug fixes: * * changed debug handling so set, get and internals can be tracked separately (rha) * added debug statements throughout to track most function calls (rha) * debug output contained raw HTML -- is now escaped with htmlentities (rha) * Alter regex in set_block to remove more whitespace around BEGIN/END tags to improve HTML layout (rha) * Add "append" option to set_var, works just like append in parse (dale at linuxwebpro.com, rha) * Altered parse so that append is honored if passed an array (Brian) * Converted comments and documentation to phpdoc style (rha) * Added clear_var to set the value of variables to "" (rha) * Added unset_var to usset variables (rha) * *//** * The template class allows you to keep your HTML code in some external files * which are completely free of PHP code, but contain replacement fields. * The class provides you with functions which can fill in the replacement fields * with arbitrary strings. These strings can become very large, e.g. entire tables. * * Note: If you think that this is like FastTemplates, read carefully. It isn't. * */class Template{ /** * Serialization helper, the name of this class. * * @var string * @access public */ var $classname = "Template"; /** * Determines how much debugging output Template will produce. * This is a bitwise mask of available debug levels: * 0 = no debugging * 1 = debug variable assignments * 2 = debug calls to get variable * 4 = debug internals (outputs all function calls with parameters). * * Note: setting $this->debug = true will enable debugging of variable * assignments only which is the same behaviour as versions up to release 7.2d. * * @var int * @access public */ var $debug = false; /** * The base directory from which template files are loaded. * * @var string * @access private * @see set_root */ var $root = "."; /** * A hash of strings forming a translation table which translates variable names * into names of files containing the variable content. * $file[varname] = "filename"; * * @var array * @access private * @see set_file */ var $file = array(); /** * A hash of strings forming a translation table which translates variable names * into regular expressions for themselves. * $varkeys[varname] = "/varname/" * * @var array * @access private * @see set_var */ var $varkeys = array(); /** * A hash of strings forming a translation table which translates variable names * into values for their respective varkeys. * $varvals[varname] = "value" * * @var array * @access private * @see set_var */ var $varvals = array(); /** * Determines how to output variable tags with no assigned value in templates. * * @var string * @access private * @see set_unknowns */ var $unknowns = "remove"; /** * Determines how Template handles error conditions. * "yes" = the error is reported, then execution is halted * "report" = the error is reported, then execution continues by returning "false" * "no" = errors are silently ignored, and execution resumes reporting "false" * * @var string * @access public * @see halt */ var $halt_on_error = "yes"; /** * The last error message is retained in this variable. * * @var string * @access public * @see halt */ var $last_error = ""; /****************************************************************************** * Class constructor. May be called with two optional parameters. * The first parameter sets the template directory the second parameter * sets the policy regarding handling of unknown variables. * * usage: Template([string $root = "."], [string $unknowns = "remove"]) * * @param $root path to template directory * @param $string what to do with undefined variables * @see set_root * @see set_unknowns * @access public * @return void */ function Template($root = ".", $unknowns = "remove") { if ($this->debug & 4) { echo "<p><b>Template:</b> root = $root, unknowns = $unknowns</p>\n"; } $this->set_root($root); $this->set_unknowns($unknowns); } /****************************************************************************** * Checks that $root is a valid directory and if so sets this directory as the * base directory from which templates are loaded by storing the value in * $this->root. Relative filenames are prepended with the path in $this->root. * * Returns true on success, false on error. * * usage: set_root(string $root) * * @param $root string containing new template directory * @see root * @access public * @return boolean */ function set_root($root) { if ($this->debug & 4) { echo "<p><b>set_root:</b> root = $root</p>\n"; } if (!is_dir($root)) { $this->halt("set_root: $root is not a directory."); return false; } $this->root = $root; return true; } /****************************************************************************** * Sets the policy for dealing with unresolved variable names. * * unknowns defines what to do with undefined template variables * "remove" = remove undefined variables * "comment" = replace undefined variables with comments * "keep" = keep undefined variables * * Note: "comment" can cause unexpected results when the variable tag is embedded * inside an HTML tag, for example a tag which is expected to be replaced with a URL. * * usage: set_unknowns(string $unknowns) * * @param $unknowns new value for unknowns * @see unknowns * @access public * @return void */ function set_unknowns($unknowns = "remove") { if ($this->debug & 4) { echo "<p><b>unknowns:</b> unknowns = $unknowns</p>\n"; } $this->unknowns = $unknowns; } /****************************************************************************** * Defines a filename for the initial value of a variable. * * It may be passed either a varname and a file name as two strings or * a hash of strings with the key being the varname and the value * being the file name. * * The new mappings are stored in the array $this->file. * The files are not loaded yet, but only when needed. * * Returns true on success, false on error. * * usage: set_file(array $filelist = (string $varname => string $filename)) * or * usage: set_file(string $varname, string $filename) * * @param $varname either a string containing a varname or a hash of varname/file name pairs. * @param $filename if varname is a string this is the filename otherwise filename is not required * @access public * @return boolean */ function set_file($varname, $filename = "") { if (!is_array($varname)) { if ($this->debug & 4) { echo "<p><b>set_file:</b> (with scalar) varname = $varname, filename = $filename</p>\n"; } if ($filename == "") { $this->halt("set_file: For varname $varname filename is empty."); return false; } $this->file[$varname] = $this->filename($filename); } else { reset($varname); while(list($v, $f) = each($varname)) { if ($this->debug & 4) { echo "<p><b>set_file:</b> (with array) varname = $v, filename = $f</p>\n"; } if ($f == "") { $this->halt("set_file: For varname $v filename is empty."); return false; } $this->file[$v] = $this->filename($f); } } return true; } /****************************************************************************** * A variable $parent may contain a variable block defined by: * <!-- BEGIN $varname --> content <!-- END $varname -->. This function removes * that block from $parent and replaces it with a variable reference named $name. * The block is inserted into the varkeys and varvals hashes. If $name is * omitted, it is assumed to be the same as $varname. * * Blocks may be nested but care must be taken to extract the blocks in order * from the innermost block to the outermost block. * * Returns true on success, false on error. * * usage: set_block(string $parent, string $varname, [string $name = ""]) * * @param $parent a string containing the name of the parent variable * @param $varname a string containing the name of the block to be extracted * @param $name the name of the variable in which to store the block * @access public * @return boolean */ function set_block($parent, $varname, $name = "") { if ($this->debug & 4) { echo "<p><b>set_block:</b> parent = $parent, varname = $varname, name = $name</p>\n"; } if (!$this->loadfile($parent)) { $this->halt("set_block: unable to load $parent."); return false; } if ($name == "") { $name = $varname; } $str = $this->get_var($parent); $reg = "/[ \t]*<!--\s+BEGIN $varname\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $varname\s+-->\s*?\n?/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str); $this->set_var($varname, $m[1][0]); $this->set_var($parent, $str); return true;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -