模板引擎
鼎峰企业智能建站系统是一个非常灵活的企业建站工具(简称:dfeiew),网页设计师可以使用dfeiew来快速建立企业网站。dfeiew采用adodo作为数据库持久层,采用smarty模板引擎,美工灵活,而且smarty是编译型的,访问快速。鼎峰拥有php+mysql,asp+access/ms sql版本,并且都是开源、免费的!快速提供企业建站传统的cms体系结构过于复杂,不适合做企业站点,而鼎峰
templateDir = $templateDir;
$this->templateCompileDir = $templateComplieDir;
}
/**
* 显示模板
* @param string $fileName 模板文件名
*/
public function display($fileName){
$this->fileName = $fileName;
if(file_exists($this->templateDir.'/'.$this->fileName)){
$compileFileName = $this->templateCompileDir.'/'.$this->file_safe_name().'.php';
if(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.'/'.$this->fileName)){
$this->del_old_file();
$this->compile();
}
extract($this->templateVar);
include $compileFileName;
}else{
$this->error('Sorry,the template file '.$this->fileName.' does not exist!!');
}
}
/**
* 获取编译文件名
*/
private function get_compile_file(){
$compileFile = explode('.',$this->fileName);
unset($compileFile[count($compileFile)-1]);
return implode('.',$compileFile);
}
/**
* 编译
*/
private function compile(){
$fileHandle = @fopen($this->templateDir.'/'.$this->fileName, 'r');
while(!feof($fileHandle)){
$fileContent = fread($fileHandle,1024);
}
fclose($fileHandle);
$fileContent = $this->template_replace($fileContent);
//$compileFile = $this->get_compile_file($fileName);
$fileHandle = @fopen($this->templateCompileDir.'/'.$this->file_safe_name().'.php','w');
if($fileHandle){
fwrite($fileHandle, $fileContent);
fclose($fileHandle);
}else{
$this->error('Sorry,Compile dir can not write!');
}
}
/**
* 模板传值
* @param string $valueName 模板中使用的变量名
* @param $value 变量值
*/
public function assign($valueName,$value){
$this->templateVar[$valueName] = $value;
}
/**
* 模板正则替换
* @param string $content 替换内容
* @return string 替换过后的内容
*/
private function template_replace($content){
$orginArray = array(
'//s',
'//s',
'/(.+?)/s',
'//s',
'//s',
'//s',
'//s',
'//s',
'/\{P:(.+?)\:}/s',
'/\{C:(\w+)\}/s',
'/\{I:(.+?)\}/s',
'/\{F:(.+?)\}/s',
'/\{EF:(.+?)\}/s',
'/\{([a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s',
);
$changeArray = array(
'',
'$$3){$countLoop++;?>',
'$1',
'',
'',
'',
'',
'',
'',
'',
'templateDir.'/$1";?>',
'',
'',
'',
);
return $repContent=preg_replace($orginArray,$changeArray,$content);
}
/**
* 删除旧文件
*/
private function del_old_file(){
$compileFile = $this->get_compile_file($this->fileName);
$files = glob($this->templateCompileDir.'/'.$compileFile.'*.php');
// print_r($files);
if($files){
@unlink($files[0]);
}
}
/**
* 编译文件名安全处理方法
* @return string 返回编译文件名
*/
private function file_safe_name(){
$compileFile = $this->get_compile_file($this->fileName);
return $compileFile.filemtime($this->templateDir.'/'.$this->fileName);
}
/**
* 错误输出函数
* @param string $content 错误输出信息
*/
private function error($content){
$stringHtml = '';
$stringHtml .= 'Error information:
';
$stringHtml .= '';
$stringHtml .= $content;
$stringHtml .= '';
$stringHtml .= '';
exit($stringHtml);
}
}
?>










