收 藏 投 稿 繁 体 RSS 
站长吧-中国站长学习与交流的网站
首 页 运 营 学 院 建 站 论 坛
Web master8.net  
 网站运营  
  欢迎在本站发布信息,在线投递稿件请点这里。编辑QQ:4908220,欢迎联系交流。
业界动态 创业故事 推广研究 策划盈利 电子商务 企业平台
  站长工具
SEO查询 Whois查询 Pr查询 域名查询 IP查询 网页编辑器
 建站服务  
  如有建站意向,请尽快联系我们,以便安排时间... 建站服务 QQ4908220 QQ:4908220
作品展示 服务范围 服务流程 服务报价 联系方式 付款方式
文章正文  » 您的当前位置: 首页 >> 学院 >> 程序开发 >> PHP
一个易用的模板类
  来源:互联网 | 时间: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]
master8
  • 上一篇:大家关心的问题,开发短信程序(java)
  • 下一篇:Skype出台海外赔偿方案

  • 我要投稿  打印本文  推荐本文  加入收藏  返回顶部  关闭窗口
    搜模板(www.somoban.com) 原创网站模板交易平台
    阿里妈妈再掀疯狂采购风,网站广告位严重告急,急召天下站长
    基于PHP+MySQL的整站、模块、插件开发等或者按需求实现相应功能;
基于各PHP主流建站系统CMS,BBS,BLOG等的模板定制,完全手写代码;
整站数据迁移或备份恢复;网页代码优化、重构;整站常规SEO优化;网站技术支持;
点击了解详情...
    站长论坛
    • 验证码: