| 一个易用的模板类 |
|
来源:互联网 |
时间:2006-03-08 |
浏览:
相关评论 |
报告错误 |
发布文章
|
【字号:大 | 中 | 小】
【背景色
】
|
|
c>$parent)) {
$this
>->halt("subst: unable to load $parent."); return false; } if ($name == "") $name = $handle;
$str = $this->get_var($parent); $reg = "/<!--s BEGIN $handles -->(.*)ns*<!--s END $handles -->/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str); $this->set_var($handle, $m[1][0]); $this->set_var($parent, $str); }
/* public: set_var(array $values) * values: array of variable name, value pairs. * * public: set_var(string $varname, string $value) * varname: name of a variable that is to be 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(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; }
$str = $this->get_var($handle); $str = @preg_replace($this->varkeys, $this->varvals, $str); return $str; }
/* 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(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 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<
|
| 分页 [1] [2] [3] [4] [5] [6] [7] [8] |
|
 |
| |
|
|
|
|