php tpl模板引擎定义与使用示例
作者:人生如初见_张默 发布时间:2023-11-14 22:04:49
标签:php,模板引擎
本文实例讲述了php tpl模板引擎定义与使用。分享给大家供大家参考,具体如下:
tpl.php
<?php
namespace tpl;
/**
* Class Tpl
*/
class Tpl
{
protected $view_dir;//模板文件
protected $cache_dir;//缓存文件
protected $lifetime;//过期时间
protected $vars = [];//存放显示变量的数组
/**
* Tpl constructor.
* @param string $view_dir
* @param string $cache_dir
* @param string $lifetime
*/
public function __construct($view_dir='', $cache_dir='', $lifetime='')
{
//如果模板文件不为空,则设置,为空则为默认值
if (!empty($view_dir)) {
if ($this->check_dir($view_dir)) {
$this->view_dir = $view_dir;
}
}
//如果缓存文件不为空,则设置,为空时为默认值
if (!empty($cache_dir)) {
if ($this->check_dir($cache_dir)) {
$this->cache_dir = $cache_dir;
}
}
//如果过期时间不为空,则设置,为空时为默认值
if (!empty($lifetime)) {
$this->lifetime = $lifetime;
}
}
/**
* 对外公开的方法
* @param string $name
* @param string $value
*/
public function assign($name, $value)
{
$this->vars[$name] = $value;//将传入的参数以键值对存入数组中
}
/**
* 测试文件
* @param $dir_path
* @return bool
*/
protected function check_dir($dir_path)
{
//如果文件不存在或不是文件夹,则创建
if (!file_exists($dir_path) || !is_dir($dir_path)) {
return mkdir($dir_path, 0777, true);
}
//如果文件不可读或不可写,则设置模式
if (!is_writable($dir_path) || !is_readable($dir_path)) {
return chmod($dir_path, 0777);
}
return true;
}
/**
* 展示方法
* @param $view_name
* @param bool $isInclude
* @param null $uri
*/
public function display($view_name, $isInclude=true, $uri=null)
{
//通过传入的文件名,得到模板文件路径
$view_path = rtrim($this->view_dir, '/') . '/' . $view_name;
//判断路径是否存在
if (!file_exists($view_path)) {
die('文件不存在');
}
//通过传入的文件名得到缓存文件名
$cache_name = md5($view_name . $uri) . '.php';
//缓过缓存文件名得到缓存路径
$cache_path = rtrim($this->cache_dir, '/') . '/' .$cache_name;
//判断缓存文件是否存在,如果不存在,重新生成
if (!file_exists($cache_path)) {
$php = $this->compile($view_path);//解析模板文件
file_put_contents($cache_path, $php);//缓存文件重新生成
} else {
//如果缓存文件存在,判断是否过期,判断模板文件是否被修改
$is_time_out = (filectime($cache_path) + $this->lifetime) > time() ? false : true;
$is_change = filemtime($view_path) > filemtime($cache_path) ? true : false;
//如果缓存文件过期或模板文件被修改,重新生成缓存文件
if ($is_time_out || $is_change) {
$php = $this->compile($view_path);
file_put_contents($cache_path, $php);
}
}
if ($isInclude) {
extract($this->vars);//解析传入变量的数组
include $cache_path;//展示缓存
}
}
/**
* 正则解析模板文件
* @param string $file_name
* @return mixed|string
*/
protected function compile($file_name)
{
$html = file_get_contents($file_name);//获取模板文件
//正则转换数组
$array = [
'{$%%}' => '<?=$\1?>',
'{foreach %%}' => '<?php foreach (\1): ?>',
'{/foreach}' => '<?php endforeach ?>',
'{include %%}' => '',
'{if %%}' => '<?php if (\1): ?>',
'{/if}' => '<?php endif ?>',
'{for %%}' => '<?php for (\1): ?>',
'{/for}' => '<?php endfor ?>',
'{switch %%}' => '<?php switch (\1) ?>',
'{/switch}' => '<?php endswitch ?>'
];
//遍历数组,生成正则表达式
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;
}
/**
* 处理include表达式
* @param array $data
* @return string
*/
protected function parseInclude($data)
{
$file_name = trim($data[1], '\'"');
$this->display($file_name, false);
$cache_name = md5($file_name) . '.php';
$cache_path = rtrim($this->cache_dir, '/') . '/' . $cache_name;
return '<?php include "'.$cache_path.'" ?>';
}
}
user_tpl,,,,从数据库中取值,作为参数传到模板文件,再解析模板文件
<?php
include './sql/pdo.sql.php';
include 'tpl.php';
$tpl = new tpl\Tpl('./view/', './cache/', 3000);
$link = new pdo_sql();
$dat = ['menu_name', 'menu_url'];
$res = $link->table('blog_menu')->field($dat)->order('id ASC')->select();
$tpl->assign('menu', $res);
$tpl->display('index.html');
希望本文所述对大家PHP程序设计有所帮助。
来源:https://blog.csdn.net/qq_42176520/article/details/80426384
0
投稿
猜你喜欢
- 动手写个小组件(组件入门) 这篇文章主要是为想将自己的ASP水平提高的人写的! 把ASP代码变成组件,开发者不仅是加快了ASP的速度,而且也
- 建立网站时,有很多理由让你遵循Web标准。企业、开发人员和用户都会从兼容标准的做法中获益。这里列举了一些最令人信服的理由,为什么所有网站都应
- 前段时间嗷嗷有发过"好玩的放大镜效果",今天看了下,发现还有简单的方法也能够实现,即利用内外补丁的调整。有兴趣的可以在琢
- 本文中介绍的系统优化,主要针对前端和后台这两方面(后台方面主要对SQL语句和数据存储进行了优化),下文中我们将介绍一些优化技巧和经验。技巧:
- Notepad++是一款不错的编辑器,很轻巧,我很喜欢它。再换个主题,加个代码高亮,看上去就更专业了。如果你是Mac用户,应该听说或使用过T
- 在默认情况下,Access 2000/2002数据库是以“共享”的方式打开的,这样可以保证多人能够同时使用同一个数据库。不过,在共享方式打开
- 上次在blueidea上看到一个元素圆角的实现方法,但是那个太复杂了。于是就自己写了一个函数,可以将元素自动圆角,如div层,表格等。共有四
- MySQL中模式就是数据库SHOW DATABASES;show databases;罗列所有数据库名称CREATE DATABASE &l
- <table> <tr> &nb
- 在改进SQL Server 7.0系列所实现的安全机制的过程中,Microsoft建立了一种既灵活又强大的安全管理机制,它能够对用户访问SQ
- 使用select @@identity 得到刚插入数据的ID1.适用于所有 ADO 版本<%Dim loConn, 
- 阅读上一篇:网马解密大讲堂——网马解密中级篇(Eval篇) 一.Document.write 函数简介:在Microsoft JScript
- 前言大家好!这个系列文章是W3CN 阿捷编写的。是一些制作过程中的心得和经验,希望对大家有点帮助。第一天开始制作符合标准的站点,第一件事情就
- 这篇论坛文章(赛迪网技术社区)详细的介绍了在MySQL中使用GRANT语句增添新用户的具体步骤,更多内容请参考下文…&
- 直接给源代码了:$current_dir = 'E:/temp/';$dir = opendir($current_dir)
- 判断服务器是否安装了某种asp组件,比较常用的代码如下:代码如下:<% '功能:检查是否存在系统组件或组件是否安装成功
- gzip 是什么东东呢?百科跟我们说gzip是GNU zip的缩写,它是一个 GNU 自由软件的文件压缩程序。…gzip 的基础是 DEFL
- ASP错误大全 Microsoft VBScript语法错误(0x800A03E9)-->内存不足 Microsoft VBScript语法
- 写入:1:把gif图像文件读入内存(一个变量strTemp)。2:写入数据库。Dim binTmp() As ByteDim conn As
- 本文实例讲述了Python基于正则表达式实现文件内容替换的方法。分享给大家供大家参考,具体如下:最近因为有一个项目需要从普通的服务器移植到S