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. BBBasd不知道说点什么好,可又想说点什么好


  3. 1号

  4. 2号

    讯飞听见会议
    讯飞听见会议

    科大讯飞推出的AI智能会议系统

    下载

  5. ........
  • 复制代码
    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 if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) _HtmlFile)){
    40. $this->Set_Comilled();
    41. }
    42. //Include Compiled
    43. include $this->_Compiled;
    44. }
    45. //public function
    46. public function Set_Comilled(){
    47. $this->SetArr();
    48. $this->SetInclude();
    49. if(!file_put_contents($this->_Compiled, $this->_FileVar)){
    50. exit('Compiled files generated error!');
    51. }
    52. }
    53. //arr
    54. private function SetArr(){
    55. $_preaa = array(
    56. '//',
    57. '//',
    58. '//',
    59. '//',
    60. '//',
    61. '//',
    62. '//',
    63. '//');
    64. $_prebb = array(
    65. '_KeyArr["$1"];?>',
    66. '_KeyArr["$1"]) {?>',
    67. '',
    68. '',
    69. '_KeyArr["$1"] as \$$2=>\$$3) { ?>',
    70. '',
    71. '',
    72. '');
    73. $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
    74. if(preg_match($_preaa[0], $this->_FileVar)){
    75. $this->_FileVar = $this->SetArr($this->_FileVar);
    76. }
    77. }
    78. //Include
    79. private function SetInclude(){
    80. $_preFile = '//';
    81. if(preg_match($_preFile, $this->_FileVar,$_File)){
    82. if(!file_exists($_File[1]) || empty($_File)){
    83. exit('You of Include File Error!');
    84. }
    85. $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
    86. }
    87. }
    88. }
    89. ?>
    复制代码
    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 if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) _HtmlFile)){
    40. $this->Set_Comilled();
    41. }
    42. //Include Compiled
    43. include $this->_Compiled;
    44. $this->SetCaChe();
    45. }
    46. //The setting cache file if you want to be generated again
    47. private function SetCaChe(){
    48. if(!file_exists($this->_CaChe) || filemtime($this->_CaChe) _Compiled)){
    49. if(NEW_CACHE){
    50. file_put_contents($this->_CaChe, ob_get_contents());
    51. ob_end_clean();
    52. include $this->_CaChe;
    53. }
    54. }
    55. }
    56. //public function
    57. public function Set_Comilled(){
    58. $this->SetArr();
    59. $this->SetInclude();
    60. if(!file_put_contents($this->_Compiled, $this->_FileVar)){
    61. exit('Compiled files generated error!');
    62. }
    63. }
    64. //arr
    65. private function SetArr(){
    66. $_preaa = array(
    67. '//',
    68. '//',
    69. '//',
    70. '//',
    71. '//',
    72. '//',
    73. '//',
    74. '//');
    75. $_prebb = array(
    76. '_KeyArr["$1"];?>',
    77. '_KeyArr["$1"]) {?>',
    78. '',
    79. '',
    80. '_KeyArr["$1"] as \$$2=>\$$3) { ?>',
    81. '',
    82. '',
    83. '');
    84. $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
    85. if(preg_match($_preaa[0], $this->_FileVar)){
    86. $this->_FileVar = $this->SetArr($this->_FileVar);
    87. }
    88. }
    89. //Include
    90. private function SetInclude(){
    91. $_preFile = '//';
    92. if(preg_match($_preFile, $this->_FileVar,$_File)){
    93. if(!file_exists($_File[1]) || empty($_File)){
    94. exit('You of Include File Error!');
    95. }
    96. $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
    97. }
    98. }
    99. }
    100. ?>
    复制代码


    相关专题

    更多
    excel制作动态图表教程
    excel制作动态图表教程

    本专题整合了excel制作动态图表相关教程,阅读专题下面的文章了解更多详细教程。

    20

    2025.12.29

    freeok看剧入口合集
    freeok看剧入口合集

    本专题整合了freeok看剧入口网址,阅读下面的文章了解更多网址。

    65

    2025.12.29

    俄罗斯搜索引擎Yandex最新官方入口网址
    俄罗斯搜索引擎Yandex最新官方入口网址

    Yandex官方入口网址是https://yandex.com;用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    197

    2025.12.29

    python中def的用法大全
    python中def的用法大全

    def关键字用于在Python中定义函数。其基本语法包括函数名、参数列表、文档字符串和返回值。使用def可以定义无参数、单参数、多参数、默认参数和可变参数的函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    16

    2025.12.29

    python改成中文版教程大全
    python改成中文版教程大全

    Python界面可通过以下方法改为中文版:修改系统语言环境:更改系统语言为“中文(简体)”。使用 IDE 修改:在 PyCharm 等 IDE 中更改语言设置为“中文”。使用 IDLE 修改:在 IDLE 中修改语言为“Chinese”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    16

    2025.12.29

    C++的Top K问题怎么解决
    C++的Top K问题怎么解决

    TopK问题可通过优先队列、partial_sort和nth_element解决:优先队列维护大小为K的堆,适合流式数据;partial_sort对前K个元素排序,适用于需有序结果且K较小的场景;nth_element基于快速选择,平均时间复杂度O(n),效率最高但不保证前K内部有序。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    12

    2025.12.29

    php8.4实现接口限流的教程
    php8.4实现接口限流的教程

    PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    134

    2025.12.29

    抖音网页版入口在哪(最新版)
    抖音网页版入口在哪(最新版)

    抖音网页版可通过官网https://www.douyin.com进入,打开浏览器输入网址后,可选择扫码或账号登录,登录后同步移动端数据,未登录仅可浏览部分推荐内容。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    63

    2025.12.29

    快手直播回放在哪看教程
    快手直播回放在哪看教程

    快手直播回放需主播开启功能才可观看,主要通过三种路径查看:一是从“我”主页进入“关注”标签再进主播主页的“直播”分类;二是通过“历史记录”中的“直播”标签页找回;三是进入“个人信息查阅与下载”里的“直播回放”选项。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    18

    2025.12.29

    热门下载

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

    精品课程

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

    共26课时 | 1.6万人学习

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

    共18课时 | 1.2万人学习

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

    共36课时 | 7.9万人学习

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

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