调试bug 请通知本人,谢谢! 新版功能: 1. 添加{-script lang='php'-} 模板也可以写php代码 2.修改1.0 细节错误 : 如无用功代码,优化核心代码,提高运行编译速度。 3.添加模板格式化函数{-$string|subistr(12):'...'-} ,时间格式化 {-$time|time('ymd')-}
- /**
- * [Wecis] (C)2012-2099 Haowei Inc.
- * This is NOT a freeware, use is subject to license terms
- * version: 2.0 RC
- **/
- class wecis{
-
- private $assign=array();
- public $tpl_cache_dir = 'cache/';
- public $tpl_cache_open = TRUE;
- public $tpl_cache_time = 5 ;
- public $tpl_tpl_dir = 'template/';
- public $tpl_rw_left = '{-';
- public $tpl_rw_right = '-}';
-
- public function wecis(){
- return '[{name:\'wecis 2.0 rc\'}]';
- }
-
- public function assign($tplvar,$value){
- if(empty($tplvar) || empty($value)){
- $this->error('wecis-> assign() Parameters are missing');
- }
- return $this->assign[$tplvar]=$value;
- }
-
- public function display($tplfile){
- if(empty($tplfile)){ $this->error('wecis-> display() Parameters are missing'); }
- if($this->tpl_cache_open){
- if(file_exists($this->tpl_cache_dir.$this->cahce_format($tplfile))){
- if($this->cahce_timeout($tplfile)) {
- $this->cache_delete($tplfile);
- include $this->cache($tplfile) ? $this->tpl_cache_dir.$this->cahce_format($tplfile) : '';
- }else{
- include $this->tpl_cache_dir.$this->cahce_format($tplfile);
-
- }
- }else{
- include $this->cache($tplfile) ? $this->tpl_cache_dir.$this->cahce_format($tplfile) : '';
- }
- }else{
- include $this->cache($tplfile) ? $this->tpl_cache_dir.$this->cahce_format($tplfile) : '';
- }
- }
-
- public function cache($tplfile){
- $data = file_get_contents($this->tpl_tpl_dir.$tplfile);
- $data = preg_replace('/'.$this->tpl_rw_left.'script lang=\'php\''.$this->tpl_rw_right.'/',' $data = preg_replace('/'.$this->tpl_rw_left.'\/script'.$this->tpl_rw_right.'/','?>',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'loop (\$(.*)?) as (\$(.*)?)'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'\/loop'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\[((.*)?)\]'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'if((.*)?)'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'else'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'\/if'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\|subistr\(((.*)?)\)\:((.*)?)'.$this->tpl_rw_right.'/','',$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\|time\:\(((.*)?)\)'.$this->tpl_rw_right.'/',"",$data);
- $data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)'.$this->tpl_rw_right.'/','',$data);
- $arr='';
- foreach($this->assign as $k=>$v) {
- if(!is_array($v)) {
- $data = !is_numeric($v) ? str_replace('$'.$k,'\''.$v.'\'',$data) : str_replace('$'.$k,$v,$data);
- }else{
- $arr.='';
- }
- }
- return file_put_contents($this->tpl_cache_dir.$this->cahce_format($tplfile),$arr.$data) ? 1 : 0;
- }
-
- public function cahce_timeout($tplfile){
- $time = time();
- $filetime = fileatime($this->tpl_cache_dir.$this->cahce_format($tplfile));
- return $time-$filetime > ($this->tpl_cache_time*60) ? 1 : 0;
- }
-
- public function cahce_format($tplfile){
- return md5($this->version()).str_replace('/','.',$tplfile).'.php';
- }
-
- public function cache_delete($tplfile){
- return unlink($this->tpl_cache_dir.$this->cahce_format($tplfile)) ? 1 : 0;
- }
-
- private function error($str){
- return exit($str);
- }
-
- public function version(){
- return 'wecis 2.0 RC';
- }
-
- }
|