php写简单的模板tpl

这是模板文件

<?php
class Tpl
{
	//模版文件的路径
	protected $viewDir='./view/';
	//生成缓存文件的路径
	protected $cacheDir='./cache/';
	//过期时间
	protected $lifeTime=3600;
	//用来存放显示变量的数组
	protected $vars=array();
	//构造方法对成员变量进行初始化
	function __construct($viewDir=null,$cacheDir=null,$lifeTiem=null)
	{
		if(!empty($viewDir))
		{
			if($this->checkDir($viewDir))
			{
				$this->viewDir=$viewDir;
			}
		}
		if(!empty($cacheDir))
		{
			if($this->checkDir($cacheDir))
			{
				$this->cacheDir=$cacheDir;
			}
		}
		if(!empty($lifeTiem))
		{
			$this->lifeTiem=$lifeTiem;
		}
	}
	
	//判断目录路径是否为路径或文件夹
	protected function checkDir($dirPath)
	{
		if(!file_exists($dirPath) || !is_dir($dirPath))
		{
			return mkdir($dirPath,0755,true);
		}
		
		if(!is_writable($dirPath) || is_readable($dirPath))
		{
			return chmod($dirPath,0755);
		}
		return true;
	}
	
	//需要对外公开的方法
	//分配变量方法
	//$title='日本';$tpl->assign('title',$title);
	function assign($key,$value)
	{
		$this->vars[$key]=$value;
	}
	
	//展示缓存文件方法
	/*
	*$viewName:模板文件名
	*$isInclude:模板文件是仅仅需要编译,还是先编译再包含进来
	*$uri:index.php?page=1,page=1就是那个uri,为了让缓存的文件名不重复,将文件名和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->compile($viewPath);
			//写入文件,生成缓存文件
			file_put_contents($cachePath,$php);
		}
		else
		{
			//如果缓存文件不存在,编译模板文件生成缓存文件
			//如果缓存文件存在,1、判断缓存文件是否过期,2、判断模板文件是否被修改过,如果被修改过,缓存文件需要重新生成
			$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;
		}
	}
	
	//compile方法,编译html文件
	protected function compile($filePath)
	{
		//读取文件内容
		$html=file_get_contents($filePath);
		//正则替换
		$array=[
			'{$%%}'=>'<?=$\1; ?>',
			'{foreach %%}'=>'<?php foreach (\1): ?>',
			'{/foreach}'=>'<?php endforeach?>',
			'{include %%}'=>'',
			'{if %%}'=>'<?php if (\1): ?>',
			'{for %%}'=>'<?php for (\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)
	{
		//将文件名两的引号去除掉,preg_replace_callback()中$data[1]表示获取到第1个子集,$data[0]表示全部
		$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=['足球','世界杯'];

$head="这次世界谁是冠军";
$tpl->assign('title',$title);
$tpl->assign('data',$data);
$tpl->assign('head',$head);
$tpl->display('test.html');
?>

这是在view里面的test.html文件

<html>
<head>
<meta charset="utf-8">
<title>{$title}</title>
</head>
<body>
{include head.html}
{foreach $data as $value}
	{$value}<br/>
{/foreach}
</body>
</html>

这是在view里面的head.html文件

<div>{$head}</div>

这简单的模板文件可以分别写出简单的功能板块
如头部文件、底部文件、及登陆块等

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值