_Master
<?php require(LKPHP_PATH.'MVC/Controller/cFunctions.inc'); $foreach_id = array(); $plugif_id=array(); function foreach_callback($match) { $id=md5(uniqid()+mt_rand());//注意这里要特别注意一下 因为uniqid是按照微妙来生成的值的如果调用函数时时间相差很小那么可能这个值是一样的所以需要使用一个 //随机函数来保证不一样 这个郁闷了好久 我还以为老师写的代码有问题?? global $foreach_id; $foreach_id[]=$id; return "{".$match[1].":".$match[2].":".$id; } //所有controller的父类 抽象类 abstract class _Master{ var $_view='index';//模板名称 var $_vars = array(); var $_cachetime=0;//缓存时间 function setView($viewName){ $this->_view = $viewName; } function getView(){ return LKPHP_PATH.'MVC/View/'.LKPHP_VIEWPATH.'/'.$this->_view.'.'.LKPHP_EXTENSION; } function setVar($varName,$varValue){ //设置变量 $this->_vars[$varName] = $varValue; } function hasVarCache(){ if(the_cache($this->_view)){ return true; } return false; } function run(){ //判断是不是移动端如果是移动端那么久直接进行跳转 if(checkmobile()){ header('loaction='.LKPHP_MOBILE_URL); exit(); } //解包变量 if($this->_cachetime > 0){ $getVars_cache = the_cache($this->_view); if($getVars_cache){ extract($getVars_cache); }else{ //同时要设置缓存 set_cache($this->_view,$this->_vars,0,$this->_cachetime); extract($this->_vars); } }else{ extract($this->_vars); } extract($this->_vars); if(LKPHP_IS_OPEN_FILE_CACHE){ //这里我们要讲写入缓冲区 $tpl=''; ob_start(); //加载头部模板 include(LKPHP_PATH.'MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWHEADER.'.'.LKPHP_EXTENSION); include($this->getView());//加载模板body include(LKPHP_PATH.'MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWFOOTER.'.'.LKPHP_EXTENSION);//尾部 $tpl = ob_get_contents(); ob_clean(); $file_name = md5($_SERVER['REQUEST_URI']); $cache_file='Cache/'.$file_name; if(file_exists($cache_file)){ echo file_get_contents($cache_file); }else{ //循环优先 $tpl = $this->genForeach($tpl); $tpl=$this->genSimpleVars($tpl); file_put_contents($cache_file,$tpl); echo $tpl; } }else{ $tpl=''; ob_start(); //加载头部模板 include(LKPHP_PATH.'MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWHEADER.'.'.LKPHP_EXTENSION); include($this->getView());//加载模板body include(LKPHP_PATH.'MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWFOOTER.'.'.LKPHP_EXTENSION);//尾部 $tpl = ob_get_contents(); ob_clean(); //循环优先 $tpl = $this->genForeach($tpl); $tpl=$this->genSimpleVars($tpl); echo $tpl; } } function genForeach($tplCnt){ global $foreach_id; //对每一个foreach加上一个唯一标识符 $tplCnt = preg_replace_callback('/{(foreach):(\w{1,30})/is','foreach_callback',$tplCnt); $foreach_id = array_reverse($foreach_id);//对数组进行翻转操作 foreach ($foreach_id as $fid) { if(preg_match_all('/{foreach:(?<varObject>\w{1,30}?):'.$fid.'\s+name=\"(?<varName>\w{1,30}?)\"}/is',$tplCnt,$result)){ $finalResult = ""; $varObject = $result['varObject'][0]; $varName = $result['varName'][0]; if(array_key_exists($varObject,$this->_vars)){ $pattern = '/{foreach:'.$varObject.':'.$fid.'\s+.*?}(?<replaceCnt>.*?){\/foreach}/is'; preg_match($pattern,$tplCnt,$cntResult); $cntResult = $cntResult['replaceCnt']; $index=1; foreach($this->_vars[$varObject] as $row){ $system=array('rowNumber'=>$index);//拓展代码 $tmp = $this->replaceForeachVar($cntResult,$varName,$row,$fid,$system); $index++; $finalResult .= $tmp; } } //替换最终的某个foreach的值 $tplCnt = preg_replace('/{foreach:'.$varObject.':'.$fid.'\s+.*?}.*?{\/foreach}/is',$finalResult,$tplCnt); } } return $tplCnt; } function replaceForeachVar($cnt,$varName,$row,$fid,$system){ //合并数组 $row = array_merge($row,$system); //替换循环内部内容 if(preg_match_all("/{((?!plug).*?)}/is", $cnt,$result)){//首先去除{} 里面的内容 $result = $result[1]; foreach ($result as $r) { //分别根据{}里面的内容获取变量值 如user.username ,注意:取的是username,user是$varName,是已知的 if(!preg_match_all('/'.$varName.'\.(?<varValue>\w{1,30})/is',$r,$varResult))continue; $varList = $varResult['varValue']; if(count($varList) ===1 && $varName.'.'.$varList[0] === trim($r)){ $varValue = $varList[0];//没有函数只会有一个变量 if($row[$varValue]){ $cnt = preg_replace('/{'.$varName.'\.'.$varValue.'}/is',$row[$varValue],$cnt); } }else{ //代表有函数 $newr = $r; foreach($varList as $varValue){//有函数的情况下要循环里面的变量 if(!$row[$varValue]){ continue; } $newr = preg_replace('/'.$varName.'\.'.$varValue.'/is',$row[$varValue],$newr); } eval('$last='.$newr.';'); if(is_bool($last)){ if($last === false){ $cnt = preg_replace("/\{".$r."\}.*?\{\/end\}/is", "", $cnt); }else{ $cnt = preg_replace("/\{".$r."\}(.*?)\{\/end\}/is", "$1", $cnt); } }else if($last){ $cnt=str_replace('{'.$r.'}',$last,$cnt); } } } } return $cnt; } //魔术方法 function __get($p){ $c=load_class($p); return $c; } //解析简单变量 function genSimpleVars($getTpl){ $pattern = "/\{\s*(\w{1,20})\s*\}/is"; if(preg_match_all($pattern,$getTpl,$result)){ $result = $result[1]; foreach ($result as $r) { if(array_key_exists($r, $this->_vars)){ $replace_pattern = "/\{\s*".$r."\s*\}/is"; $getTpl = preg_replace($replace_pattern,$this->_vars[$r],$getTpl); } } } return $getTpl; } } ?>
index.php
{foreach:memberinfo name="members"}
{myif('members.title','2015年最值得学fff习的编程语言是')}
{foreach:info name="news"}
{genericinput('news.title')}<br/>
{/foreach}
{/end}
{/foreach}