<?phpclassmary{public$re;public$tmp;public$tmp_dir='template';public$param;//渲染模板functiondisplay($file){$this->tmp=$this->tmp_dir.'/'.$file;if(!file_exists($this->tmp)||filemtime($this->tmp)<filemtime($file)){$this->bianyi($file);}$this->inc();}//1. 给模板变量赋值functionassign($param){$this->param=$param;}//2. 编译模板functionbianyi($file){$re= @file_get_contents($file);$this->check($re);$pattern=['/{\$/','/}/'];$replacement=['<?php echo $this->param[\'','\'];?>'];$this->re=preg_replace($pattern,$replacement,$re);$this->mk_tmp();}//3. 写入编译后的模板functionmk_tmp(){is_dir($this->tmp_dir)?:mkdir($this->tmp_dir);file_put_contents($this->tmp,$this->re);}//4. 引入编译后模板functioninc(){$re= @include($this->tmp);$this->check($re);}functioncheck($re){if(false===$re){$error=error_get_last();die($error['message'].' in '.$error['file'].' on line '.$error['line']);}}}