创建TPL自定义模板

文件布局

<!--1d7c7a527b6335cc7a623305ca940e1findex.tpl.html-->
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title>小明在线</title>
</head>
<body>
小明你在干嘛?<!--我是静态页面-->
<hr/>

<div>我是二号页面</div>
<hr/>

0...1<br/>
1...2<br/>
2...3<br/>
3...4<br/>
4...5<br/>
5...6<br/>
6...7<br/>

分页数是10</body>
</html>



<!--系统变量文件 profile.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<root>
<taglib>
<name>webname</name>
<value>小明在线</value>
</taglib>

<taglib>
<name>pagesize</name>
<value>10</value>
</taglib>
</root>



<!--Parser.class.php解析类-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/28
* Time: 10:27
*/
//模板解析类
class Parser {
//字段保存模板类内容
private $_tpl;

function __construct($_tplFile){
if(!$this->_tpl=file_get_contents($_tplFile)){
exit('ERROR:模板文件读取错误!');
}
}

//解析普通变量
private function parVar(){
$_pattern='/{$([w]+)}/';
if(preg_match($_pattern,$this->_tpl)){
$this->_tpl=preg_replace($_pattern,"<?php echo $this->_vars['$1'];?>",$this->_tpl);
}
}

//解析if else语句
private function parIf(){
$_patternStartIf='/{ifs+$([w]+)}/';
$_patternEndIf='/{/if}/';
$_patternStartElse='/{else}/';
$_patternEndElse='/{/else}/';
if(preg_match($_patternStartIf,$this->_tpl)){
$this->_tpl=preg_replace($_patternStartIf,"<?php if($this->_vars['$1']){?>",$this->_tpl);
if(preg_match($_patternEndIf,$this->_tpl)){
$this->_tpl=preg_replace($_patternEndIf,"<?php }?>",$this->_tpl);
if(preg_match($_patternStartElse,$this->_tpl)){
$this->_tpl=preg_replace($_patternStartElse,"<?php }else{?>",$this->_tpl);
}
}else{
exit('ERROR:if语句没有关闭!');
}
}
}

//PHP注释解析
private function parCommon(){
$_patternCommon='/{#(.*)#}/';
if(preg_match($_patternCommon,$this->_tpl)){
$this->_tpl=preg_replace($_patternCommon,"<?php /* $1 */?>",$this->_tpl);
}else{
exit('ERROR:PHP注释解析错误!');
}
}

//foreach语句解析
private function parForeach(){
$_patternStartForeach='/{foreachs+$([w]+)(([w]+),([w]+))}/';
$_patternEndForeach='/{/foreach}/';
$_patternKeyOrValue='/{@([w]+)}/';
if(preg_match($_patternStartForeach,$this->_tpl)){
if(preg_match($_patternEndForeach,$this->_tpl)){
$this->_tpl=preg_replace($_patternStartForeach,"<?php foreach($this->_vars['$1'] as $$2=>$$3){?>",$this->_tpl);
$this->_tpl=preg_replace($_patternEndForeach,"<?php }?>",$this->_tpl);
if(preg_match($_patternKeyOrValue,$this->_tpl)){
$this->_tpl=preg_replace($_patternKeyOrValue,"<?php echo $$1;?>",$this->_tpl);
}
}else{
exit('ERROR:foreach语句没有关闭!');
}
}
}

//include语句解析
private function parInclude(){
$_patternInclude='/{includes+file="([w.\_]+)"}/';
if(preg_match($_patternInclude,$this->_tpl,$_file)){
if(!file_exists($_file[1])||empty($_file)){
exit('ERROR:包含文件出错!');
}
$this->_tpl=preg_replace($_patternInclude,"<?php include ROOT_PATH.'/$1';?>",$this->_tpl);
}
}

//系统变量解析
private function parConfig(){
$_patternConfig='/<!--{([w]+)}-->/';
if(preg_match($_patternConfig,$this->_tpl)){
$this->_tpl=preg_replace($_patternConfig,"<?php echo $this->_config['$1'];?>",$this->_tpl);
}
}

//对外公共接口
public function compile($_parFile){
//解析模板内容
$this->parVar();
$this->parIf();
$this->parCommon();
$this->parForeach();
$this->parInclude();
$this->parConfig();
//生成编译文件
if(!file_put_contents($_parFile,$this->_tpl)){
exit('ERROR:编译文件出错!');
}
}
}

?>




<!--Templates.class.php模板类-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/28
* Time: 10:26
*/
//模板类
class Templates {
//动态接收 变量
private $_vars=array();
//保存系统变量
private $_config=array();

//创建一个构造方法判断各个目录是否存在
public function __construct()
{
if(!is_dir(TPL_DIR)||!is_dir(TPL_C_DIR)||!is_dir(CACHE_DIR)){
exit('ERROR:模板文件目录或编译文件目录或缓存文件目录不存在,请手动添加!');
}
$_sxe=simplexml_load_file('config/profile.xml');
$_taglib=$_sxe->xpath('/root/taglib');
foreach($_taglib as $_tag){
$this->_config["$_tag->name"]=$_tag->value;
}
}

//assign()方法,用于注入变量
public function assign($_var,$_value){
//$_var用于同步模板里的变量,$_var相当于index.php里的name,那么在index.tpl中就是{$name}
//$_value相当于index.php里的$_value,在index.php里就是$_name
if(isset($_var)&&!empty($_var)){
$this->_vars[$_var]=$_value;
}else{
exit('ERROR:请设置模板变量!');
}
}


//display方法
public function display($_file){
//设置模板路径
$_tplFile=TPL_DIR.$_file;
//判断模板文件是否存在
if(!file_exists($_tplFile)){
exit('ERROR:模板文件不存在!');
}
//编译文件
$_parFile=TPL_C_DIR.md5($_file).$_file.'.php';
//缓存文件
$_cacheFile=CACHE_DIR.md5($_file).$_file.'.html';
//如果第二次运行相同的文件,只需要载入缓存文件
if(IS_CACHE){
//编译文件和缓存文件必须同时存在
if(file_exists($_parFile)&&file_exists($_cacheFile)){
//编译文件和缓存文件没有修改过
if(filemtime($_parFile)>=filemtime($_tplFile)&&filemtime($_cacheFile)>=filemtime($_parFile)){
//载入缓存文件
include $_cacheFile;
return;
}
}
}
if(!is_file($_parFile)||filemtime($_parFile)<filemtime($_tplFile)){
//引入模板解析类
require ROOT_PATH.'/includes/Parser.class.php';
$_parser=new Parser($_tplFile);
$_parser->compile($_parFile);
}
//载入编译文件
include $_parFile;
if(IS_CACHE) {
//获取缓存区里的数据,并创建缓存文件
file_put_contents($_cacheFile, ob_get_contents());
//清除缓冲区里(编译文件加载的内容)
ob_clean();
//载入缓存文件
include $_cacheFile;
}
}
}

?>



<!--index.tpl模板文件-->
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title><!--{webname}--></title>
</head>
<body>
{$name}{$content}
<!--我是静态页面-->
{#我是php中注释#}
<hr/>

{if $a}
<div>我是一号界面</div>
{else}<div>我是二号页面</div>
{/if}
<hr/>

{foreach $array(key,value)}
{@key}...{@value}<br/>
{/foreach}

分页数是<!--{pagesize}-->
</body>
</html>



<!--由模板生成的1d7c7a527b6335cc7a623305ca940e1findex.tpl.php解析文件-->
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title><?php echo $this->_config['webname'];?></title>
</head>
<body>
<?php echo $this->_vars['name'];?><?php echo $this->_vars['content'];?>
<!--我是静态页面-->
<?php /* 我是php中注释 */?>
<hr/>

<?php if($this->_vars['a']){?>
<div>我是一号界面</div>
<?php }else{?><div>我是二号页面</div>
<?php }?>
<hr/>

<?php foreach($this->_vars['array'] as $key=>$value){?>
<?php echo $key;?>...<?php echo $value;?><br/>
<?php }?>

分页数是<?php echo $this->_config['pagesize'];?>
</body>
</html>



<!--主页面index.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/28
* Time: 10:02
*/
require dirname(__FILE__).'/template.inc.php';
//实例化模板类
$_tpl=new Templates();
//声明一个变量
$_name='小明';
$_content='你在干嘛?';
$_array=array(1,2,3,4,5,6,7);
//注入变量 name相当于index.tpl里的{$name}
$_tpl->assign('name',$_name);
$_tpl->assign('content',$_content);
$_tpl->assign('a',5<4);
$_tpl->assign('array',$_array);
//载入tpl文件
$_tpl->display('index.tpl');
?>



<!--template.inc.php辅助index.php的分担代码页面-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/29
* Time: 20:05
*/
//开启缓冲区
ob_start();
//设置utf-8编码
header('content-type:text/html;charset="utf-8"');
//网站根目录
define('ROOT_PATH',dirname(__FILE__));
//模板文件目录
define('TPL_DIR',ROOT_PATH.'/templates/');
//编译文件目录
define('TPL_C_DIR',ROOT_PATH.'/templates_c/');
//缓存文件目录
define('CACHE_DIR',ROOT_PATH.'/cache/');
//是否开启缓冲区
define('IS_CACHE',true);
//判断是否开启缓冲区
IS_CACHE?ob_start():null;
//引入模板类
require ROOT_PATH.'/includes/Templates.class.php';
?>
 
原文地址:https://www.cnblogs.com/xiaomingzaixian/p/4769731.html