该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public function assign($var,$value){
if(isset($var)&&!empty($var)){//判断模板变量是否有设置,且不能为空
$this->_vars[$var]=$value;//接收从index.php文件分配过来的变量的值,将它们保存在$_vars变量中
}else{
exit('请设置模板变量!');
}
}
//getConfig()方法:将系统变量的值读入到$_configs变量中
private function getConfig(){
if(file_exists(ROOT_PATH.'/config/profile.xml')){//判断系统配置文件是否存在
$sxe=simplexml_load_file(ROOT_PATH.'/config/profile.xml');//载入系统配置文件
$taglib=$sxe->xpath('/root/taglib');//使用xpath()方法读取相关节点
foreach($taglib as $value){
$this->_configs["$value->name"]=$value->value;//将系统变量的值读入到$_configs变量中
}
}
}
//is_dir_exists()方法:相关目录是否存在的检测
private function is_dir_exists(){
if(!is_dir(TPL_DIR)){//检测是否存在模板文件夹
exit('模板文件夹不存在!');
}
if(!is_dir(TPL_C_DIR)){//检测是否存在编译文件夹
exit('编译文件夹不存在!');
}
if(!is_dir(CACHE_DIR)){//检测是否存在缓存文件夹
exit('缓存文件夹不存在!');
}
}
//cache()方法:完成与缓存相关的一些功能
private function cache(){
$this->cache_file=CACHE_DIR.md5($file).'.'.$file.'.html';//设置缓存文件路径
//如果开启缓存,缓存文件存在且模板文件没有被修改过,直接载入缓存文件
if(IS_CACHE){
if(file_exists($this->cache_file)&&filemtime($this->cache_file)>=filemtime($this->parse_file)){
include $this->cache_file;//载入缓存文件
return ;
}
}
//判断是否开启缓存,如果开启就生成静态html文件,否则,直接载入编译文件
IS_CACHE? ob_start():null;
if(IS_CACHE){
include $this->parse_file;
file_put_contents($this->cache_file,ob_get_contents());//生成静态html缓存文件
ob_end_clean();
include $this->cache_file;//载入静态html缓存文件
}else{
include $this->parse_file;//载入编译文件
}
}
}
?>
Parse.class.php模板解析类文件
//模板解析类
class Parse {
private $_tpl;//存放静态模板文件的内容
//初始化构造方法:读取模板文件内容保存到到$_tpl变量中
public function __construct($tpl_file){
if(!$this->_tpl=file_get_contents($tpl_file)){//读取静态模板文件内容到$_tpl变量中
exit('模板内容读取失败!');
}
}
//compile()方法:完成静态模板的解析并生成编译文件
public function compile($parse_file,$tpl_file){
$this->parse();//调用私有方法parse()完成各种标签的解析
//如果编译文件不存在或是模板文件被修改过就生成编译文件
if(!file_exists($parse_file)||filemtime($tpl_file)>=filemtime($parse_file)){
if(!file_put_contents($parse_file,$this->_tpl)){
exit('编译文件生成失败!');
}
}
}
//parseVar()方法:解析普通变量
private function parseVar(){
$mode='/\{\$([\w]+)\}/';//普通变量模式
//在模板文件中匹配模式,如果匹配成功,则替换成index.php文件中注入的变量
if(preg_match($mode,$this->_tpl)){
$this->_tpl=preg_replace($mode,"<?php echo \$this->_vars['$1'];?>",$this->_tpl);//替换成index.php文件中注入的变量
}
}
//parseIf()方法:解析if语句
private function parseIf(){