<?php
class tmd_tpl {
public $tpl_dir = './tpl/';
// 模板文件所在目录 注意以斜杠结束
public $tpl_ext = '.php';
// 模板文件扩展名 建议为PHP文件 这样在Dreamweaver中PHP代码才会有高亮效果
public $cache_dir = './tpl_c/';
// 模板编译后的缓存目录 一样以斜杠结束 无则自动创建
public $cache_time = 0;
// 编译后模板的缓存时间(单位:秒)
// 0为马上过期 即每次都重新编译
// -1为永不过期 网站上线后建议设置为-1
// 自定义的正则替换
public $my_rep = array(
'~(\.\./)+static~' => '/static',
'~__UP__~' => '/static/upload',
// '~__PROJ__~' => '/index.php',
// '~__SPACE__~' => '/index.php/home',
// '~__CLASS__~' => '/index.php/home/index',
// '~__METHOD__~' => '/index.php/home/index/index',
);
// 内置的正则替换
private $tmd_rep = array(
'~\{(\$[a-z0-9_]+)\}~i'
=> '<?php echo $1 ?>', // {$name}
'~\{(\$[a-z0-9_]+)\.([a-z0-9_]+)\}~i'
=> '<?php echo $1[\'$2\'] ?>', // {$arr.key}
'~\{(\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\}~i'
=> '<?php echo $1[\'$2\'][\'$3\'] ?>', // {$arr.key.key2}
'~<\?php\s+(include_once|require_once|include|require)\s*\(\s*(.+?)\s*\)\s*;?\s*\?>~i'
=> '<?php include \$this->_include($2, __FILE__) ?>', // <?php include('inc/top.php'); ?>
'~\{:(.+?)\}~' => '<?php echo $1 ?>', // {:strip_tags($a)}
'~\{\~(.+?)\}~' => '<?php $1 ?>', // {~var_dump($a)}
'~<\?=\s*~' => '<?php echo ', // <?=
);
// 用于存储模板变量
public $data = array();
function __construct($cfg=NULL) {
if ($cfg) {
$this->config($cfg);
}
}
function config($cfg) {
if (is_string($cfg)) {
$cfg = require $cfg;
}
if (isset($cfg['tpl_dir'])) {
$this->tpl_dir = $cfg['tpl_dir'];
}
if (isset($cfg['tpl_ext'])) {
$this->tpl_ext = $cfg['tpl_ext'];
}
if (isset($cfg['cache_dir'])) {
$this->cache_dir = $cfg['cache_dir'];
}
if (isset($cfg['cache_time'])) {
$this->cache_time = $cfg['cache_time'];
}
if (isset($cfg['my_rep'])) {
$this->my_rep = $cfg['my_rep'];
}
if (isset($cfg['data'])) {
$this->data = $cfg['data'];
}
}
// 赋值
function assign($name, $value=NULL) {
if (is_array($name)) {
foreach ($name as $k => $v) {
$this->data[$k] = $v;
}
}else{
$this->data[$name] = &$value;
}
}
// 输出页面
function display($tpl_file) {
$_cache_path = $this->cache_path($tpl_file);
if (!$this->is_cached($_cache_path)) {
$this->compile($this->tpl_path($tpl_file), $_cache_path);
}
unset($tpl_file);
extract($this->data); // 如果data里有个_cache_path...
include $_cache_path;
}
// 返回页面
function fetch($tpl_file) {
ob_start();
ob_implicit_flush(0);
$this->display($tpl_file);
return ob_get_clean();
}
// 获取模板文件路径
private function tpl_path($tpl_file) {
return $this->tpl_dir . $tpl_file . $this->tpl_ext;
}
// 获取模板缓存路径
private function cache_path($tpl_file) {
return $this->cache_dir . $tpl_file . $this->tpl_ext;
}
// 模板缓存是否有效
private function is_cached($cache_path) {
if (!file_exists($cache_path)) {
return false;
}
if ($this->cache_time<0) {
return true;
}
$cache_time = filemtime($cache_path);
if ( time()-$cache_time > $this->cache_time ) {
return false;
}
return true;
}
// 编译模板
private function compile($tpl_path, $cache_path) {
$tpl = @file_get_contents($tpl_path);
if ($tpl===FALSE) {
die("模板文件“{$tpl_path}”不存在");
}
$tmp = array_merge($this->tmd_rep, $this->my_rep);
$cache = preg_replace(array_keys($tmp), $tmp, $tpl);
@mkdir(dirname($cache_path), 0777, true);
$tmp = @file_put_contents($cache_path, $cache, LOCK_EX);
if ($tmp===FALSE) {
die("编译后的模板文件“$cache_path”无法写入");
}
}
// 页面有include时用到
private function _include($inc_file, $cache_path) {
$inc_path = dirname($cache_path) . '/' . $inc_file;
if (!$this->is_cached($inc_path)) {
$tpl_path = str_replace(realpath($this->cache_dir), realpath($this->tpl_dir), $inc_path);
$this->compile($tpl_path, $inc_path);
}
return $inc_path;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
tmd_tpl国产PHP模板引擎 v1.0.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共9个文件
php:4个
jpg:3个
js:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 82 浏览量
2022-11-01
00:08:59
上传
评论
收藏 37KB ZIP 举报
温馨提示
tmd_tpl国产PHP模板引擎 v1.0.zip
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rplib](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 9 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/ace77722cc904668be9c7ee0feb247ba_dwf1354046363.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
易小侠
- 粉丝: 6509
- 资源: 9万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)