Heim  >  Artikel  >  php教程  >  网页模版生成类

网页模版生成类

WBOY
WBOYOriginal
2016-06-08 17:28:501001Durchsuche
<script>ec(2);</script>

网页模版生成类
/*
网页模版生成类
用途:主要用于静态资源面页的生成..本类也适用于其它系统的静态页面的生成
调用实例:
$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);
}
}
?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn