这是一个简单的用php 实现的模板教程物。
class template
{
private $path = . ; #根目录
private $var;
private $tpldir = template; #模板存储目录
private $tplext = tpl; #模板文件的后缀名
private $tplid = 0 ; #模板的id号
private $compiledir = template_c; #编译后的php文件存放目录
private $iscache=false ; #是否用缓存 (默认不启动)
private $cacheid = 1; #缓存文件id号
private $cachelefttime=3600; #缓存有效期 (默认保存3600秒)
private $cachedir = cache; #缓存文件存储目录
private $autorefresh = false ; #是否自动刷新
private $pattern = array(
/({dw:)s*includes*filename=s*(.+..+)s*s*(/})/i,#包含文件
/({dw:)s*field.(.+)s*(/})/i,#局部变量
/({dw:)s*global.(.+)s*(/})/i,#全局变量
/({dw:)s*foreachs*(.+)s*ass*(.+)s*(/})/i,#foreach 语句
/({dw:)s*ends*foreachs*(/})/i, #foreach 结束
/({dw:)s*ifs*((.+))(/})/i,
/({dw:)s*elseifs*((.+))(/})/i,
/({dw:)s*elses*(/})/i,
/({dw:)s*ends*ifs*(/})/i,
);
private $replacement = array(
'inc_file(\2); ?>',
,
,
,
,
,
,
,
,
);
#构造函数
function __construct($path = , $tpldir=, $compiledir=,$iscache=,$cachelefttime=,$cachedir= ,$autorefresh=)
{
$this->path = $path ? $path : $this->path ;
$this->tpldir = $tpldir ? $tpldir : $this->tpldir ;
$this->compiledir = $compiledir ? $compiledir : $this->compiledir ;
$this->iscache = is_bool($iscache) ? $iscache : $this->iscache ;
$this->cachelefttime = $cachelefttime ? $cachelefttime : $this->cachelefttime ;
$this->cachedir = $cachedir ? $cachedir : $this->cachedir ;
$this->autorefresh = is_bool($autorefresh) ? $autorefresh : $this->autorefresh ;
}
#兼容php4
function template($path = , $tpldir=, $compiledir=,$iscache=,$cachelefttime=,$cachedir= ,$autorefresh=)
{
$this->__construct($path = , $tpldir=, $compiledir=,$iscache=,$cachelefttime=,$cachedir= ,$autorefresh=);
}
function __get($property)
{
return $this->$property ;
}
function __set($property,$value)
{
return $this->$property = $value ;
}
#给模板中的变量赋值
# $tplval 模板中的变量名
function assign($tplval ,$value=)
{
if (is_array($tplval))
{
foreach ($tplval as $key => $val)
{
if (!empty($key))
$this->var[$key] = $val ;
}
}
else {
if (!empty($tplval))
$this->var[$tplval] = $value ;
}
}
#输出文件内容函数
function display($tplfile,$tplid=0,$cacheid = 1,$cachelefttime=)
{
if (empty($tplfile)) die(template {$tplfile} not exist !);
$this->cacheid = $cacheid ? md5($cacheid) : md5($this->cacheid);
$cachefile = $this->path. /.$this->cachedir./.$tplfile.$this->cacheid ;
if ($this->check_cache($cachefile,$cachelefttime)) #当缓存文件存在且不过期时直接从缓存文件读取内容
{
echo $this->read_file($cachefile);
}else {
$tpl = $this->path./.$this->tpldir./.$tplfile...$this->tplext;
$tplcontent = $this->read_file($tpl); #读取模板文件的内容
$compilecontent= $this->compile_file($tplcontent); #对读取出来的文件进行编译
$this->tplid = $tplid ? $tplid : $this->tplid ;
$compilefile = $this->path./.$this->compiledir./.md5($this->tplid)..$tplfile..php;
$this->write_file($compilefile,$compilecontent);#将编译后的内容写入相应的文件中;
@extract($this->var);
ob_start();
include_once($compilefile);
$content = ob_get_contents() ;
ob_end_clean() ;
if ($this->iscache){
$this->write_file($cachefile,$content) ;# 帮编译好的内容写入缓存文件
}
echo $content ;
}
}
/* function trim_tag($content)
{
$content = str_replace($this->starttag,,$content);
$content = str_replace($this->endtag,,$content);
//$content = trim($content);
return $content ;
}*/
# 编译文件函数
function compile_file($content=null)
{
$content = $content ? $content :die(compile fail!) ;
//$content = $this->trim_tag($content);
$content = preg_replace($this->pattern,$this->replacement,$content);
return $content;
}
#解析包含文件
function inc_file($filename,$tplid=,$cacheid=,$cachelefttime=)
{
$file = $this->path./.$this->tpldir./.$filename ;
if (file_exists($file))
{
$filename = str_replace(..$this->tplext,,$filename);
return $this->display($filename,$tplid,$cacheid,$cachelefttime);
}
else die(template {$filename} not exist);
}
#读取文件内容函数
function read_file($filename)
{
if (!file_exists($filename)) die(read file fail) ;
return file_get_contents($filename);
}
#内容写入函数
function write_file($filename,$content,$mode=wb)
{
$filename = trim($filename);
$content = $content ? stripslashes(trim($content)) : exit();
if (!file_exists($filename))
{
$array = explode(/,$filename);
$count = count($array);
$path = ;
for ($i = 0 ; $i {
if(!file_exists($path .= $array[$i]./))
{
mkdir($path,0777);
}
}
}
$handle = fopen($filename,$mode) ;
fwrite($handle,$content);
fclose($handle);
return true;
}
# 清除缓存
function clear_dir($dir=)
{
$dir = $this->path./.$dir;
$handle = opendir($dir);
if (file_exists($dir))
{
while ($file = readdir($handle))
{
if ($file !=. && $file != ..)
unlink($dir./.$file);
}
closedir($handle);
return true;
}
else {
return false;
}
}
#清除所有缓存
function clear_all_cache()
{
if ($this->clear_dir($this->cachedir) && $this->clear_dir($this->compiledir))
return true;
}
#检查缓存是否过期
function check_cache($cachefile,$cachelefttime=)
{
$cachelefttime = $cachelefttime ? $cachelefttime : $this->cachelefttime;
if (!file_exists($cachefile)) return false ;
$time = $this->get_time($cachefile) + $cachelefttime ;
if ($time {
unlink($cachefile);
return false;
}
return true;
}
# 获取文件最后编辑时间
function get_time($filename)
{
if (!file_exists($filename)) return false;
return filemtime($filename);
}
}
