創建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='/\{if\s+\$([\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='/\{foreach\s+\$([\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='/\{include\s+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';
?>
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM