0

0

一个简单的模板引擎类,此类仅作研究并不完善,希望有朋友一起参与学习研究

php中文网

php中文网

发布时间:2016-07-25 09:01:19

|

1105人浏览过

|

来源于php中文网

原创

第一次在这里帖码,此份代码主要是PHP模板引擎技术研究,目前只有编译版本,希望各位多多提供意见和优化技巧
三个文件组成,不知道如何以文件形式,只能复制了,抱歉!
index.php是一个配置文件,大伙看看就明白
index.html一些使用的例子
Templates.class.php基类
晚点发布下有缓存的完善版本,但希望没有在写缓存一些,有朋友或是高手指点下,这个模板引擎只要处理编译和缓存即可,其余考虑暂时不考虑,当然正则替换模式还要增加f,w之类。。。
希望有朋友可以研究研究本人Q:
76376931

Copy_3_of_Templates.class.php 文件是已经增加缓存方式的,再次刷新页面不会生成缓存,未考虑项目中某些页面是否要缓存,以后用该类在逐步添加,希望有朋友可以一起交流!
  1. header('Content-Type:text/html;charset=utf-8');
  2. define('ROOT_HOST',dirname(__FILE__));
  3. define('HTML_DIR',ROOT_HOST.'/moban/');
  4. define('COMPILED_DIR',ROOT_HOST.'/data/compiled/');
  5. define('CACHE_DIR',ROOT_HOST.'/data/cache/');
  6. //是否开启缓冲区
  7. define('NEW_CACHE', false);
  8. //判断是否开启缓冲区
  9. NEW_CACHE ? ob_start() : null;
  10. //引入模板类
  11. require ROOT_HOST.'/lib/Templates.class.php';
  12. $_moban = new Templates();
  13. $array = array(a=>'你好呀',b=>'我不是很好,但我很想你',c=>'你都在家里了,怎么还想我呀?');
  14. $xcvu = '你好啊,这是一个XCVU';
  15. $zmq = "hi";
  16. $title = "这是一个模板引擎自定义方法!";
  17. $ling = "因为正在修改一个“函数”????????????????";
  18. $_moban->assign('ling', $ling);
  19. $_moban->assign('title',$title);
  20. $_moban->assign('zmq', $zmq);
  21. $_moban->assign('xcvu', $xcvu);
  22. $_moban->assign('abc',5>4);
  23. $_moban->assign('array', $array);
  24. $_moban->display('index.html');
  25. ?>
复制代码
  1. <!-- $title -->
  2. *{ margin:0; padding:0;}
  3. body{ font-size:12px; color:#fff; background:#999;}
  4. .index { margin:0 auto; width:960px; background:#fff;height:1000px; line-height:50px; padding:20px;color:#000;}
  5. .index a{color:#000; text-decoration:none;}
  6. .index a:hover{ color:#F0F;}
  7. BBBasd不知道说点什么好,可又想说点什么好


  8. 1号

  9. 2号

    Replit Agent
    Replit Agent

    Replit最新推出的AI编程工具,可以帮助用户从零开始自动构建应用程序。

    下载

  10. ........
  • 复制代码
    1. /* about:Richard.z
    2. * site:http://www.zmq.cc
    3. * E_mail:code@zmq.cc
    4. * date:2013/01/02/17:30
    5. * */
    6. class Templates{
    7. private $_CaChe;
    8. private $_Compiled;
    9. private $_HtmlFile;
    10. private $_FileVar;
    11. private $_KeyArr = array();
    12. public function __construct(){
    13. if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){
    14. exit('Your directory does not exist!');
    15. }
    16. }
    17. public function assign($_var, $_value){
    18. if(isset($_var) && !empty($_var)){
    19. $this->_KeyArr[$_var] = $_value;
    20. }else{
    21. exit('Please set your value!');
    22. }
    23. }
    24. public function display($_File){
    25. //设置模板的变量
    26. $this->_HtmlFile = HTML_DIR.$_File;
    27. //设置编译
    28. $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
    29. //设置缓存
    30. $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
    31. //判断模板是否存在
    32. if(!file_exists($this->_HtmlFile)){
    33. exit('Template file does not exist');
    34. }
    35. //赋值和判断读取
    36. if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){
    37. exit('The template file read error!');
    38. }
    39. //if edit Compiled File date
    40. if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) _HtmlFile)){
    41. $this->Set_Comilled();
    42. }
    43. //Include Compiled
    44. include $this->_Compiled;
    45. }
    46. //public function
    47. public function Set_Comilled(){
    48. $this->SetArr();
    49. $this->SetInclude();
    50. if(!file_put_contents($this->_Compiled, $this->_FileVar)){
    51. exit('Compiled files generated error!');
    52. }
    53. }
    54. //arr
    55. private function SetArr(){
    56. $_preaa = array(
    57. '//',
    58. '//',
    59. '//',
    60. '//',
    61. '//',
    62. '//',
    63. '//',
    64. '//');
    65. $_prebb = array(
    66. '_KeyArr["$1"];?>',
    67. '_KeyArr["$1"]) {?>',
    68. '',
    69. '',
    70. '_KeyArr["$1"] as \$$2=>\$$3) { ?>',
    71. '',
    72. '',
    73. '');
    74. $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
    75. if(preg_match($_preaa[0], $this->_FileVar)){
    76. $this->_FileVar = $this->SetArr($this->_FileVar);
    77. }
    78. }
    79. //Include
    80. private function SetInclude(){
    81. $_preFile = '//';
    82. if(preg_match($_preFile, $this->_FileVar,$_File)){
    83. if(!file_exists($_File[1]) || empty($_File)){
    84. exit('You of Include File Error!');
    85. }
    86. $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
    87. }
    88. }
    89. }
    90. ?>
    复制代码
    1. /* about:Richard.z
    2. * site:http://www.zmq.cc
    3. * E_mail:code@zmq.cc
    4. * date:2013/01/02/17:30 || 2013/01/14/21:35
    5. * */
    6. class Templates{
    7. private $_CaChe;
    8. private $_Compiled;
    9. private $_HtmlFile;
    10. private $_FileVar;
    11. private $_KeyArr = array();
    12. public function __construct(){
    13. if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){
    14. exit('Your directory does not exist!');
    15. }
    16. }
    17. public function assign($_var, $_value){
    18. if(isset($_var) && !empty($_var)){
    19. $this->_KeyArr[$_var] = $_value;
    20. }else{
    21. exit('Please set your value!');
    22. }
    23. }
    24. public function display($_File){
    25. //设置模板的变量
    26. $this->_HtmlFile = HTML_DIR.$_File;
    27. //设置编译
    28. $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
    29. //设置缓存
    30. $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
    31. //判断模板是否存在
    32. if(!file_exists($this->_HtmlFile)){
    33. exit('Template file does not exist');
    34. }
    35. //赋值和判断读取
    36. if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){
    37. exit('The template file read error!');
    38. }
    39. //if edit Compiled File date
    40. if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) _HtmlFile)){
    41. $this->Set_Comilled();
    42. }
    43. //Include Compiled
    44. include $this->_Compiled;
    45. $this->SetCaChe();
    46. }
    47. //The setting cache file if you want to be generated again
    48. private function SetCaChe(){
    49. if(!file_exists($this->_CaChe) || filemtime($this->_CaChe) _Compiled)){
    50. if(NEW_CACHE){
    51. file_put_contents($this->_CaChe, ob_get_contents());
    52. ob_end_clean();
    53. include $this->_CaChe;
    54. }
    55. }
    56. }
    57. //public function
    58. public function Set_Comilled(){
    59. $this->SetArr();
    60. $this->SetInclude();
    61. if(!file_put_contents($this->_Compiled, $this->_FileVar)){
    62. exit('Compiled files generated error!');
    63. }
    64. }
    65. //arr
    66. private function SetArr(){
    67. $_preaa = array(
    68. '//',
    69. '//',
    70. '//',
    71. '//',
    72. '//',
    73. '//',
    74. '//',
    75. '//');
    76. $_prebb = array(
    77. '_KeyArr["$1"];?>',
    78. '_KeyArr["$1"]) {?>',
    79. '',
    80. '',
    81. '_KeyArr["$1"] as \$$2=>\$$3) { ?>',
    82. '',
    83. '',
    84. '');
    85. $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
    86. if(preg_match($_preaa[0], $this->_FileVar)){
    87. $this->_FileVar = $this->SetArr($this->_FileVar);
    88. }
    89. }
    90. //Include
    91. private function SetInclude(){
    92. $_preFile = '//';
    93. if(preg_match($_preFile, $this->_FileVar,$_File)){
    94. if(!file_exists($_File[1]) || empty($_File)){
    95. exit('You of Include File Error!');
    96. }
    97. $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
    98. }
    99. }
    100. }
    101. ?>
    复制代码


    热门AI工具

    更多
    DeepSeek
    DeepSeek

    幻方量化公司旗下的开源大模型平台

    豆包大模型
    豆包大模型

    字节跳动自主研发的一系列大型语言模型

    通义千问
    通义千问

    阿里巴巴推出的全能AI助手

    腾讯元宝
    腾讯元宝

    腾讯混元平台推出的AI助手

    文心一言
    文心一言

    文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

    讯飞写作
    讯飞写作

    基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

    即梦AI
    即梦AI

    一站式AI创作平台,免费AI图片和视频生成。

    ChatGPT
    ChatGPT

    最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

    相关专题

    更多
    Golang 实际项目案例:从需求到上线
    Golang 实际项目案例:从需求到上线

    《Golang 实际项目案例:从需求到上线》以真实业务场景为主线,完整覆盖需求分析、架构设计、模块拆分、编码实现、性能优化与部署上线全过程,强调工程规范与实践决策,帮助开发者打通从技术实现到系统交付的关键路径,提升独立完成 Go 项目的综合能力。

    2

    2026.02.26

    Golang Web 开发路线:构建高效后端服务
    Golang Web 开发路线:构建高效后端服务

    《Golang Web 开发路线:构建高效后端服务》围绕 Go 在后端领域的工程实践,系统讲解 Web 框架选型、路由设计、中间件机制、数据库访问与接口规范,结合高并发与可维护性思维,逐步构建稳定、高性能、易扩展的后端服务体系,帮助开发者形成完整的 Go Web 架构能力。

    3

    2026.02.26

    Golang 并发编程专题:掌握多核时代的核心技能
    Golang 并发编程专题:掌握多核时代的核心技能

    《Golang 并发编程专题:掌握多核时代的核心技能》系统讲解 Go 在并发领域的设计哲学与实践方法,深入剖析 goroutine、channel、调度模型与并发安全机制,结合真实场景与性能思维,帮助开发者构建高吞吐、低延迟、可扩展的并发程序,全面提升多核时代的工程能力。

    5

    2026.02.26

    batoto漫画官网入口与网页版访问指南
    batoto漫画官网入口与网页版访问指南

    本专题系统整理batoto漫画官方网站最新可用入口,涵盖最新官网地址、网页版登录页面及防走失访问方式说明,帮助用户快速找到batoto漫画官方平台,稳定在线阅读各类漫画内容。

    356

    2026.02.25

    Steam官网正版入口与注册登录指南_新手快速进入游戏平台方法
    Steam官网正版入口与注册登录指南_新手快速进入游戏平台方法

    本专题系统整理Steam官网最新可用入口,涵盖网页版登录地址、新用户注册流程、账号登录方法及官方游戏商店访问说明,帮助新手玩家快速进入Steam平台,完成注册登录并管理个人游戏库。

    78

    2026.02.25

    TypeScript全栈项目架构与接口规范设计
    TypeScript全栈项目架构与接口规范设计

    本专题面向全栈开发者,系统讲解基于 TypeScript 构建前后端统一技术栈的工程化实践。内容涵盖项目分层设计、接口协议规范、类型共享机制、错误码体系设计、接口自动化生成与文档维护方案。通过完整项目示例,帮助开发者构建结构清晰、类型安全、易维护的现代全栈应用架构。

    35

    2026.02.25

    Python数据处理流水线与ETL工程实战
    Python数据处理流水线与ETL工程实战

    本专题聚焦 Python 在数据工程场景下的实际应用,系统讲解 ETL 流程设计、数据抽取与清洗、批处理与增量处理方案,以及数据质量校验与异常处理机制。通过构建完整的数据处理流水线案例,帮助开发者掌握数据工程中的性能优化思路与工程化规范,为后续数据分析与机器学习提供稳定可靠的数据基础。

    14

    2026.02.25

    Java领域驱动设计(DDD)与复杂业务建模实战
    Java领域驱动设计(DDD)与复杂业务建模实战

    本专题围绕 Java 在复杂业务系统中的建模与架构设计展开,深入讲解领域驱动设计(DDD)的核心思想与落地实践。内容涵盖领域划分、聚合根设计、限界上下文、领域事件、贫血模型与充血模型对比,并结合实际业务案例,讲解如何在 Spring 体系中实现可演进的领域模型架构,帮助开发者应对复杂业务带来的系统演化挑战。

    5

    2026.02.25

    Golang 生态工具与框架:扩展开发能力
    Golang 生态工具与框架:扩展开发能力

    《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。

    19

    2026.02.24

    热门下载

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

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    ThinkPHP6.x 微实战--十天技能课堂
    ThinkPHP6.x 微实战--十天技能课堂

    共26课时 | 1.8万人学习

    Symfony教程(入门篇+基础篇)
    Symfony教程(入门篇+基础篇)

    共18课时 | 1.3万人学习

    JavaScript基础精讲视频教程
    JavaScript基础精讲视频教程

    共36课时 | 8.2万人学习

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

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号