001
templatedir = $templatedir;
022
$this->templatecompiledir = $templatecompliedir;
023
}
024
/**
025
* 显示模板
026
* @param string $filename 模板文件名
027
*/
028
public function display($filename){
029
$this->filename = $filename;
030
if(file_exists($this->templatedir.'/'.$this->filename)){
031
$compilefilename = $this->templatecompiledir.'/'.$this->file_safe_name().'.php';
032
if(!file_exists($compilefilename) || filemtime($compilefilename)templatedir.'/'.$this->filename)){
033
$this->del_old_file();
034
$this->compile();
035
}
036
extract($this->templatevar);
037
include $compilefilename;
038
}else{
039
$this->error('sorry,the template file '.$this->filename.' does not exist!!');
040
}
041
}
042
/**
043
* 获取编译文件名
044
*/
045
private function get_compile_file(){
046
$compilefile = explode('.',$this->filename);
047
unset($compilefile[count($compilefile)-1]);
048
return implode('.',$compilefile);
049
}
050
/**
051
* 编译
052
*/
053
private function compile(){
054
$filehandle = @fopen($this->templatedir.'/'.$this->filename, 'r');
055
while(!feof($filehandle)){
056
$filecontent = fread($filehandle,1024);
057
}
058
fclose($filehandle);
059
$filecontent = $this->template_replace($filecontent);
060
//$compilefile = $this->get_compile_file($filename);
061
$filehandle = @fopen($this->templatecompiledir.'/'.$this->file_safe_name().'.php','w');
062
if($filehandle){
063
fwrite($filehandle, $filecontent);
064
fclose($filehandle);
065
}else{
066
$this->error('sorry,compile dir can not write!');
067
}
068
}
069
/**
070
* 模板传值
071
* @param string $valuename 模板中使用的变量名
072
* @param $value 变量值
073
*/
074
public function assign($valuename,$value){
075
$this->templatevar[$valuename] = $value;
076
}
077
078
/**
079
* 模板正则替换
080
* @param string $content 替换内容
081
* @return string 替换过后的内容
082
*/
083
private function template_replace($content){
084
$orginarray = array(
085
'//s',
086
'//s',
087
'/(.+?)/s',
088
'//s',
089
'//s',
090
'//s',
091
'//s',
092
'//s',
093
'/\{p:(.+?)\:}/s',
094
'/\{c:(\w+)\}/s',
095
'/\{i:(.+?)\}/s',
096
'/\{f:(.+?)\}/s',
097
'/\{ef:(.+?)\}/s',
098
'/\{([a-za-z0-9_\[\]\'\\$\.\x7f-\xff]+)\}/s',
099
);
100
101
$changearray = array(
102
'',
103
'$$3){$countloop++;?>',
104
'$1',
105
'',
106
'',
107
'',
108
'',
109
'',
110
'',
111
'',
112
'templatedir.'/$1;?>',
113
'',
114
'',
115
'',
116
);
117
return $repcontent=preg_replace($orginarray,$changearray,$content);
118
}
119
/**
120
* 删除旧文件
121
*/
122
private function del_old_file(){
123
$compilefile = $this->get_compile_file($this->filename);
124
$files = glob($this->templatecompiledir.'/'.$compilefile.'*.php');
125
// print_r($files);
126
if($files){
127
@unlink($files[0]);
128
}
129
}
130
/**
131
* 编译文件名安全处理方法
132
* @return string 返回编译文件名
133
*/
134
private function file_safe_name(){
135
$compilefile = $this->get_compile_file($this->filename);
136
return $compilefile.filemtime($this->templatedir.'/'.$this->filename);
137
}
138
139
/**
140
* 错误输出函数
141
* @param string $content 错误输出信息
142
*/
143
private function error($content){
144
$stringhtml = '
';
145
$stringhtml .= 'error information:
';
146
$stringhtml .= '';
147
$stringhtml .= $content;
148
$stringhtml .= '';
149
$stringhtml .= '
';
150
exit($stringhtml);
151
}
152
}
153
?>
index.phpview sourceprint?
01
assign('a', $a);
10
$template->display('index.html');
11
?>
作者:袁家伟
http://www.bkjia.com/phpjc/478121.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478121.htmltecharticlejtemplate.class.php 001 ?php 002 /** 003 * a href=http://my.oschina.net/arthor target=_blank rel=nofollow@author/a jiawei 004 * @completed in 2012-6-29 0:23 005 */ 006 class jtempl...
