搜索
首页后端开发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号


  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. ?>
    复制代码


    声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    PHP和Python:解释了不同的范例PHP和Python:解释了不同的范例Apr 18, 2025 am 12:26 AM

    PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

    PHP和Python:深入了解他们的历史PHP和Python:深入了解他们的历史Apr 18, 2025 am 12:25 AM

    PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

    在PHP和Python之间进行选择:指南在PHP和Python之间进行选择:指南Apr 18, 2025 am 12:24 AM

    PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

    PHP和框架:现代化语言PHP和框架:现代化语言Apr 18, 2025 am 12:14 AM

    PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

    PHP的影响:网络开发及以后PHP的影响:网络开发及以后Apr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

    PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

    PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

    PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

    PHP与Python:用例和应用程序PHP与Python:用例和应用程序Apr 17, 2025 am 12:23 AM

    PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

    See all articles

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热门文章

    R.E.P.O.能量晶体解释及其做什么(黄色晶体)
    1 个月前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳图形设置
    1 个月前By尊渡假赌尊渡假赌尊渡假赌
    威尔R.E.P.O.有交叉游戏吗?
    1 个月前By尊渡假赌尊渡假赌尊渡假赌

    热工具

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

    PhpStorm Mac 版本

    PhpStorm Mac 版本

    最新(2018.2.1 )专业的PHP集成开发工具

    SublimeText3 英文版

    SublimeText3 英文版

    推荐:为Win版本,支持代码提示!

    SecLists

    SecLists

    SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    功能强大的PHP集成开发环境