A simple template engine class, which is not perfect for research purposes only. I hope some friends can join in the study.

Source: Internet
Author: User
A simple template engine class, which is not perfect for research purposes only. I hope some friends can join in the study.
This is the first post code here. this code is mainly used for PHP template engine technology research. Currently, only the compiled version is available. I hope you will provide more comments and optimization skills.
It consists of three files. I don't know how to copy them as files. sorry!
Index. php is a configuration file. you can see it.
Index.html examples
Templates. class. php base class
A well-developed version with a cache will be released later, but I hope it will not be written to the cache. some friends or experts may advise that this template engine only needs to process compilation and cache, and other considerations will not be considered for the time being, of course, the regular expression replacement mode also requires f, w, and so on...
I hope a friend can study me. Q: 76376931

The Copy_3_of_Templates.class.php file has been added to the cache mode. if you refresh the page again, the cache will not be generated. if you do not want to cache some pages in the project, you will use this class to add them gradually. I hope you can communicate with me!

  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. // Whether to enable the buffer
  7. Define ('New _ cache', false );
  8. // Determine whether the buffer zone is enabled
  9. NEW_CACHE? Ob_start (): null;
  10. // Introduce the template class
  11. Require ROOT_HOST. '/lib/Templates. class. php ';
  12. $ _ Moban = new Templates ();
  13. $ Array = array (a => 'Hi! ', B =>' I am not very good, but I miss you very much ', c =>' You are all at home, why do you still miss me? ');
  14. $ Xcvu = 'Hello, this is an xcvu ';
  15. $ Zmq = "hi ";
  16. $ Title = "this is a template engine custom method! ";
  17. $ Ling = "because a" function "???????????????? ";
  18. $ _ Moban-> assign ('ling', $ ling );
  19. $ _ Moban-> assign ('title', $ title );
  20. $ _ Moban-> assign ('zrabbit', $ 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 doesn't know what to say, but wants to say something better.


  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. // Set Template variables
  26. $ This-> _ HtmlFile = HTML_DIR. $ _ File;
  27. // Set compilation
  28. $ This-> _ Compiled = COMPILED_DIR.md5 ($ _ File). $ _ File. '. php ';
  29. // Set cache
  30. $ This-> _ CaChe = cache_dir.md5(pai_file).pai_file.'.html ';
  31. // Determine whether the template exists
  32. If (! File_exists ($ this-> _ HtmlFile )){
  33. Exit ('Template file does not exist ');
  34. }
  35. // Value assignment and Read Judgment
  36. If (! $ This-> _ FileVar = file_get_contents ($ this-> _ HtmlFile )){
  37. Exit ('The template file read error! ');
  38. }
  39. // If edit Compiled File date <date HtmlFile
  40. If (! File_exists ($ this-> _ Compiled) | filemtime ($ this-> _ Compiled) <filemtime ($ this-> _ 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. '/<\! -- \ S + \ $ ([\ w] +) \ s + \ --> /',
  58. '/<\! -- \ S + if \ s + \ $ ([\ w] +) \ s + \ --> /',
  59. '/<\! -- \ S + \/if \ s + \ --> /',
  60. '/<\! -- \ S + else \ s + \ --> /',
  61. '/<\! -- \ S + loop \ s + \ $ ([\ w] +) \ ([\ w] +), ([\ w] + )\) \ s + \ --> /',
  62. '/<\! -- \ S + \/loop \ s + \ --> /',
  63. '/<\! -- \ S + @ ([\ w] +) \ s + \ --> /',
  64. '/<\! -- \ S + \ # (. *) \ s + \ --> /');
  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 = '/<\! -- \ S + include \ s + file = \ "([\ w \. \-] +) \" \ s + \ --> /';
  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. // Set Template variables
  26. $ This-> _ HtmlFile = HTML_DIR. $ _ File;
  27. // Set compilation
  28. $ This-> _ Compiled = COMPILED_DIR.md5 ($ _ File). $ _ File. '. php ';
  29. // Set cache
  30. $ This-> _ CaChe = cache_dir.md5(pai_file).pai_file.'.html ';
  31. // Determine whether the template exists
  32. If (! File_exists ($ this-> _ HtmlFile )){
  33. Exit ('Template file does not exist ');
  34. }
  35. // Value assignment and Read Judgment
  36. If (! $ This-> _ FileVar = file_get_contents ($ this-> _ HtmlFile )){
  37. Exit ('The template file read error! ');
  38. }
  39. // If edit Compiled File date <date HtmlFile
  40. If (! File_exists ($ this-> _ Compiled) | filemtime ($ this-> _ Compiled) <filemtime ($ this-> _ 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) <filemtime ($ this-> _ 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. '/<\! -- \ S + \ $ ([\ w] +) \ s + \ --> /',
  69. '/<\! -- \ S + if \ s + \ $ ([\ w] +) \ s + \ --> /',
  70. '/<\! -- \ S + \/if \ s + \ --> /',
  71. '/<\! -- \ S + else \ s + \ --> /',
  72. '/<\! -- \ S + loop \ s + \ $ ([\ w] +) \ ([\ w] +), ([\ w] + )\) \ s + \ --> /',
  73. '/<\! -- \ S + \/loop \ s + \ --> /',
  74. '/<\! -- \ S + @ ([\ w] +) \ s + \ --> /',
  75. '/<\! -- \ S + \ # (. *) \ s + \ --> /');
  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 = '/<\! -- \ S + include \ s + file = \ "([\ w \. \-] +) \" \ s + \ --> /';
  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. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.