📄 04-template.sgml
字号:
<!-- $Id: 04-template.sgml,v 1.1.1.1 2000/04/17 16:40:04 kk Exp $ --><sect1>Template<p><em/Note:/ If you think that this is like FastTemplates, readcarefully. It isn't.<p> The template class allows you to keep your HTML code in someexternal files which are completely free of PHP code, butcontain replacement fields. The class provides you withfunctions which can fill in the replacement fields witharbitrary strings. These strings can become very large, e.g.entire tables.<sect2>Instance variables<p><table><tabular ca="">classname<colsep>String. Serialization helper: The name of this class.<rowsep>debug<colsep>Boolean: if set to true, the class will emitdebugging output.<rowsep>unknowns<colsep>One of "keep", "comment", "remove" (Default).Determines how to handle unresolved variable names intemplates upon output. If set to "keep", those are leftuntouched. If set to "comment", unresolved variable names aretransformed into HTML comments reporting the error. If set to"remove", unresolved variable names are silently removed (thedefault).<rowsep>halt_on_error = "yes"<colsep>One of "yes"(Default), "report", "no". Determines how Template handleserror conditions. If set to "yes" (the Default), the error isreported, then execution is halted. If set to "report", theerror is reported, then execution continues by returning"false". If set to "no", errors are silently ignored, andexecution resumes reporting "false".<rowsep>last_error = ""<colsep>The last error message iskept in this variable.<rowsep></tabular><caption>Accessible instance variables.</caption></table><table><tabular ca="">file<colsep>Hash of strings. A translation table whichtranslates variable names into filenames.<rowsep>root<colsep>String (Pathname). The base directory from whichtemplate files are being loaded.<rowsep>varkeys<colsep>Hash of strings. A translation table whichtranslates variable names into regular expressions forthemselves.<rowsep>varvals<colsep>Hash of strings. A translation table whichtranslates variable names into replacement values for theirrespective varkeys.<rowsep></tabular><caption>Internal instance variables.</caption></table><sect2>Instance methods<p><sect3>Accessible instance methods<p><descrip><tag>Template($root = ".", $unknowns = "remove")</tag><p>Constructor. May be called with two optional parameters. Thefirst parameter sets the template directory (see<tt/set_root()/, the second parameter sets the policyregarding handling of unknown variables.<tag>set_root($root)</tag><p>The function checks that $root is a valid directory and setsthis directory as the base directory where templates are beingstored.<tag>set_unknowns($unknowns = "keep")</tag><p>The function sets the policy for dealing with unresolvedvariable names. Must be either "remove", "comment" or "keep". Ifset to "keep", those are left untouched. If set to "comment",unresolved variable names are transformed into HTML commentsreporting the error. If set to "remove", unresolved variablenames are silently removed (the default).<tag>set_file($handle, $filename = "")</tag><p>The function defines a filename for the initial value of avariable. It may be called with either a $handle/$filename pairor with a hash of $handle/$filename pairs. The files are notreferenced yet, but only when needed.<tag>set_block($parent, $handle, $name = "")</tag><p>A variable $parent may contain a variable block named by$handle. The function removes that block from $parent andreplaces it with a variable reference named $name. If $name isomitted, it is assumed to be the same as $handle.<tag>set_var($varname, $value = "")</tag><p>The functions sets the inital value of a variable. It may becalled with either a $varname/$value pair or with a hash of$varname/$value pairs.<tag>subst($handle)</tag><p>The function returns the value of the variable named $handle,with all defined variable values filled in. The resulting stringis not "finished", that is, the unresolved variable name policyhas not been applied yet.<tag>psubst($handle)</tag><p>This is a shorthand for <tt/print $this->subst($handle)/.<tag>parse($target, $handle, $append = false)</tag><p>The function substitutes the values of all defined variables inthe variable named $handle and stores or appends the result inthe variable named $target.If $handle is an array of variable names, $append is ignored.The variables named by $handle are being sequentiallysubstituted and the result of each substitution step is storedin $target. The resulting substitution is available in thevariable named by $target, as is each intermediate step for thenext $handle in sequence.<tag>pparse($target, $handle, $append = false)</tag><p>A shorthand for <tt/print $this->parse(...)/.<tag>get_vars()</tag><p>Returns a hash of all defined values, keyed by their names.<tag>get_var($varname)</tag><p>Returns the value of the variable named by $varname. If $varnamereferences a file and that file has not been loaded, yet, thevariable will be reported as empty.When called with an array of variable names, an hash of values,keyed by their names, will be returned.<tag>get_undefined($handle)</tag><p>The function will return a hash of unresolved variable names in$handle, keyed by their names (that is, the hash has the form$a[$name] = $name).<tag>finish($str)</tag><p>The function will returned the finished version of $str, thatis, the policy regarding unresolved variable names will beapplied to $str.<tag>p($varname)</tag><p>The function will print the finished version of the value of thevariable named by $varname.<tag>get($varname)</tag><p>The function will return the finished version of the value ofthe variable named by $varname.<tag>haltmsg($msg)</tag><p>This function can be overridden by your subclass of Template. Itwill be called with an error message to print.</descrip><sect3>Internal instance methods<p><descrip><tag>filename($filename)</tag><p>When called with a relative pathname, this function will returnthe pathname with $this->root prepended. Absolute pathnames aretaken unchanged.The resulting filename must exist, or an error is generated.<tag>varname($varname)</tag><p>The function will construct a variable name regexp for a givenvariable name.<tag>loadfile($handle)</tag><p>If a variable is undefined or empty and is backed by a filename,the backing file will be loaded and the files contents will beassigned as the variables value.<tag>halt($msg)</tag><p>This function is called whenever an error occurs and will handlethe error according to the policy defined in$this->halt_on_error.</descrip><sect2>Example<p>The class manages a set of variables which are text strings.These strings may contain references to other variables in theform of "{variable}". When parsed or substituted, a variablereference is being replaced by the value of that variable.A variable value may be defined manually by calling<tt/set_var("name", "value");/ or it may be defined froma file by calling <tt/set_file("name","filename.ihtml");/. In the latter case, the contents of thefile are being loaded when needed (as late as possible) and setas the value of that variable.A third way to define a variable value is to call<tt/set_block("parent", "block", "name");/. In this case,the variable named <tt/parent/ is being searched for a blockthat starts with <tt><!-- BEGIN block --></tt> and endswith <tt><!-- END block --></tt>. This string is removedfrom the variable <tt/parent/ and assigned to the variable named<tt/block/. In <tt/parent/, a variable reference to <tt/name/ isplaced instead. If the optional parameter <tt/"name"/ is leftout, <tt/"block"/ is being used instead.Use <tt/Template/ direcly or define a subclass of <tt/Template/as needed.Define a template file named page.ihtml as follows:<tscreen><code><html> <head><title>{PAGETITLE}</title></head> <body bgcolor="#ffffff"> <table border=1 cellpadding=4 cellspacing=0 bgcolor="#eeeeee"> <tr> <td colspan=2><h1>{PAGETITLE}</h1></td> </tr> <tr> <td>{OUT}</td> <td>Content</td> </tr> </table> </body></html></code></tscreen>This file contains a reference to the variable <tt/pagetitle/and a reference to the variable named <tt/out/. Another template file, named box.ihtml, contains a block named row with threevariable references {TITLE}, {NUM} and {BIGNUM}:<tscreen><code><!-- start box.ihtml --><table border=1 bgcolor="#cccccc" cellpadding=4 cellspacing=0> <tr> <td colspan=2><b>{TITLE}</b></td> </tr> <!-- BEGIN row --> <tr> <td>{NUM}</td> <td>{BIGNUM} </tr> <!-- END row --></table><!-- end box.ihtml --></code></tscreen>The following php3 file demonstrates how to use these templates:<tscreen><code><?php include("./template.inc"); # create Template instance called $t $t = new Template("/page/to/webserver/template", "keep"); # define variables named page and box, referencing files $t->set_file(array( "page" => "page.ihtml", "box" => "box.ihtml")); # extract the block named "row" from "box", creating a # reference to {rows} in "box". $t->set_block("box", "row", "rows"); # define the variables TITLE and PAGETITLE $t->set_var(array("TITLE" => "Testpage", "PAGETITLE" => "hugo")); # define NUM and BIGNUM, then append "row" to "rows"... for ($i=1; $i<=3; $i++) { $n = $i; $nn = $i*10; $t->set_var(array("NUM" => $n, "BIGNUM" => $nn)); $t->parse("rows", "row", true); } # build out from box, then build out from page... $t->parse("out", array("box", "page")); # finish out and print it. $t->p("out");?><hr><?php # report leftover variables, if any. print implode(", ", $t->get_undefined("rows")); ?></code></tscreen>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -