模板引擎类

模板引擎类

在html中穿插php代码,但是文件后缀名是php,为了让前端和后端的代码分离,后端就是.php用来获取数据的,前端就是.html用来展示数据的

preg_replace:将正则匹配到的字符串转换为指定的字符串

preg_replace_callback:

preg_quote:定界符 原子 元字符 模式修正符

Tpl.php代码:

<?php
class Tpl{
	//模板文件的路径
	protected $viewDir='./view';
	
	//生成的缓存文件的路径
	protected $cacheDir='./cache';
	//过期时间
	protected $lifeTime=3600;
	protected $vars=[];//用来存放显示变量的数组
	function __construct($viewDir=null,$cacheDir=null,$lifeTime=null){
		if(!empty($viewDir)){
			if($this->checkDir($viewDir)){//判断所传路径是否是目录
				$this->viewDir=$viewDir;
			}
		}
		if(!empty($cacheDir)){
			if($this->checkDir($cacheDir)){
				$this->cacheDir=$cacheDir;
			}
		}
		if(!empty($liftTime)){
			$this->lifeTime=$lifeTime;
		}
	}
	protected function checkDir($dirPath){
		//如果目录不存在或者不是目录那面创建该目录
		if(!file_exists($dirPath)||!is_dir($dirPath)){
			return mkdir($dirPath,0755,true);
		}
	if(!is_writable($dirPat)||!is_readable($dirPath)){
		return chmod($dirPath,0755);
	}
	}
	//需要对外公开的方法
	//分配变量方法
	function assign($name,$value){
		$this->vars[$name]=$value;
	}
	//展示缓存文件方法$viewName模板文件名,模板文交是仅仅需要编译,还是先编译再包含进来,index.php?page=1为了让缓存的文件名不重复,将文件名和uri拼接起来在md5一下,生成缓存的文件名
	function display($viewName,$isInclude=true,$uri=null){
		//拼接模板文件的全路径
		$viewPath=rtrim($this->viewDir,'/').'/'.$viewName;
		if(!file_exists($viewPath)){
			die('模板文件不存在');
		}
		//拼接缓存文件的全路径
		$cacheName=md5($viewName.$uri).'.php';
		$cachePath=rtrim($this->cacheDir,'/').'/'.$cacheName;
		//根据缓存文件全路径判断缓存文件是否存在
		if(!file_exists($cachePath)){
			$php=$this->copile($viewPath);//编译模板文件
			file_put_contents($cachePath,$php);//写入文件,生成缓存文件
		}else{
			$isTimeout=(filectime($cachePath)+$this->lifeTime>time())?false:true;
			$isChange=filemtime($viewPath)>filemtime($cachePath)?true:false;
			if($isTimeout||$isChange){
				$php=$this->compile($viewPath);
				file_put_contents($cachePath,$php);
			}
		}
		//如果缓存文件不存在,编译模板文件,生成缓存文件
		//如果缓存文件存在,第一个判断缓存文件是否过期,第二个判断模板文件是否被修改,如果模板文件被修改过缓存文件需要重新生成
		//判断缓存文件是否需要包含进来
		if($isInclude){
			extract($this->vars);
			//展示缓存文件
			include $cachePath;
		}
	}
	protected function compile($filePath){
		//读取文件内容
		$html=file_get_contents($filePath);
		//正则替换
		$array=['{%%}'=>'<?=$\1;?>',
				'{foreach%%}'=>'<?php foreach(\1):?>',
				'{/foreach}'=>'<?php endforeach?>',
				'{include %%}'=>'',
				'{if %%}'=>'<?php if(\1):?>',];
		//遍历数组%%全部修改为.+,然后执行正则替换
		foreach($array as $key=>$value){
			//生成正则表达式
			$pattern='#'.str_replace('%%','(.+?)',preg_quote($key,'#')).'#';
		//实现正则替换
		if(strstr($pattern,'include')){
			$html=preg_replace_callback($pattern,[$this,'parseInclude'],$html);
		}else{
			//执行替换
			$html=preg_replace($pattern,$value,$html);
		}
		}
		return $html;
		//返回替换后的内容
		
	}
	protected function parseInclude($data){
		//处理include正则表达式,$data就是匹配到的内容
		//将文件名两边的引号去掉
		$fileName=trim($data[1],'\'"');
		//然后不包含文件生成缓存
		$this->display($fileName,false);
		//拼接缓存文件全路径
		$cacheName=md5($fileName).'php';
		$cachePath=rtrim($this->cacheDir,'/').'/'.$cacheName;
		return '<?php include "'.$cachePath.'"?>';
	}
}
?>
test.php

<?php
include "Tpl.php";
$tpl=new Tpl();
$title='你好';
$data=['科比','韦德'];
$tpl->assign('title',$title);
$tpl->assign('data',$data);
$tpl->display('test.html');
?>
test.html
<html>
<head>
<title>{$title}</title>
</head>
<body>
{foreach $data as $value}
{$value}<br/>
{/foreach}
</body>
</html>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值