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

WBOY
Freigeben: 2016-07-25 09:01:19
Original
936 Leute haben es durchsucht
第一次在这里帖码,此份代码主要是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. ?>
    复制代码


    Quelle:php.cn
    Erklärung dieser Website
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
    Beliebte Tutorials
    Mehr>
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage