一个编译型的PHP模板引擎大致实现过程

没啥解说。。直接代码:

 JTemplate.class.php

<?php
/**
 * @author  Jiawei
 * @Completed in 2012-6-29 0:23
 */
class JTemplate{
	//通过assign函数传入的变量临时存放数组
	private $templateVar = array();
	//模板目录
	private $templateDir = '';
	//编译目录
	private $templateCompileDir = '';
	
	private $fileName = '';
	/**
	 * 构造函数
	 * @param string $templateDir 模板目录
	 * @param string $templateComplieDir 模板编译目录
	 */
	public function __construct($templateDir,$templateComplieDir){
		$this->templateDir = $templateDir;
		$this->templateCompileDir = $templateComplieDir;	
	}
	/**
	 * 显示模板
	 * @param string $fileName 模板文件名
	 */
	public function display($fileName){
		$this->fileName = $fileName;
		if(file_exists($this->templateDir.'/'.$this->fileName)){
			$compileFileName = $this->templateCompileDir.'/'.$this->file_safe_name().'.php';
			if(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.'/'.$this->fileName)){
				$this->del_old_file();
				$this->compile();
			}
			extract($this->templateVar);
			include $compileFileName;
		}else{
			$this->error('Sorry,the template file '.$this->fileName.' does not exist!!');
		}
	}
	/**
	 * 获取编译文件名
	 */
	private function get_compile_file(){
		$compileFile = explode('.',$this->fileName);
		unset($compileFile[count($compileFile)-1]);
		return implode('.',$compileFile);
	}
	/**
	 * 编译
	 */
	private function compile(){
		$fileHandle = @fopen($this->templateDir.'/'.$this->fileName, 'r');
		while(!feof($fileHandle)){
			$fileContent = fread($fileHandle,1024);
		}
		fclose($fileHandle);
		$fileContent = $this->template_replace($fileContent);
		//$compileFile = $this->get_compile_file($fileName);
		$fileHandle = @fopen($this->templateCompileDir.'/'.$this->file_safe_name().'.php','w');
		if($fileHandle){
			fwrite($fileHandle, $fileContent);
			fclose($fileHandle);
		}else{
			$this->error('Sorry,Compile dir can not write!');
		}
	}
	/**
	 * 模板传值
	 * @param string $valueName 模板中使用的变量名
	 * @param $value 变量值
	 */
	public function assign($valueName,$value){
		$this->templateVar[$valueName] = $value;
	}
	
	/**
	 * 模板正则替换
	 * @param string $content 替换内容
	 * @return  string 替换过后的内容
	 */
	private function template_replace($content){
		$orginArray = array(
			'/<!--loop\s+\$(\w+)\s+\$(\w+)-->/s',
			'/<!--loop\s+\$(\w+)\s+\$(\w+)\s+\$(\w+)-->/s',
       		'/<!--elseloop-->(.+?)<!--endloop-->/s',
       		'/<!--endloop-->/s',
       		'/<!--if\s+\((.+?)\)-->/s',
			'/<!--endif-->/s',
			'/<!--elseif\s+\((.+?)\)-->/s',
			'/<!--else-->/s',
			'/\{P:(.+?)\:}/s',
			'/\{C:(\w+)\}/s',
			'/\{I:(.+?)\}/s',
			'/\{F:(.+?)\}/s',
			'/\{EF:(.+?)\}/s',
			'/\{([a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s',
		);
		
		$changeArray = array(
			'<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2){$countLoop++;?>',
			'<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2=>$$3){$countLoop++;?>',
       		'<?php }if(!empty($countLoop))$countLoop--;}else{?>$1<?php }?>',
       		'<?php }if(!empty($countLoop))$countLoop--;}?>',
       		'<?php if($1){?>',
       		'<?php }?>',
       		'<?php }elseif($1){?>',
       		'<?php }else{?>',
       		'<?php $1?>',
       		'<?php echo $1;?>',
       		'<?php include_once "'.$this->templateDir.'/$1";?>',
       		'<?php $1;?>',
       		'<?php echo $1;?>',
       		'<?php echo $$1;?>',
		);
		return $repContent=preg_replace($orginArray,$changeArray,$content);
	}
	/**
	 * 删除旧文件
	 */
	private function del_old_file(){
		$compileFile = $this->get_compile_file($this->fileName);
		$files = glob($this->templateCompileDir.'/'.$compileFile.'*.php');
		// print_r($files);
		if($files){
			@unlink($files[0]);
		}
	}
	/**
	 * 编译文件名安全处理方法
	 * @return  string 返回编译文件名
	 */
	private function file_safe_name(){
		$compileFile = $this->get_compile_file($this->fileName);
		return $compileFile.filemtime($this->templateDir.'/'.$this->fileName);
	}
	
	/**
	 * 错误输出函数
	 * @param string $content 错误输出信息
	 */
	private function error($content){
		$stringHtml = '<div style="width:780px;height:auto;padding:10px;border:1px solid #CCC;margin:0 auto;">';
		$stringHtml .= 'Error information:<br />';
		$stringHtml .= '<font color="red">';
		$stringHtml .= $content;
		$stringHtml .= '</font>';
		$stringHtml .= '</div>';
		exit($stringHtml);
	}
}
?>

index.php

<?php
/**
 * @author Jiawei
 */
include_once 'JTemplate/JTemplate.class.php';
$template = new JTemplate('template','compile');
$a = array('a','b','c','d');
define('_CORE_','aaa');
$template->assign('a', $a);
$template->display('index.html');
?>

 

转载于:https://my.oschina.net/cdark/blog/64790

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值