大发体育娱乐在线-大发体育娱乐官方网站-大发体育娱乐登录网址
做最好的网站

领到出来的模板类,网页模版生成类

来源:http://www.dfwstonefabricators.com 作者:大发体育网络 人气:132 发布时间:2019-09-28
摘要:网页模版生成类 ?php /* 网页模版生成类 用途:首要用来静态财富面页的生成..本类也适用于别的系统的静态页面包车型客车变动 调用实例: $tpl = new TemplateEngine(); $tpl-opentemplate("e:/t.ht

网页模版生成类
<?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;

<?php
/*template.class.php
@康盛乐乎 模板提取类 认为那几个模板好用 花些日子独自出来。 by 雷日锦
@看了眨眼之间间ctt 那些模板 跟 phpcms的模版类似 难道?? ^_^ 嘿嘿!!!
@ 微博
@ 好东西武大学家分享 磕磕绊绊的领抽出来 不通常请提议来
@ 模板文件默感到 .htm
$tpl = new template('skin',"default");
$tpl->objdir='tpp';
$tpl->rewrite=true;//开启rewrite 要求服务器援助
$tpl->rewrite_rule=array(array("/index.php/"),array("index.html")); //rewrite规则
$tpl->assign("indexurl","index.php");
$tpl->assign("str","小编是字符串啦啦啦");
$tpl->assign("ec","我是被echo出来的");
$tpl->assign("subhtml","{subtpl ttt}那是用来引进三个模板文件的,那么些就是引入ttt.htm");
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc'));
$tpl->assign("i",1);
$tpl->display("index");
*/
if(!defined("CHA福睿斯SET")) define("CHA兰德传祺SET","gb2312");//字符编码
if(!defined("DIR_TPL")) define("DIR_TPL","tpl");//暗许模板目录
if(!defined("DIR_DATA")) define("DIR_DATA","data");//暗中同意数据目录
if(!defined("DEBUG")) define("DEBUG",0);//暗中认可运营方式
class template {
//note var
public $rewrite=false;//是不是开启 伪静态 rewrite
public $rewrite_rule=array(); //设置伪静态准则
public $defaulttpldir;//暗中认可的沙盘
public $tpldir;//模板目录
public $objdir;//编写翻译缓存目录
public $tplfile;//模板文件
public $objfile;//编写翻译文件
public $tplid=1;//模板编号
public $currdir='default';//当前风格目录
public $vars=array();//note 变量表
public $removeblanks=false;//移除空格
public $stdout='display';//输出类型
function __construct($tplid, $currdir) {
$this->template($tplid, $currdir);
}
function template($tplid, $currdir) {
ob_start();
if(file_exists(DIR_TPL.'/'.$currdir)) {
$this->currdir = $currdir;
$this->tplid = $tplid;
} else {
$this->currdir = 'default';
$this->tplid = 1;
}
$this->defaulttpldir = DIR_TPL.'/default';
$this->tpldir = DIR_TPL.'/'.$this->currdir;
$this->objdir = DIR_DATA.'/cache/tpl';
if(version_compare(PHP_VERSION, '5') == -1) {
register_shutdown_function(array(&$this, '__destruct'));
}
}
//note publlic
function assign($k, $v) {
$this->vars[$k] = $v;
}
//note publlic
function display($file) {
extract($this->vars, EXTR_SKIP);
include $this->getObj($file);
}
function getObj($file, $tpldir = '') {
$subdir = ($pos = strpos($file, '/')) === false ? '' : substr($file, 0, $pos);
$file = $subdir ? substr($file, $pos + 1) : $file;
$this->tplfile = ($tpldir ? $tpldir : $this->tpldir).'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
$this->objfile = $this->objdir.'/'.($tpldir ? '' : $this->tplid.'_').($subdir ? $subdir.'_' : '').$file.'.php';
//note 暗中同意目录
if(@filemtime($this->tplfile) === FALSE) {
$this->tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
}
//note 剖断是或不是正如过期
if(!file_exists($this->objfile) || DEBUG && @filemtime($this->objfile) < filemtime($this->tplfile)) {
$this->compile();
}
return $this->objfile;
}
function getTpl($file) {
$subdir = ($pos = strpos($file, '/')) === false ? '' : substr($file, 0, $pos);
$file = $subdir ? substr($file, $pos + 1) : $file;
$tplfile = $this->tpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
if(@filemtime($tplfile) === FALSE) {
$tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
}
return $tplfile;
}
function compile() {
$var_regexp = "@?\$[a-zA-Z_]w*(?:[[w."'[]$]+])*";
$vtag_regexp = "<?=(@?\$[a-zA-Z_]w*(?:[[w."'[]$]+])*)?>";
$const_regexp = "{([w]+)}";
$template = file_get_contents($this->tplfile);
for($i = 1; $i <= 3; $i++) {
if(strpos($template, '{subtpl') !== FALSE) {
if(DEBUG == 2) {
$template = str_replace('{subtpl ', '{tpl ', $template);
} else {
$template = preg_replace("/[nrt]*{subtpls+([a-z0-9_:/]+)}[nrt]*/ies", "file_get_contents($this->getTpl('\1'))", $template);
}
}
}
$remove = array(
'/(^|r|n)/*.+?(r|n)*/(r|n)/is',
'///note.+?(r|n)/i',
'///debug.+?(r|n)/i',
'/(^|r|n)(s|t)+/',
'/(r|n)/',
);
$this->removeblanks && $template = preg_replace($remove, '', $template);
$template = preg_replace("/<!--{(.+?)}-->/s", "{\1}", $template);
$template = preg_replace("/{($var_regexp)}/", "<?=\1?>", $template);
$template = preg_replace("/{($const_regexp)}/", "<?=\1?>", $template);
$template = preg_replace("/(?<!<?=|\\)$var_regexp/", "<?=\0?>", $template);
$template = preg_replace("/<?=(@?\$[a-zA-Z_]w*)(([[\$[]w]+])+)?>/ies", "$this->arrayindex('\1', '\2')", $template);
$template = preg_replace("/{{eval (.*?)}}/ies", "$this->stripvtag('<? \1?>')", $template);
$template = preg_replace("/{eval (.*?)}/ies", "$this->stripvtag('<? \1?>')", $template);
$template = preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/ies", "$this->stripvtag('<? echo \1; ?>','')", $template);
$template = preg_replace("/{for (.*?)}/ies", "$this->stripvtag('<? for(\1) {?>')", $template);
$template = preg_replace("/{elseifs+(.+?)}/ies", "$this->stripvtag('<? } elseif(\1) { ?>')", $template);
for($i=0; $i<2; $i++) {
$template = preg_replace("/{loops+$vtag_regexps+$vtag_regexps+$vtag_regexp}(.+?){/loop}/ies", "$this->loopsection('\1', '\2', '\3', '\4')", $template);
$template = preg_replace("/{loops+$vtag_regexps+$vtag_regexp}(.+?){/loop}/ies", "$this->loopsection('\1', '', '\2', '\3')", $template);
}
$template = preg_replace("/{ifs+(.+?)}/ies", "$this->stripvtag('<? if(\1) { ?>')", $template);
$template = preg_replace("/{tpls+(w+?)}/is", "<? include $this->getObj("\1");?>", $template);
$template = preg_replace("/{tpls+(.+?)}/ise", "$this->stripvtag('<? include $this->getObj("\1"); ?>')", $template);
$template = preg_replace("/{tmptpls+(w+?)}/is", "<? include $this->getObj("\1", $this->objdir);?>", $template);
$template = preg_replace("/{tmptpls+(.+?)}/ise", "$this->stripvtag('<? include $this->getObj("\1", $this->objdir); ?>')", $template);
$template = preg_replace("/{else}/is", "<? } else { ?>", $template);
$template = preg_replace("/{/if}/is", "<? } ?>", $template);
$template = preg_replace("/{/for}/is", "<? } ?>", $template);
$template = preg_replace("/$const_regexp/", "<?=\1?>", $template);//note {else} 也相符常量格式,此处要留神前后相继顺序
$template = preg_replace("/(\$[a-zA-Z_]w+[)([a-zA-Z_]w+)]/i", "\1'\2']", $template);
$fp = fopen($this->objfile, 'w');
fwrite($fp, $template);
fclose($fp);
}
function arrayindex($name, $items) {
$items = preg_replace("/[([a-zA-Z_]w*)]/is", "['\1']", $items);
return "<?=$name$items?>";
}
function stripvtag($s) {
$vtag_regexp = "<?=(@?\$[a-zA-Z_]w*(?:[[w."'[]$]+])*)?>";
return preg_replace("/$vtag_regexp/is", "\1", str_replace("\"", '"', $s));
}
function loopsection($arr, $k, $v, $statement) {
$arr = $this->stripvtag($arr);
$k = $this->stripvtag($k);
$v = $this->stripvtag($v);
$statement = str_replace("\"", '"', $statement);
return $k ? "<? foreach((array)$arr as $k => $v) {?>$statement<?}?>" : "<? foreach((array)$arr as $v) {?>$statement<? } ?>";
}
function __destruct() {
$content = ob_get_contents();
//处理 rewrite
if($this->rewrite) {
$arr=$this->rewrite_rule;
$s=$arr[0];
$e=$arr[1];
$content=preg_replace($s,$e,$content);
}
ob_end_clean();
echo $content;
}
}
$tpl = new template('skin',"default");
$tpl->objdir='tpp';
$tpl->rewrite=true;//开启rewrite 供给服务器扶助
$tpl->rewrite_rule=array(array("/index.php/"),array("index.html")); //rewrite规则
$tpl->assign("indexurl","index.php");
$tpl->assign("str","我是字符串啦啦啦");
$tpl->assign("ec","我是被echo出来的");
$tpl->assign("subhtml","{subtpl ttt}那是用来引进二个模板文件的,这一个正是引进ttt.htm");
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc'));
$tpl->assign("i",1);
$tpl->display("index");
?>

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;
}

新建 tpl/default/index.html

//设置模板文件路线
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);
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";
<html xmlns=";
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
1.字符串赋值 :<br />
{$str}
<br />
2.数组赋值 :<br />
{loop $a $v}{$v},{/loop}
或者<br />
{loop $a $key $val }{$val},{/loop}

3.{$subhtml}<br />
{subtpl ttt}<br />
4.原本自家是{$indexurl } 现在自家被改成了index.php<br />
5.自个儿还能 echo 出来呢<br />
{echo $ec}<br />
6.其实小编还足以加减乘除的 6*7*8
{echo 6*7*8;}
7.常用的就那些了 还应该有哪些不懂的 <br />

</body>
</html>

新建 tpl/default/ttt.html
新建 tpp目录 ok了

本文由大发体育娱乐在线发布于大发体育网络,转载请注明出处:领到出来的模板类,网页模版生成类

关键词:

最火资讯