PHP Class Library: template.h.php
Operating Environment: PHP4.0
Modification Date: 2002-08-01
Last modified: Stangly.wrong
Introduction to the Class Library: template extraction Display class
/////////////////////////////////////////////////////////////
Class Name: Template base class Template_base
Function: Template specific operation class.
Class Template_base {
var $classname = "Template_base";
/* If set, echo assignments * *
var $debug = false;
/* Public:set_var (array $values)
* Values:array of variable name, value pairs.
*
* Public:set_var (String $varname, String $value)
* Varname:name of a variable which is defined
* Value:value of that variable
*/
function Set_var ($varname, $value = "") {
if (!is_array ($varname)) {
if (!empty ($varname))
if ($this->debug) echo "Scalar:set * $varname * to * $value *<br>\n";
$this->varkeys[$varname] = "/". $this->varname ($varname). " /";
$this->varvals[$varname] = $value;
} else {
Reset ($varname);
while (the list ($k, $v) = each ($varname)) {
if (!empty ($k))
if ($this->debug) echo "Array:set * $k * to * $v *<br>\n";
$this->varkeys[$k] = "/". $this->varname ($k). " /";
$this->varvals[$k] = $v;
}
}
}
/* PUBLIC:SUBST (String $handle)
* Handle:handle of template where variables are to be substituted.
*/
function subst ($handle) {
if (! $this->loadfile ($handle)) {
$this->halt ("Subst:unable to load $handle.");
return false;
}
/* PUBLIC:PSUBST (String $handle)
* Handle:handle of template where variables are to be substituted.
*/
function Psubst ($handle) {
echo $this->subst ($handle);
return false;
}
/* Public:parse (String $target, String $handle, Boolean append)
* Public:parse (string $target, Array $handle, Boolean append)
* Target:handle of variable to generate
* Handle:handle of template to substitute
* Append:append to target handle
*/
Function Parse ($target, $handle, $append = False) {
if (!is_array ($handle)) {
$str = $this->subst ($handle);
if ($append) {
$this->set_var ($target, $this->get_var ($target). $str);
} else {
$this->set_var ($target, $STR);
}
} else {
Reset ($handle);
while (the list ($i, $h) = each ($handle)) {
$str = $this->subst ($h);
$this->set_var ($target, $STR);
}
}
/* Public:get_vars () * *
function Get_vars () {
Reset ($this->varkeys);
while (the list ($k, $v) = each ($this->varkeys)) {
$result [$k] = $this->varvals[$k];
}
return $result;
}
/* Public:get_var (String varname)
* Varname:name of variable.
*
* Public:get_var (array varname)
* Varname:array of variable names
*/
function Get_var ($varname) {
if (!is_array ($varname)) {
return $this->varvals[$varname];
} else {
Reset ($varname);
while (the list ($k, $v) = each ($varname)) {
$result [$k] = $this->varvals[$k];
}
return $result;
}
}
/* public:get_undefined ($handle)
* Handle:handle of a template.
*/
function get_undefined ($handle) {
if (! $this->loadfile ($handle)) {
$this->halt ("Get_undefined:unable to load $handle.");
return false;
}
Reset ($m);
while (the list ($k, $v) = each ($m)) {
if (!isset ($this->varkeys[$v]))
$result [$v] = $v;
}
if (count ($result))
return $result;
Else
return false;
}
/* Public:finish (String $str)
* Str:string to finish.
*/
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 $handle: Variable \1 undefined-->", $str);
Break
}
return $str;
}
/* PUBLIC:P (String $varname)
* Varname:name of variable to print.
*/
function P ($varname) {
echo $this->finish ($this->get_var ($varname));
}
function Get ($varname) {
return $this->finish ($this->get_var ($varname));
}
/***************************************************************************/
/* Private:filename ($filename)
* Filename:name to be completed.
*/
function filename ($filename) {
if (substr ($filename, 0, 1)!= "/") {
$filename = $this->root. " /". $filename;
}
if (!file_exists ($filename))
$this->halt ("Filename:file $filename does not exist.");
return $filename;
}
/* Private:varname ($varname)
* Varname:name of a replacement variable to be protected.
*/
function VarName ($varname) {
Return Preg_quote ("{". $varname. "}");
}
/* Private:loadfile (String $handle)
* handle:load file defined by handle, if it isn't loaded yet.
*/
function LoadFile ($handle) {
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];
$str = Implode ("", @file ($filename));
if (empty ($STR)) {
$this->halt ("Loadfile:while loading $handle, $filename does not exist or are empty.");
return false;
}
$this->set_var ($handle, $STR);
return true;
}
/***************************************************************************/
/* Public:halt (String $msg)
* Msg: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;
}
/* Public, override:haltmsg ($msg)
* Msg:error.
*/
function Haltmsg ($msg) {
printf ("<b>template error:</b>%s<br>\n", $msg);
}
} #end Template_base Class
Class Name: Template
Features: Template processing extensions
Description: Inherits from Tempalte_base, modifies part of the template handler function
Class Template extends Template_base {
var $handelkey = array ();
var $handelcount;
function Template ($filename) {
$this->template_base ();
if (Empty ($filename) | |!file_exists ($filename)) {
Die ("Template-> Template (): Error-file $filename does not exist");
}
$this->set_file (' ihtml ', $filename);
$this->handelcount = 1;
return true;
}
function Output () {
$this->p (' out ');
return true;
}
function Compile () {
$this->parse (' Out ', ' ihtml ');
return true;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.