template = preg_replace("/\{tmptpl\s+(\w+?)\}/is", "<? include \$this->getObj(\"\\1\", \$this->objdir);?>", $template);
$template = preg_replace("/\{tmptpl\s+(.+?)\}/ise", "\$this->stripvtag(''<? include \$this->getObj(\"\\1\", \$this->objdir); ?>'')", $template);
$template = preg_replace("/\{else\}/is", "<? } else { ?>", $template);
$template = preg_replace("/\{\/if\}/is", "<? } ?>", $template);
$template = preg_replace("/\{\/for\}/is", "<? } ?>", $template);
$template = preg_replace("/$const_regexp/", "<?=\\1?>", $template);//note {else} 也符合常量格式,此处要注意先后顺序
$template = preg_replace("/(\\\$[a-zA-Z_]\w+\[)([a-zA-Z_]\w+)\]/i", "\\1''\\2'']", $template);
$fp = fopen($this->objfile, ''w'');
fwrite($fp, $template);
fclose($fp);
}
function arrayindex($name, $items) {
$items = preg_replace("/\[([a-zA-Z_]\w*)\]/is", "[''\\1'']", $items);
return "<?=$name$items?>";
}
function stripvtag($s) {
$vtag_regexp = "\<\?=(\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\''\[\]\$]+\])*)\?\>";
return preg_replace("/$vtag_regexp/is", "\\1", str_replace("\\\"", ''"'', $s));
}
function loopsection($arr, $k, $v, $statement) {
$arr = $this->stripvtag($arr);
$k = $this->stripvtag($k);
$v = $this->stripvtag($v);
$statement = str_replace("\\\"", ''"'', $statement);
return $k ? "<? foreach((array)$arr as $k => $v) {?>$statement<?}?>" : "<? foreach((array)$arr as $v) {?>$statement<? } ?>";
}
function __destruct() {
$content = ob_get_contents();
//处理 rewrite
if($this->rewrite) {
$arr=$this->rewrite_rule;
$s=$arr[0];
$e=$arr;
$content=preg_replace($s,$e,$content);
}
ob_end_clean();
echo $content;
}
}
$tpl = new template(''skin'',"default");
$tpl->objdir=''tpp'';
$tpl->rewrite=true;//开启rewrite 需要服务器支持
$tpl->rewrite_rule=array(array("/index\.php/"),array("index.html")); //rewrite规则
$tpl->assign("indexurl","index.php");
$tpl->assign("str","我是字符串啦啦啦");
$tpl->assign("ec","我是被echo出来的");
$tpl->assign("subhtml","{subtpl ttt}这是用来引入一个
模板文件的,这个就是引入ttt.htm");
$tpl->assign("a",array(''dasdasd''.''bbbbbbb'',''cccccccccccccc''));
$tpl->assign("i",1);
$tpl->display("index");
?>
新建 tpl/default/index.html
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
1.字符串赋值 :<br />
{$str}
<br />
2.数组赋值 :<br />
{loop $a $v}{$v},{/loop}
或者<br />
{loop $a $key $val }{$val},{/loop}
3.{$subhtml}<br />
{subtpl ttt}<br />
4.原来我是{$indexurl } 现在我被变成了index.php<br />
5.我还可以 echo 出来呢<br />
{echo $ec}<br />
6.其实我还可以加减乘除的 6*7*8
{echo 6*7*8;}
7.常用