php 自定义模板,自定义简单的PHP模板引擎

目录结构:

a2a7c1f876f3365a3230205f0ecfea00.pngcache: 缓存目录

comp: 编译目录

temp: 模板目录template.php 文件:class template

{

// 模板文件目录

protected static $temp_dir = 'temp';

// 编译文件目录

protected static $comp_dir = 'comp';

// 缓存文件目录

protected static $cache_dir = 'cache';

// 模板变量

protected static $temp_var = [];

// 模板内容

protected static $content = '';

// 是否开启缓存

public $is_cache = false;

// 模板编译

private static function parse($parse_file)

{

// 解析变量正则表达式

$pattern = '/\{\$([\w\d]+)\}/';

// 变量名转换成对应的 PHP 语句

if (preg_match($pattern, self::$content)) {

self::$content = preg_replace($pattern, '<?php  echo self::$temp_var["$1"] ?>', self::$content);

}

// 编译完成, 生成编译文件, 否则输出错误

if (!file_put_contents($parse_file, self::$content)) {

exit('编译文件生成失败, 请检查~');

}

}

// 模板变量注入

public function assign($var, $value = null)

{

// 如果第一参数存在且不为空, 则将第二参数赋值给数组, 以第一参数为键名

if (isset($var) && !empty($var)) {

self::$temp_var[$var] = $value;

} else {

// 否则输出错误

exit('模板变量未设置');

}

}

// 模板文件编译

public function display($file)

{

// 检测目录是否存在, 没有则创建指定文件夹

if (!is_dir(self::$temp_dir)) {

mkdir(temp, 0777, true);

}

if (!is_dir(self::$comp_dir)) {

mkdir(comp, 0777, true);

}

if (!is_dir(self::$cache_dir)) {

mkdir(cache, 0777, true);

}

// 模板文件

$tpl_file = self::$temp_dir. '/' . $file . '.html';

// 判断模板文件是否存在

if (!file_exists($tpl_file)) {

exit('模板文件不存在!');

}

// 编译后的文件路径

$parse_file = self::$comp_dir. '/' . md5($file, true) . $file . '.php';

// 如果编译后的文件不存在或者模板文件被修改了, 重新编译文件

if (!file_exists($parse_file) || filemtime($parse_file) 

// 模板内容赋值

self::$content = file_get_contents($tpl_file);

// 执行编译方法

self::parse($parse_file);

}

// 如果开启了缓存, 则首先判断缓存文件, 否则直接加载编译文件

if ($this -> is_cache) {

// 缓存文件

$cache_file = self::$cache_dir . '/' . md5($file, true) . $file . '.html';

// 如果存在文件不存在或者编译文件被修改, 重新生成缓存文件

if (!file_exists($cache_file) || filemtime($cache_file) 

// 打开缓存区

ob_start();

// 先引入编译后的文件

include $parse_file;

// 获取缓存区的内容并删除当前缓存区数据

$content = ob_get_clean();

// 把内容写入到缓存文件, 如果写入失败则输出错误, 否则引入缓存文件

if (!file_put_contents($cache_file, $content)) {

exit('缓存文件生成失败, 请检查~');

} else {

include $cache_file;

}

} else {

// 如果缓存文件已存在则直接引入

include $cache_file;

}

} else {

// 没开启缓存则直接引入编译后的文件

include $parse_file;

}

}

}demo.php 文件:require_once 'template.php';

$tpl = new template();

// 要开启设置为 true

$tpl -> is_cache = false;

$webname = 'webname';

$author = 'author';

$title = 'webtitle';

$content = 'test content';

$tpl -> assign('webname', $webname);

$tpl -> assign('author', $author);

$tpl -> assign('title', $title);

$tpl -> assign('content', $content);

$tpl -> display('demo');demo.html 文件:html>

{$title}

{$webname}

Author: {$author}

Content: {$content}

输出:

32b87976e1ab0f68f57f318b2e6382bf.png

d147beb1f1dbf6a464d6c38f37daefd4.png==  缓存已测试通过, 如果数据经常修改, 不建议开启缓存  ==

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值