wecis 2.0 RC 版
p调试bug 请通知本人,谢谢!br / 新版功能:br / 1. 添加{-script lang=php-} 模板也可以写php代码br / 2.修改1.0 细节错误 : 如无用功代码,优化核心代码,提高运行编译速度。br / 3.添加模板格式
调试bug 请通知本人,谢谢!
新版功能:
1. 添加{-script lang='php'-} 模板也可以写php代码
2.修改1.0 细节错误 : 如无用功代码,优化核心代码,提高运行编译速度。
3.添加模板格式化函数{-$string|subistr(12):'...'-} ,时间格式化 {-$time|time('ymd')-}
<?php
/**
* [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.'/','<?php ',$data);
$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.'/','<?php foreach( \\1 as \\3){ ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'\/loop'.$this->tpl_rw_right.'/','<?php } ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\[((.*)?)\]'.$this->tpl_rw_right.'/','<?php echo \\1[\'\\3\']; ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'if((.*)?)'.$this->tpl_rw_right.'/','<?php if(\\1){ ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'else'.$this->tpl_rw_right.'/','<?php }else{ ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'\/if'.$this->tpl_rw_right.'/','<?php } ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\|subistr\(((.*)?)\)\:((.*)?)'.$this->tpl_rw_right.'/','<?php echo subistr(\\1,\\3,\\5); ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\|time\:\(((.*)?)\)'.$this->tpl_rw_right.'/',"<?php echo formatime(\\1,\\3); ?>",$data);
$data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)'.$this->tpl_rw_right.'/','<?php echo \\1; ?>',$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.='<?php $'.$k.'='.var_export($v,true).'; ?>';
}
}
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';
}
}
- 上一篇:递归遍历目录 及 更改目录权限
- 下一篇:PHP subistr 截取字符串无码
精彩图集
精彩文章






