å建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'; ?>