| 一个易用的模板类 |
|
来源:互联网 |
时间:2006-03-08 |
浏览:
相关评论 |
报告错误 |
发布文章
|
【字号:大 | 中 | 小】
【背景色
】
|
|
($i, $h) = each($handle)) { $str = $this->subst($h); $this->set_var($target, $str); } }
return $str; }
function pparse($target, $handle, $append = false) { echo $this->parse($target, $handle, $append); return false; }
/* public: get_vars()*/ function get_vars() { reset($this->varkeys); while(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(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; }
preg_match_all("/{([^}] )}/", $this->get_var($handle), $m); $m = $m[1]; if (!is_array($m)) return false;
reset($m); while(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('/{[^ trn}] }/', "", $str); break; case "comment": $str = preg_replace('/{([^ trn}] )}/', "<!-- Template $handle: Variable color=#0000cc>set_var($target, $this->get_var($target) . $str); } else { $this->set_var($target, $str); } } else { reset($handle); while(list($i, $h) = each($handle)) { $str = $this->subst($h); $this->set_var($target, $str); } }
return $str; }
function pparse($target, $handle, $append = false) { echo $this->parse($target, $handle, $append); return false; }
/* public: get_vars()*/ function get_vars() { reset($this->varkeys); while(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(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; }
preg_match_all("/{([^}] )}/", $this->get_var($handle), $m); $m = $m[1]; if (!is_array($m)) return false;
reset($m); while(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('/{[^ trn}] }/', "", $str); break; case "comment": $str = preg_replace('/{([^ trn}] )}/', "<!-- 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 is not 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 is empty."); return false; }
$this->set_var($handle, $str);
return true; }
/***************************************************************************/ /* public: halt(string $msg) * msg: error message to show. */ 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 message to show. */ function haltmsg($msg) { printf("<b>Template Error:</b> %s<br>n", $msg); } }#end Template_base class
//类名:Template //功能:模板处理扩展 //说明:继承于Tempalte_base,修改了部分的模板处理函数 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; }
function OP() { $copyright = '<p align=center>© 2002 new Maya workroom</p>'; $this->SetVar('copyright',$copyright); $this->Compile(); $this->OutPut(); return true; }
//example: var or array // $key = array( // 'row1' => '序号', // 'row2' => '姓名', // 'row3' => '性别' // ) function SetVar($key,$value = '') { $this->set_var($key,$value); return true; }
// $key is define the current block function SetBlock($blockname) { $this->handelkey[$blockname] = $this->handelcount; $this->set_block('ihtml',$blockname,$this->handelcount);
$this->handelcount ; return true; }
|
| 分页 [1] [2] [3] [4] [5] [6] [7] [8] |
|
 |
| |
|
|
|
|