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');
tpl模板引擎和使用相关推荐
- php tpl 模板 用法,php tpl模板引擎定义与使用示例
本文实例讲述了php tpl模板引擎定义与使用.分享给大家供大家参考,具体如下: tpl.php namespace tpl; /** * Class Tpl */ class Tpl { prote ...
- php tpl模板文件,php自己创建TPL模板引擎之初学习
一,创建初始化模板所需的文件和文件夹. 1,index.php主文件,用于编写业务逻辑. 2,template.inc.php模板初始化文件,用于初始化模板信息. 3,templates目录存放所有的 ...
- 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...
- php smarty 原理,php模板原理PHP模板引擎smarty模板原理浅谈
mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...
- php模板引擎如何实现,php模板引擎技术简单实现
用了smarty,tp过后,也想了解了解其模板技术是怎么实现,于是写一个简单的模板类,大致就是读取模板文件->替换模板文件的内容->保存或者静态化 tpl.class.php主要解析 as ...
- 浅谈Web中前后端模板引擎的使用
前言 这篇文章本来不打算写的,实话说楼主对前端模板的认识还处在非常初级的阶段,但是为了整个 源码解读系列 的完整性,在深入 Underscore _.template 方法源码后,觉得还是有必要记下此 ...
- Web模板引擎—Mustache
Web模板引擎--Mustache 2012年9月12日 BY BELL·12 COMMENTS Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 ...
- php yii 模板引擎,模板引擎(Template Engines)
使用模板引擎(Using template engines) 默认情况下,Yii 使用 PHP 作为其默认的模板引擎语言,但是,你可以配置 Yii 以扩展的方式支持其他的渲染引擎, 比如 Twig 或 ...
- 一个简单的PHP模板引擎
PHP早期开发中通常是PHP代码和HTML代码混写,这也使代码中充斥着数据库操作,逻辑处理等.当项目不大时,这样的代码还可以接受,但是随着项目不断扩大,我们就会发现同一个文件中同时存在前端逻辑和后端处 ...
最新文章
- 多实例gpu_MIG技术快速提高AI生产率
- 菜鸟学Linux 第050篇笔记 dhcp
- clistctrl控件最后插入在后面_用图表控件做一个简单的员工信息查询系统
- ECCV2018目标检测(object detection)算法总览
- c dup 函数
- C++编程模拟生产者消费者模型
- “天猫精灵”如何为你写诗?人机共创的秘密在这儿
- 深入理解Linux异步I/O框架 io_uring
- 《心欢喜,灵快乐》出版
- 基于Fragment的百度地图框架的使用
- 修改Launcher2欢迎页面字符重叠
- 【渝粤教育】电大中专计算机应用基础作业 题库
- 数据结构 http://www.cnblogs.com/sun-haiyu/p/7704654.html
- 获取本地视频url的方法
- win2003 序列号 windows2003 sp2可用序列号大全(准版与企业版)
- ubuntu浏览器突然使用不了搜狗拼音法
- Mac Mounty正常卸载方法(mount failed异常解决)
- 小白也能看懂的零知识证明与zk-SNARKs
- uni-app获取元素节点信息
- openwrt安装ipk报错“incompatible with the architectures configured”
热门文章
- Linux基础命令---ping
- WAS启动控制台,但不自动启动应用(详细配置两种方法)
- 城市环境类毕业论文文献有哪些?
- linux计划任务日志路径,计划任务日志文件
- 关于ActionForward
- LTE RRC 定时器汇总
- 2 RRC 三种状态
- 哪些服务器支持avx指令,至强E5的AVX指令集功能
- 国标流媒体-H265摄像头如何实现网页直播
- 成功解决CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anacond