讲师中心 微信公众号
AI工具推荐 视频效率加速

php模板简单写法

夏辰小哥_1351

夏辰小哥_1351

发布时间:2016-06-08 17:29:59

|

2015人浏览过

|

来源于php中文网

原创

这是一个简单的用php 实现的模板教程物。

<script>ec(2);</script>

class  TEMPLATE
   {
                  
              
                      private  $path = "." ;          #根目录        
                             
                   private $var;
                  
                   private $tplDir = "template"; #模板存储目录
                  
                   private $tplExt = "tpl";      #模板文件的后缀名
                  
                   private $tplId = 0 ;   #模板的ID号
                  
                   private $compileDir = "template_c";  #编译后的php文件存放目录
                  
                   private $isCache=false ; #是否用缓存 (默认不启动)
                  
                   private $cacheId = 1; #缓存文件ID号
                  
                   private $cacheLeftTime=3600; #缓存有效期 (默认保存3600秒)
                  
                   private $cacheDir = "cache"; #缓存文件存储目录
                  
                   private $autoRefresh = false ; #是否自动刷新
                  
                   private $pattern = array(
                  
                                           "/({dw:)s*includes*filename=s*"(.+..+)s*"s*(/})/i",#包含文件
                                           "/({dw:)s*field.(.+)s*(/})/i",#局部变量
                                           "/({dw:)s*global.(.+)s*(/})/i",#全局变量
                                           "/({dw:)s*foreachs*(.+)s*ass*(.+)s*(/})/i",#foreach 语句
                                           "/({dw:)s*ends*foreachs*(/})/i",           #foreach 结束
                                           "/({dw:)s*ifs*((.+))(/})/i",
                                           "/({dw:)s*elseifs*((.+))(/})/i",
                                           "/({dw:)s*elses*(/})/i",
                                           "/({dw:)s*ends*ifs*(/})/i",
                                                                
                                );
                  
                   private $replacement = array(
                  
                                           'inc_file("\2"); ?>',
                                           "",
                                           "",
                                           "",
                                           "",
                                           "" ,
                                           "",
                                           "",
                                           "",
                                          
                                          
                                          
                                          
                                                                                                  
                                                                   );
                  
                  
                   #构造函数
                  
                   function __construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="")
                   {
                           $this->path = $path ? $path : $this->path ;
                          
                           $this->tplDir = $tplDir ? $tplDir : $this->tplDir ;
                          
                           $this->compileDir = $compileDir ? $compileDir : $this->compileDir ;
                          
                           $this->isCache = is_bool($isCache) ? $isCache : $this->isCache ;
                          
                           $this->cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime ;
                          
                           $this->cacheDir = $cacheDir ? $cacheDir : $this->cacheDir ;
                          
                           $this->autoRefresh = is_bool($autoRefresh) ? $autoRefresh : $this->autoRefresh ;
                   }
                  
                  
                   #兼容php4
                  
                   function TEMPLATE($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="")
                   {
                           $this->__construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="");
                   }
                   function  __get($property)
                   {
                           return $this->$property ;
                   }
                  
                  
                   function __set($property,$value)
                   {
                           return $this->$property = $value ;
                   }
                  
                  
                  
        #给模板中的变量赋值
        # $tplVal 模板中的变量名                  
                  
                   function assign($tplVal ,$value="")
                   {
                           if (is_array($tplVal))
                           {
                                   foreach ($tplVal as $key => $val)
                                   {
                                           if (!empty($key))
                                          
                                           $this->var[$key] = $val ;
                                   }
                           }
                           else {
                                   if (!empty($tplVal))
                                  
                                   $this->var[$tplVal] = $value ;
                           }
                   }
                  
                   #输出文件内容函数
                  
                   function display($tplFile,$tplId=0,$cacheId = 1,$cacheLeftTime="")
                   {
                           if (empty($tplFile)) die("Template "{$tplFile}" not exist !");
                          
                           $this->cacheId = $cacheId ?  md5($cacheId) : md5($this->cacheId);
                          
                           $cacheFile = $this->path. "/".$this->cacheDir."/".$tplFile.$this->cacheId ;
                          
                           if ($this->check_cache($cacheFile,$cacheLeftTime))  #当缓存文件存在且不过期时直接从缓存文件读取内容
                           {
                                   echo $this->read_file($cacheFile);
                           }else {
                          
                              $tpl = $this->path."/".$this->tplDir."/".$tplFile.".".$this->tplExt;
          
                              $tplContent = $this->read_file($tpl);   #读取模板文件的内容
          
                             $compileContent= $this->compile_file($tplContent); #对读取出来的文件进行编译
           
                             $this->tplId = $tplId ? $tplId : $this->tplId ;
           
                             $compileFile = $this->path."/".$this->compileDir."/".md5($this->tplId)."".$tplFile.".php";
           
                             $this->write_file($compileFile,$compileContent);#将编译后的内容写入相应的文件中;
                          
                           @extract($this->var);
                          
                        ob_start();
                          
                           include_once($compileFile);
                          
                           $content = ob_get_contents() ;
                          
                           ob_end_clean() ;
                          
                           if ($this->isCache){
                                                     
                            $this->write_file($cacheFile,$content) ;# 帮编译好的内容写入缓存文件
                           }
                          
                           echo $content ;
                          
                           }
                   }
                  
                  
/*                   function  trim_tag($content)
                   {
                           $content = str_replace($this->startTag,"",$content);
                          
                           $content = str_replace($this->endTag,"",$content);
                                  
                           //$content = trim($content);
                          
                           return $content ;
                   }*/
                  
                   # 编译文件函数
                  
                   function compile_file($content=null)
                   {
                          
                           $content = $content ? $content :die("Compile fail!") ;
                          
                           //$content = $this->trim_tag($content);
                          
                           $content = preg_replace($this->pattern,$this->replacement,$content);
                          
                           return $content;
                          
                   }
                  
                   #解析包含文件
                  
                   function inc_file($filename,$tplId="",$cacheId="",$cacheLeftTime="")
                   { 
                           $file = $this->path."/".$this->tplDir."/".$filename ;
                          
                           if (file_exists($file))
                           {
                                   $filename = str_replace(".".$this->tplExt,"",$filename);
                                  
                           return         $this->display($filename,$tplId,$cacheId,$cacheLeftTime);
                           }
                           else die("Template "{$filename}" not exist");
                   }
                          
                   #读取文件内容函数
                  
                   function  read_file($filename)
                   {
                           if (!file_exists($filename)) die("Read file fail") ;
                          
                           return file_get_contents($filename);
                   }
                  
                   #内容写入函数
                  
                   function write_file($filename,$content,$mode="wb")
                   {
                          
                           $filename = trim($filename);
                          
                           $content = $content ? stripslashes(trim($content)) : exit();
                          
                           if (!file_exists($filename))
                           {
                                   $array = explode("/",$filename);
                                  
                                   $count = count($array);
                                  
                                   $path = "";
                                  
                                   for ($i = 0 ; $i                                    {
                                           if(!file_exists($path .= $array[$i]."/"))
                                           {
                                                   mkdir($path,0777);
                                           }
                                   }
                           }
                           $handle = fopen($filename,$mode) ;
                          
                           fwrite($handle,$content);
                          
                           fclose($handle);
                          
                           return true;       
                   }
                  
                  
                  
                   # 清除缓存
                  
                   function clear_dir($dir="")
                   {
                          
                           $dir = $this->path."/".$dir;
                          
                           $handle = opendir($dir);
                          
                           if (file_exists($dir))
                           {
                                   while ($file = readdir($handle))
                                   {
                                           if ($file !="." && $file != "..")
                                  
                                           unlink($dir."/".$file);
                                   }
                          
                             closedir($handle);
                            
                             return true;
                           }
                          
                           else {
                                   return false;
                           }
                          
                          
                   }
                  
                   #清除所有缓存
                  
                   function clear_all_cache()
                   {
                           if ($this->clear_dir($this->cacheDir) && $this->clear_dir($this->compileDir))
                                  
          
                           return true;
                   }
                  
                  
                  
                   #检查缓存是否过期
                  
                   function check_cache($cacheFile,$cacheLeftTime="")
                   {
                          
                           $cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime;
                          
                           if (!file_exists($cacheFile)) return false ;
                          
                          $time = $this->get_time($cacheFile) + $cacheLeftTime ;
                          
                           if ($time                            {
                                   unlink($cacheFile);
                                  
                                  
                                  
                                   return false;
                           }
                          
                           return true;
                          
                   }
                  
                  
                  
                   # 获取文件最后编辑时间
                  
                   function get_time($filename)
                   {
                           if (!file_exists($filename)) return false;
                          
                           return filemtime($filename);
                   }
                  
                  
                  
   }
                  

 

  

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
VibeKnow
VibeKnow Hot

一款AI视频创作工具,主要用于全球首个AI知识视频创作平台,文档、文章、网页,一键生成视频,适合需要提升相关任务效率的用户。

讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

超级简历WonderCV

一款AI办公效率工具,主要用于免费求职简历模版下载制作,应届生职场人必备简历制作神器,适合需要提升相关任务效率的用户。

Atoms
Atoms Hot

Atoms是一款AI智能体工具,第一支自动构建真实业务的 AI 团队。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

讯飞绘文

讯飞绘文是一款由科大讯飞推出的一站式 AIGC 内容运营平台。

UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

相关专题

更多
LLVM自定义Pass怎么写
LLVM自定义Pass怎么写

本专题聚焦LLVM自定义Pass开发,整理Pass类结构、run()方法、PreservedAnalyses、CMake构建、插件注册、-load-pass-plugin加载和测试用例编写流程。

0

2026.09.30

LLVM RISC-V参数配置教程
LLVM RISC-V参数配置教程

本专题介绍LLVM对RISC-V基础ISA和扩展的支持方式,涵盖RV32、RV64、标准扩展、实验性扩展、厂商扩展、-menable-experimental-extensions和版本差异。

0

2026.09.30

LLVM IR中间表示入门指南
LLVM IR中间表示入门指南

本专题整理LLVM IR的核心概念,包括中间表示作用、模块结构、函数、基本块、SSA形式、类型系统和常见语法,帮助新手理解LLVM编译流程中的关键层。

0

2026.09.30

PDF转图片方法
PDF转图片方法

需要把 PDF 页面用于上传、预览、分享或图片归档时,PDF 转图片方法专题整理 JPG/PNG 格式选择、逐页导出、清晰度设置、批量下载和结果检查等流程,帮助用户稳定完成 PDF 图片化处理。

0

2026.09.30

PixTV AI视频生成与无限画布创作
PixTV AI视频生成与无限画布创作

PixTV专题整理AI视频与视觉内容创作相关功能使用教程,涵盖AI生图、视频生成、无限画布、多模型创作、素材管理、声音音乐及视频剪辑等功能,帮助用户快速掌握PixTV从创意到成片的完整制作方法。

0

2026.09.29

Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

200

2026.09.23

Buffalo框架路由与请求处理实操指南
Buffalo框架路由与请求处理实操指南

本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。

120

2026.09.23

Buffalo框架零基础入门教程
Buffalo框架零基础入门教程

本专题整理Buffalo框架入门内容,涵盖Go环境准备、buffalo CLI安装、新项目生成、目录结构说明、dev热加载启动、数据库连接配置与常见报错排查,帮助新手按约定优于配置的思路跑通第一个Buffalo框架应用。

100

2026.09.23

Conan创建软件包配方指南
Conan创建软件包配方指南

本专题介绍通过conanfile.py创建软件包的方法,讲解包名、版本、依赖和构建设置等基础信息,以及source、build、package、package_info等常用方法的作用及编写思路。

60

2026.09.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 43.6万人学习

Pandas 教程
Pandas 教程

共15课时 | 1.9万人学习

C# 教程
C# 教程

共94课时 | 21.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn