<?php
class Tpl{
private $debugMode = true;
private $baseDir = "";
private $logFile = "";
private $tpl = array();
/*
$tpl[] = array(
name => "header",
file => "header.html",
source => "...",//原始信息。
content => "..."//解析后的内容
)
*/
private $key = array();
private $block = array();
private $loop = array();
/*
block:
<tpl tplName>content...</tpl>
*/
public function __construct(){
$this->baseDir = func_get_arg(0);
$this->logFile = func_get_arg(1);
}
/*
example:
$name = array(
"header" => "header.html",
"footer" => "footer.html"
);
*/
public function load($name){
if(is_array($name)){
while (list($k,$v) = each($name)) {
$this->load($k,$v);
}
return;
}
$name = strtoupper($name);//转换为大写
if(isset($this->tpl[$name])){
$this->debug("重复的模板名称 : $name");
}
$file = func_get_arg(1);
$source = "";
if(!is_file($this->baseDir.$file)){
$this->debug("找不到文件 : $file 文件夹 : $this->baseDir");
return;
}else{
$source = join("",@file($this->baseDir.$file));
}
/*
$reg = "/<tpl(.*)>(.*)<\/tpl>/iUs";
$s = preg_match_all($reg,"<tpl aa>aaaa<tpl>fffff</tpl></tpl>dddddddddd<tpl bb>bbbb</tpl>eeeeeeee<tpl cc>cccc</tpl>",$su);
var_dump($su);
*/
$blockReg = "/<!--block(.*)-->(.*)<!--\/block-->/iUs";
if( preg_match_all( $blockReg, $source ,$ma ) ){
while (list($k,$v) = each($ma[1])) {
$blockName = strtoupper( trim($v) ); //以完全被转换成大写了!
if (isset($this->block[$blockName])) {
$this->debug("以载入的模板中,有重复的块名称 : $blockName");
}
$this->block[$blockName] = array(
"name" =>$blockName,
"parentName" => $name,
"file" => $file,
"source" => $ma[2][$k],//原始信息
"content" => ""//解析后的内容,初始化为空
);
}
}
$this->tpl[$name] = array(
"name"=>$name,
"file"=>$file,
"source"=>$source,//原始信息
"content"=>$source//解析后的内容,这里是初始化
);
}
public function assign($key){
if(is_array($key)){
while(list($k,$v) = each($key)){
$this->assign($k,$v);
}
return;
}
$key = strtoupper($key);//转为大写
$this->key[strtoupper($key)] = func_get_arg(1);
}
public function parseBlock($name){
$name = strtoupper($name);
if(!isset($this->block[$name])){
$this->debug("没有找到块: $name 请确认该块是否存在于以载入的模板文件中!");
return;
}
$source = $this->block[$name]["source"];
$reg = array();
$rep = array();
reset($this->key);
while (list($k,$v) = each($this->key)) {
$reg[] = "/#%$k%#/iUs";
$rep[] = "$v";
}
$this->block[$name]["content"] .= preg_replace($reg,$rep,$source);
}
public function parse($name){
$name = strtoupper($name);//名字大小写统一,所以不区分大小写。
if(!isset($this->tpl[$name])){
$this->debug("没有找到模板: $name ,请确认是否载入!");
return "";
}
/*替换block*/
$blockReg = "/<!--block(.*)-->(.*)<!--\/block-->/iUs";
if(preg_match_all( $blockReg , $this->tpl[$name]["source"] , $ma)){//从source中查找block
//$ma[1]中存放所有block的名称
//var_dump($ma[1]);
while (list($k,$v) = each($ma[1])) {
$blockName = strtoupper( trim($v) );
if (isset($this->block[$blockName])) {
$this->tpl[$name]["content"] = str_ireplace(// str_ireplace ....
$ma[0][$k],////////////
$this->block[$blockName]["content"],
$this->tpl[$name]["content"]);
}
}
}
$reg = array();
$rep = array();
reset($this->key);
while (list($k,$v) = each($this->key)) {
$reg[] = "/#%$k%#/iUs";
$rep[] = "$v";
}
$this->tpl[$name]["content"] = preg_replace( $reg, $rep, $this->tpl[$name]["content"]);
return $this->tpl[$name]["content"];
}
protected function debug($msg){
if(!$this->debugMode || !is_file($this->logFile)){
return;
}
error_log(sprintf("%s\t%s\r\n",date("Y/m/d H:i:s"),$msg),3,$this->logFile);
}
}
?>
<?php
require_once("../common.inc.php");
$tpl = new Tpl(PATH_FULL.Config::DIR_TEMPLATE ,PATH_FULL.Config::FILE_TEMPLATE_LOG);
$tpl->load(array(
"header" => "header.html",
"menu" => "menu.html",
"holler" => "holler/holler.html",
"hollerJs" => "holler/hollerJs.html",
"footer" => "footer.html",
"customJs" => "error/customJs.html",
"pagination" => "pagination.html"
));
$tpl->assign(array(
"title" => "我要\"小声\"叫喊 ".Config::TITLE_DOMAIN,
"path" => PATH_BASE_ROOT,
"formAction" => PATH_BASE_ROOT."holler/do.php?".FORM_KEY_ACTION."=".FORM_KEY_ADD
));
$tpl->parseBlock("tplOtherPlus");
$page = Holler::getList();
$result = $page->process();
$tpl->assign(array(
"recordNum" => $page->recordNum,
"totalPage" => $page->totalPage,
"currPage" => $page->currPage,
"pageSize" => $page->pageSize,
"rangeS" => $page->rangeS,
"rangeE" => $page->rangeE,
"pageLabels" => $page->exportPageLabel()
));
$pagination = $tpl->parse("pagination");
$tpl->assign("pagination",$pagination);
while ($row = mysql_fetch_array($result)){
$tpl->assign(array(
"hollerTitle" => $row["TITLE"],
"hollerContent" => nl2br( htmlspecialchars($row["CONTENT"])),
"hollerId" => $row["ID"],
"hollerTime" => $row["PUB_DATE"],
"hollerIP" => $row["IP"],
));
$tpl->parseBlock("tplHollerList");
}
$tpl->parseBlock("tplHollerNew");
echo $tpl->parse("header");
echo $tpl->parse("menu");
echo $tpl->parse("holler");
echo $tpl->parse("hollerJs");
echo $tpl->parse("footer");
ReWrite::flush();
?>