龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

一个简单的模板引擎类,此类仅作研究并不完善

时间:2014-07-22 14:50来源: 作者: 点击:
分享到:
第一次在这里帖码,此份代码主要是PHP模板引擎技术研究,目前只有编译版本,希望各位多多提供意见和优化技巧 br / span三个文件组成,不知道如何以文件形式,只能复制了,抱歉!
第一次在这里帖码,此份代码主要是PHP模板引擎技术研究,目前只有编译版本,希望各位多多提供意见和优化技巧
三个文件组成,不知道如何以文件形式,只能复制了,抱歉!
index.php是一个配置文件,大伙看看就明白
index.html一些使用的例子
Templates.class.php基类
晚点发布下有缓存的完善版本,但希望没有在写缓存一些,有朋友或是高手指点下,这个模板引擎只要处理编译和缓存即可,其余考虑暂时不考虑,当然正则替换模式还要增加f,w之类。。。
希望有朋友可以研究研究本人Q:
76376931

Copy_3_of_Templates.class.php 文件是已经增加缓存方式的,再次刷新页面不会生成缓存,未考虑项目中某些页面是否要缓存,以后用该类在逐步添加,希望有朋友可以一起交流!

<?php
header('Content-Type:text/html;charset=utf-8');
define('ROOT_HOST',dirname(__FILE__));
define('HTML_DIR',ROOT_HOST.'/moban/');
define('COMPILED_DIR',ROOT_HOST.'/data/compiled/');
define('CACHE_DIR',ROOT_HOST.'/data/cache/');
//是否开启缓冲区
define('NEW_CACHE', false);
//判断是否开启缓冲区
NEW_CACHE ? ob_start() : null;
//引入模板类
require ROOT_HOST.'/lib/Templates.class.php';

$_moban = new Templates();

$array = array(a=>'你好呀',b=>'我不是很好,但我很想你',c=>'你都在家里了,怎么还想我呀?');
$xcvu = '你好啊,这是一个XCVU';
$zmq = "hi";
$title  = "这是一个模板引擎自定义方法!";
$ling = "因为正在修改一个“函数”????????????????";

$_moban->assign('ling', $ling);
$_moban->assign('title',$title);
$_moban->assign('zmq', $zmq);
$_moban->assign('xcvu', $xcvu);
$_moban->assign('abc',5>4);
$_moban->assign('array', $array);
$_moban->display('index.html');

?>

2. [文件] index.html ~ 856B     下载(7)     跳至 [1] [2] [3] [4] [全屏预览]

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><!-- $title --></title>
<style type="text/css">
*{ margin:0; padding:0;}
body{ font-size:12px; color:#fff; background:#999;}
.index { margin:0 auto; width:960px; background:#fff;height:1000px; line-height:50px; padding:20px;color:#000;}
.index a{color:#000; text-decoration:none;}
.index a:hover{ color:#F0F;}
</style>
</head>
<body>
<div class="index">
	<span style="color:#000;">BBBasd</span><span style="color:#000;">不知道说点什么好,可又想说点什么好</span><br />
	<a href="#"><!-- $ling --></a>
	<br />
	<!-- $xcvu -->
	<br />
	<!-- if $abc -->
	<P>1号</P>
	<!-- else -->
	<P>2号</P>
	<!-- /if -->
	<br />
	<!-- loop $array(k,v) -->
			<!-- @k -->........<!-- @v --><br/>
	<!-- /loop -->
	<!-- #这个是PHP注释 -->
</div>
</body>
</html>

3. [文件] Templates.class.php ~ 3KB     下载(6)     跳至 [1] [2] [3] [4] [全屏预览]

<?php
/* about:Richard.z
 * site:http://www.zmq.cc
 * E_mail:code@zmq.cc
 * date:2013/01/02/17:30
 * */
class Templates{
	private $_CaChe;
	private $_Compiled;
	private $_HtmlFile;
	private $_FileVar;
	private $_KeyArr = array();
	
	public function __construct(){
		if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){
			exit('Your directory does not exist!');
		}
	}
	
	public function assign($_var, $_value){
		if(isset($_var) && !empty($_var)){
			$this->_KeyArr[$_var] = $_value;
		}else{
			exit('Please set your value!');
		}	
	}
	
	public function display($_File){
		//设置模板的变量
		$this->_HtmlFile = HTML_DIR.$_File;
		//设置编译
		$this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
		//设置缓存
		$this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
		//判断模板是否存在
		if(!file_exists($this->_HtmlFile)){
			exit('Template file does not exist');
		}
		//赋值和判断读取
		if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){
			exit('The template file read error!');
		}
		//if edit Compiled File date < date HtmlFile 
		if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) < filemtime($this->_HtmlFile)){
			$this->Set_Comilled();
		}
		//Include Compiled
		include $this->_Compiled;		
	}

	//public function
	public function Set_Comilled(){
		$this->SetArr();
		$this->SetInclude();
		if(!file_put_contents($this->_Compiled, $this->_FileVar)){
			exit('Compiled files generated error!');
		}
	}
	//arr
	private function SetArr(){
		$_preaa = array(
			'/<\!--\s+\$([\w]+)\s+\-->/',
			'/<\!--\s+if\s+\$([\w]+)\s+\-->/',
			'/<\!--\s+\/if\s+\-->/',
			'/<\!--\s+else\s+\-->/',
			'/<\!--\s+loop\s+\$([\w]+)\(([\w]+),([\w]+)\)\s+\-->/',
			'/<\!--\s+\/loop\s+\-->/',
			'/<\!--\s+@([\w]+)\s+\-->/',
			'/<\!--\s+\#(.*)\s+\-->/');
		$_prebb = array(
			'<?php echo \$this->_KeyArr["$1"];?>',
			'<?php if (\$this->_KeyArr["$1"]) {?>',
			'<?php } ?>',
			'<?php } else { ?>',
			'<?php foreach (\$this->_KeyArr["$1"] as \$$2=>\$$3) { ?>',
			'<?php } ?>',
			'<?php echo \$$1; ?>',
			'<?php /* $1 */ ?>');
		$this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
		if(preg_match($_preaa[0], $this->_FileVar)){
		$this->_FileVar = $this->SetArr($this->_FileVar);
			}
	}

	//Include
	private function SetInclude(){
		$_preFile = '/<\!--\s+include\s+file=\"([\w\.\-]+)\"\s+\-->/';
		if(preg_match($_preFile, $this->_FileVar,$_File)){
			if(!file_exists($_File[1]) || empty($_File)){
				exit('You of Include File Error!');
			}
			$this->_FileVar = preg_replace($_preFile, "<?php include '$1'; ?>", $this->_FileVar);
		}
	}
	
}
?>

4. [文件] Copy_3_of_Templates.class.php ~ 3KB     下载(9)     跳至 [1] [2] [3] [4] [全屏预览]

<?php
/* about:Richard.z
 * site:http://www.zmq.cc
 * E_mail:code@zmq.cc
 * date:2013/01/02/17:30 || 2013/01/14/21:35
 * */
class Templates{
	private $_CaChe;
	private $_Compiled;
	private $_HtmlFile;
	private $_FileVar;
	private $_KeyArr = array();
	
	public function __construct(){
		if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){
			exit('Your directory does not exist!');
		}
	}
	
	public function assign($_var, $_value){
		if(isset($_var) && !empty($_var)){
			$this->_KeyArr[$_var] = $_value;
		}else{
			exit('Please set your value!');
		}	
	}
	
	public function display($_File){
		//设置模板的变量
		$this->_HtmlFile = HTML_DIR.$_File;
		//设置编译
		$this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
		//设置缓存
		$this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
		//判断模板是否存在
		if(!file_exists($this->_HtmlFile)){
			exit('Template file does not exist');
		}
		//赋值和判断读取
		if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){
			exit('The template file read error!');
		}
		//if edit Compiled File date < date HtmlFile 
		if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) < filemtime($this->_HtmlFile)){
			$this->Set_Comilled();
		}
		//Include Compiled
		include $this->_Compiled;
		$this->SetCaChe();
		}

		//The setting cache file if you want to be generated again
		private function SetCaChe(){
			if(!file_exists($this->_CaChe) || filemtime($this->_CaChe) < filemtime($this->_Compiled)){
				if(NEW_CACHE){
					file_put_contents($this->_CaChe, ob_get_contents());
					ob_end_clean();
					include $this->_CaChe;
				}
			}
		}

	//public function
	public function Set_Comilled(){
		$this->SetArr();
		$this->SetInclude();
		if(!file_put_contents($this->_Compiled, $this->_FileVar)){
			exit('Compiled files generated error!');
		}
	}
	//arr
	private function SetArr(){
		$_preaa = array(
			'/<\!--\s+\$([\w]+)\s+\-->/',
			'/<\!--\s+if\s+\$([\w]+)\s+\-->/',
			'/<\!--\s+\/if\s+\-->/',
			'/<\!--\s+else\s+\-->/',
			'/<\!--\s+loop\s+\$([\w]+)\(([\w]+),([\w]+)\)\s+\-->/',
			'/<\!--\s+\/loop\s+\-->/',
			'/<\!--\s+@([\w]+)\s+\-->/',
			'/<\!--\s+\#(.*)\s+\-->/');
		$_prebb = array(
			'<?php echo \$this->_KeyArr["$1"];?>',
			'<?php if (\$this->_KeyArr["$1"]) {?>',
			'<?php } ?>',
			'<?php } else { ?>',
			'<?php foreach (\$this->_KeyArr["$1"] as \$$2=>\$$3) { ?>',
			'<?php } ?>',
			'<?php echo \$$1; ?>',
			'<?php /* $1 */ ?>');
		$this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
		if(preg_match($_preaa[0], $this->_FileVar)){
		$this->_FileVar = $this->SetArr($this->_FileVar);
			}
	}

	//Include
	private function SetInclude(){
		$_preFile = '/<\!--\s+include\s+file=\"([\w\.\-]+)\"\s+\-->/';
		if(preg_match($_preFile, $this->_FileVar,$_File)){
			if(!file_exists($_File[1]) || empty($_File)){
				exit('You of Include File Error!');
			}
			$this->_FileVar = preg_replace($_preFile, "<?php include '$1'; ?>", $this->_FileVar);
		}
	}
	
}
?>
精彩图集

赞助商链接