wecis 模板引擎 ,很方便
webtmp 正改名为 wecis
webtmp 正改名为 wecis
<?php
/**
* [wecis] (C)2010-2099 Haowei Inc.
* This is NOT a freeware, use is subject to license terms
*
**/
class wecis{
private $assign=array();
public $tpl_cache_dir = 'cache/';
public $tpl_cache_open = FALSE;
public $tpl_cache_time = 5 ;
public $tpl_tpl_dir = 'template/';
public $tpl_rw_left = '{-';
public $tpl_rw_right = '-}';
public function wecis() {
}
public function assign($tplvar,$value) {
if(empty($tplvar) && empty($value)) {
exit('wecis-> assign() Parameters are missing');
}elseif(empty($tplvar)){
exit('wecis-> assign() Parameters are missing');
}
return $this->assign[$tplvar]=$value;
}
public function display($tplfile) {
if(empty($tplfile)) {
exit('wecis-> display() Parameters are missing');
}
if($this->tpl_cache_open) {
if(file_exists($this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php')) {
$filetime = fileatime($this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php');
if(time()-$filetime > ($this->tpl_cache_time*60)) {
unlink($this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php');
include $this->createCachefile($tplfile) ? $this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php' : FALSE;
}else{
include $this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php';
}
}else{
include $this->createCachefile($tplfile) ? $this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php' : FALSE;
}
}else{
include $this->createCachefile($tplfile) ? $this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php' : FALSE;
}
}
private function createCachefile($tplfile) {
$arrayWrite = '';
$data = file_get_contents($this->tpl_tpl_dir.$tplfile);
$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.'loop as (\$(.*)+)'.$this->tpl_rw_right.'/',"<?php foreach(\\1 as \\1){ ?>",$data);
$data = preg_replace('/'.$this->tpl_rw_left.'(\$(.*)?)\[((.*)?)\]'.$this->tpl_rw_right.'/',"<?php echo \\1['\\3']; ?>",$data);
$data = preg_replace('/'.$this->tpl_rw_left.'\/loop'.$this->tpl_rw_right.'/','<?php } ?>',$data);
$data = preg_replace('/'.$this->tpl_rw_left.'/','<?php echo ',preg_replace('/-}/',' ?>',$data));
foreach($this->assign as $k=>$v) {
if(is_array($v)) {
$arrayWrite .= '<?php $'.$k.'='.var_export($v,true).'; ?>';
}else{
if(!is_numeric($v)) {
$data = preg_replace('/\$'.$k.'/','\''.$v.'\';',$data);
}else{
$data = preg_replace('/\$'.$k.'/',$v,$data);
}
}
}
$data = $arrayWrite.$data;
return file_put_contents($this->tpl_cache_dir.md5($this->version()).$this->formatname($tplfile).'.php',$data) ? 1 : 0;
}
private function formatname($tplfile) {
return str_replace('/','.',$tplfile);
}
private function version(){
return 'wecis 1.0';
}
}
?>
精彩图集
精彩文章






