一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

网页模版生成类

时间:2009-12-11 编辑:简简单单 来源:一聚教程网

网页模版生成类
php
/*
网页模版生成类
用途:主要用于静态资源面页的生成..本类也适用于其它系统的静态页面的生成
调用实例:
$tpl = new TemplateEngine();
$tpl->opentemplate("e:/t.htm");
$tpl->startelement = "";
$tpl->endelement = "";
$tpl->value = "这是资源标题的替换内容";
$tpl->replacetemplate();
$tpl->save();
*/

本帖隐藏的内容需要回复才可以浏览
class TemplateEngine
{
public $filepath, $startelement, $endelement, $value;
private $template, $fso ,$fle, $regex, $filestate;

function __construct()
{
  $this->filestate = false;
  $this->filepath  = '';
  $this->template  = '';
  $this->startelement = '';
  $this->endelement = '';
}
private function filterstr($str)
{
  if($str=='' || is_null($str))return '';
//  $str = str_replace('','\',$str);
  $str = str_replace('/','/',$str);
  $str = str_replace('(','(',$str);
  $str = str_replace(')',')',$str);
  $str = str_replace('*','*',$str);
  $str = str_replace('?','?',$str);
  $str = str_replace('{','}',$str);
  $str = str_replace('}','{',$str);
  $str = str_replace('.','.',$str);
  $str = str_replace('+','+',$str);
  $str = str_replace('[','[',$str);
  $str = str_replace(']',']',$str);
  return $str;
}

//设置模板文件路径
public function opentemplate($mfilepath)
{
  global $flib;
  if(!($this->template=@file_get_contents($mfilepath))){$flib->alert('指定的模板文件不存在!','BACK',0);exit();}
  $this->filestate = true;
  $this->filepath = $mfilepath;
}
//判断标识是否存在
public function iselement()
{
  if(!$this->filestate || $this->template=='' || $this->startelement=='' || $this->endelement=='')return;
  $strpatrn = "/".$this->filterstr($this->startelement)."[Ss]*?".$this->filterstr($this->endelement)."/";
  return preg_match($strpatrn,$this->template)?1:0;
}

// 规换模版元素,元素标签一般格式为:"",你当然也可以自义
    // 元素标签不区分大小写
public function replacetemplate()
{
  if(!$this->filestate || $this->template=='' || $this->startelement=='' || $this->endelement=='')return;
  $strpatrn = "/".$this->filterstr($this->startelement)."[Ss]*?".$this->filterstr($this->endelement)."/";
  $this->template = preg_replace($strpatrn,$this->startelement.$this->value.$this->endelement,$this->template);
}
//保存新的模板内容
public function save()
{
  if(!$this->filestate)return;
 
  @file_put_contents($this->filepath,$this->template);
}
//另存模板内容
public function saveas($mfilepath)
{
  if(!$this->filestate)return;
  @file_put_contents($mfilepath,$this->template);
}
}
?>

热门栏目