A simple template engine class, this class is only for research is not perfect, want to have friends to participate in learning research

Source: Internet
Author: User
Tags php foreach php template
The first time here, this code is mainly PHP template engine technology research, currently only compiled version, I hope you will provide advice and optimization skills
three file composition, do not know how to file form, can only copy, sorry!
index.php is a configuration file, folks, just look at it.
index.html Some examples of use
Templates.class.php base class
Late release has a perfect version of the cache, but I hope not in the write cache some, a friend or expert guidance, the template engine as long as the processing of compilation and caching, the rest of the consideration is not considered, of course, the regular replacement mode to increase the f,w and the like ...
Hope to have a friend can study my Q: 76376931

Copy_3_of_Templates.class.php file is already increased cache mode, refresh the page again does not generate the cache, regardless of whether some pages in the project to cache, later with the class in the gradual addition, I hope friends can communicate together!
  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 open buffer
  7. Define (' New_cache ', false);
  8. Determine if the buffer is turned on
  9. New_cache? Ob_start (): null;
  10. Introducing Template Classes
  11. Require Root_host. ' /lib/templates.class.php ';
  12. $_moban = new Templates ();
  13. $array = Array (a=> ' hello ',b=> ' I am not very good, but I miss you ',c=> ' you are at home, why still think of me? ');
  14. $XCVU = ' Hello, this is a XCVU ';
  15. $ZMQ = "HI";
  16. $title = "This is a template engine custom Method!";
  17. $ling = "because a function"???????????????? "is being modified;
  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. ?>
Copy Code
    1. <!--$title-->
    2. BBBASD I don't know what to say, but I want to say something.
    3. !--$ling-->

    4. !--$xcvu-->

    5. li>!--if $abc-->
    6. 1th

    7. !--Else-->
    8. 2nd

    9. !--/if-->

    10. !--loop $array (k,v)-->
    11. !--@k--> .... !--@v-->
    12. !--/loop-->
    13. !--#这个是PHP注释-->
Copy Code
  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 a variable for a template
  26. $this->_htmlfile = html_dir.$_file;
  27. Set compilation
  28. $this->_compiled = COMPILED_DIR.MD5 ($_file). $_file. PHP ';
  29. Setting up the cache
  30. $this->_cache = CACHE_DIR.MD5 ($_file). $_file. HTML ';
  31. Determine if a template exists
  32. if (!file_exists ($this->_htmlfile)) {
  33. Exit (' Template file does not exist ');
  34. }
  35. Assignment and judgment reading
  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[' "$"];? > ',
  67. ' _keyarr[' "]) {?> ',
  68. ' ',
  69. ' ',
  70. ' _keyarr[' ("$" ] 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 Include File error! ');
  85. }
  86. $this->_filevar = preg_replace ($_prefile, " ", $this->_filevar);
  87. }
  88. }
  89. }
  90. ?>
Copy Code
  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 a variable for a template
  26. $this->_htmlfile = html_dir.$_file;
  27. Set compilation
  28. $this->_compiled = COMPILED_DIR.MD5 ($_file). $_file. PHP ';
  29. Setting up the cache
  30. $this->_cache = CACHE_DIR.MD5 ($_file). $_file. HTML ';
  31. Determine if a template exists
  32. if (!file_exists ($this->_htmlfile)) {
  33. Exit (' Template file does not exist ');
  34. }
  35. Assignment and judgment reading
  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[' "$"];? > ',
  78. ' _keyarr[' "]) {?> ',
  79. ' ',
  80. ' ',
  81. ' _keyarr[' ("$" ] 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 Include File error! ');
  96. }
  97. $this->_filevar = preg_replace ($_prefile, " ", $this->_filevar);
  98. }
  99. }
  100. }
  101. ?>
Copy Code
  • 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.