Experience sharing of template engine development for PHP MVC pattern

Source: Internet
Author: User
Tags php foreach vars
  1. /*
  2. * Class: Template parsing Class
  3. * AUTHOR:51JS.COM-ZMM
  4. * date:2011.3.1
  5. * email:304924248@qq.com
  6. * blog:http://www.cnblogs.com/cnzmm/
  7. * link:http://bbs.it-home.org
  8. */
  9. Class Template {
  10. Public $html, $vars, $bTag, $eTag;
  11. Public $bFlag = ' {', $eFlag = '} ', $pfix = ' zmm: ';
  12. Private $folder, $file;
  13. function __construct ($vars =array ()) {
  14. !empty ($vars) && $this->vars = $vars;
  15. !empty ($GLOBALS [' Cfg_tag_prefix ']) &&
  16. $this->pfix = $GLOBALS [' Cfg_tag_prefix ']. ': ';
  17. $this->btag = $this->bflag. $this->pfix;
  18. $this->etag = $this->bflag. ' \ '. $this->pfix;
  19. Empty (Tags:: $vars) && Tags:: $vars = & $this->vars;
  20. }
  21. Public Function Loadtpl ($TPL) {
  22. $this->file = $this->gettplpath ($TPL);
  23. Tags:: $file = & $this->file;
  24. if (Is_file ($this->file)) {
  25. if ($this->gettplhtml ()) {
  26. $this->settpltags ();
  27. } else {
  28. Exit (' template file failed to load! ');
  29. }
  30. } else {
  31. Exit (' template file ['. $this->file. '] does not exist! ');
  32. }
  33. }
  34. Private Function Gettplpath ($TPL) {
  35. $this->folder = Website_dirroot.
  36. $GLOBALS [' Cfg_tpl_root '];
  37. return $this->folder. ' /'. $tpl;
  38. }
  39. Private Function gettplhtml () {
  40. $html = self::fmttplhtml (file_get_contents ($this->file));
  41. if (!empty ($html)) {
  42. $callFunc = Tags:: $prefix. ' Syntax ';
  43. $this->html = Tags:: $callFunc ($html, New Template ());
  44. } else {
  45. Exit (' template file content is empty! ');
  46. } return true;
  47. }
  48. static public Function fmttplhtml ($html) {
  49. return Preg_replace ('/(\ r) | ( \ n) | (\ t) | (\s{2,})/is ', ', $html);
  50. }
  51. Public Function Register ($vars =array ()) {
  52. if (Is_array ($vars)) {
  53. $this->vars = $vars;
  54. Tags:: $vars = & $this->vars;
  55. }
  56. }
  57. Public Function Display ($bool =false, $name = "", $time =0) {
  58. if (!empty ($this->html)) {
  59. if ($bool &&!empty ($name)) {
  60. if (!is_int ($time)) $time = 600;
  61. $cache = new cache ($time);
  62. $cache->set ($name, $this->html);
  63. }
  64. Echo $this->html; Flush ();
  65. } else {
  66. Exit (' template file content is empty! ');
  67. }
  68. }
  69. Public Function Setassign ($souc, $info) {
  70. if (!empty ($this->html)) {
  71. $this->html = Str_ireplace ($souc, self::fmttplhtml ($info), $this->html);
  72. } else {
  73. Exit (' template file content is empty! ');
  74. }
  75. }
  76. Private Function Settpltags () {
  77. $this->setpaneltags (); $this->settrunktags (); $this->reghatchvars ();
  78. }
  79. Private Function Setpaneltags () {
  80. $rule = $this->btag. ' ([^ '. $this->eflag. '] +) \/'. $this->eflag;
  81. Preg_match_all ('/'. $rule. ' /ism ', $this->html, $out _matches);
  82. $this->transtag ($out _matches, ' Panel '); unset ($out _matches);
  83. }
  84. Private Function Settrunktags () {
  85. $rule = $this->btag. ' (\w+) \s* ([^ '. $this->eflag.] *?)'. $this->eflag.
  86. '((?:(?!'. $this->btag. ') [\s\s]*?| (? R)) '. $this->etag. ' \\1\s* '. $this->eflag;
  87. Preg_match_all ('/'. $rule. ' /ism ', $this->html, $out _matches);
  88. $this->transtag ($out _matches, ' trunk '); unset ($out _matches);
  89. }
  90. Private Function Transtag ($result, $type) {
  91. if (!empty ($result [0])) {
  92. Switch ($type) {
  93. Case ' Panel ': {
  94. for ($i = 0; $i < count ($result [0]); $i + +) {
  95. $strTag = Explode ("', $result [1][$i], 2);
  96. if (Strpos ($strTag [0], '. ')) {
  97. $itemArg = Explode ('. ', $result [1][$i], 2);
  98. $callFunc = Tags:: $prefix. Ucfirst ($itemArg [0]);
  99. if (method_exists (' Tags ', $callFunc)) {
  100. $html = Tags:: $callFunc (Chop ($ITEMARG [1]);
  101. if ($html!== false) {
  102. $this->html = Str_ireplace ($result [0][$i], $html, $this->html);
  103. }
  104. }
  105. } else {
  106. $rule = ' ^ ([^\s]+) \s* ([\s\s]+) $ ';
  107. Preg_match_all ('/'. $rule. ' /is ', Trim ($result [1][$i]), $tmp _matches);
  108. $callFunc = Tags:: $prefix. Ucfirst ($tmp _matches[1][0]);
  109. if (method_exists (' Tags ', $callFunc)) {
  110. $html = Tags:: $callFunc ($tmp _matches[2][0]);
  111. if ($html!== false) {
  112. $this->html = Str_ireplace ($result [0][$i], $html, $this->html);
  113. }
  114. } unset ($tmp _matches);
  115. }
  116. } break;
  117. }
  118. Case ' trunk ': {
  119. for ($i = 0; $i < count ($result [0]); $i + +) {
  120. $callFunc = Tags:: $prefix. Ucfirst ($result [1][$i]);
  121. if (method_exists (' Tags ', $callFunc)) {
  122. $html = Tags:: $callFunc ($result [2][$i], $result [3][$i]);
  123. $this->html = Str_ireplace ($result [0][$i], $html, $this->html);
  124. }
  125. } break;
  126. }
  127. Default:break;
  128. }
  129. } else {
  130. return false;
  131. }
  132. }
  133. Private Function Reghatchvars () {
  134. $this->setpaneltags ();
  135. }
  136. function __destruct () {}
  137. }
  138. ?>
Copy Code

Second, the label resolution class : (temporarily provide data, list two kinds of labels analytic thinking)

  1. /*
  2. * Class: Tag parsing class
  3. * AUTHOR:51JS.COM-ZMM
  4. * date:2011.3.2
  5. * email:304924248@qq.com
  6. * blog:http://www.cnblogs.com/cnzmm/
  7. * link:http://bbs.it-home.org
  8. */
  9. Class Tags {
  10. static private $attrs =null;
  11. Static public $file, $vars, $rule, $prefix = ' tag_ ';
  12. static public Function Tag_syntax ($html, $that) {
  13. $rule = $that->btag. ' If\s+ ([^ '. $that->eflag. ') +) \s* '. $that->eflag;
  14. $html = preg_replace ('/'. $rule. ' /ism ', ' , $html);
  15. $rule = $that->btag. ' Elseif\s+ ([^ '. $that->eflag. ') +) \s* '. $that->eflag;
  16. $html = preg_replace ('/'. $rule. ' /ism ', ' , $html);
  17. $rule = $that->btag. ' else\s* '. $that->eflag;
  18. $html = preg_replace ('/'. $rule. ' /ism ', ' , $html);
  19. $rule = $that->btag. ' loop\s+ (\s+) \s+ (\s+) \s* '. $that->eflag;
  20. $html = preg_replace ('/'. $rule. ' /ism ', ' , $html);
  21. $rule = $that->btag. ' loop\s+ (\s+) \s+ (\s+) \s+ (\s+) \s* '. $that->eflag;
  22. $html = preg_replace ('/'. $rule. ' /ism ', ' \\3 ') {?> ', $html);
  23. $rule = $that->etag. ' (if|loop) \s* '. $that->eflag;
  24. $html = preg_replace ('/'. $rule. ' /ism ', ' , $html);
  25. $rule = $that->btag. ' php\s* '. $that->eflag. ' ((?:(?!'.
  26. $that->btag. ') [\s\s]*?| (? R)) '. $that->etag. ' php\s* '. $that->eflag;
  27. $html = preg_replace ('/'. $rule. ' /ism ', ' , $html);
  28. Return Self::tag_execute ($html);
  29. }
  30. static public Function tag_list ($attr, $html) {
  31. if (!empty ($html)) {
  32. if (Self::tag_havetag ($html)) {
  33. Return Self::tag_dealtag ($attr, $html, true);
  34. } else {
  35. Return Self::tag_getdata ($attr, $html, true);
  36. }
  37. } else {
  38. Exit (' tag {list} ' is empty! ');
  39. }
  40. }
  41. static public Function Tag_data ($attr, $html) {
  42. if (!empty ($html)) {
  43. if (Self::tag_havetag ($html)) {
  44. Return Self::tag_dealtag ($attr, $html, false);
  45. } else {
  46. Return Self::tag_getdata ($attr, $html, false);
  47. }
  48. } else {
  49. Exit (' tag {data} ' is empty! ');
  50. }
  51. }
  52. static public Function Tag_execute ($html) {
  53. Ob_clean (); Ob_start ();
  54. if (!empty (self:: $vars)) {
  55. Is_array (self:: $vars) &&
  56. Extract (self:: $vars, Extr_overwrite);
  57. }
  58. $file _inc = website_dirinc. ' /buffer/'.
  59. MD5 (Uniqid (rand (), true)). PHP ';
  60. if ($fp = fopen ($file _inc, ' XB ')) {
  61. Fwrite ($fp, $html);
  62. if (fclose ($fp)) {
  63. Include ($file _inc);
  64. $html = Ob_get_contents ();
  65. } unset ($FP);
  66. } else {
  67. Exit (' Template parsing file generation failed! ');
  68. } Ob_end_clean (); @unlink ($file _inc);
  69. return $html;
  70. }
  71. Static Private Function Tag_havetag ($html) {
  72. $bool _has = false;
  73. $tpl _ins = new Template ();
  74. Self:: $rule = $tpl _ins->btag. ' ([^ '. $tpl _ins->eflag. '] +) \/'. $TPL _ins->eflag;
  75. $bool _has = $bool _has | | Preg_match ('/'. Self:: $rule. ' /ism ', $html);
  76. Self:: $rule = $tpl _ins->btag. ' (\w+) \s* ([^ '. $tpl _ins->eflag.] *?)'. $tpl _ins->eflag.
  77. '((?:(?!'. $tpl _ins->btag. ') [\s\s]*?| (? R)) '. $tpl _ins->etag. ' \\1\s* '. $tpl _ins->eflag;
  78. $bool _has = $bool _has | | Preg_match ('/'. Self:: $rule. ' /ism ', $html);
  79. unset ($tpl _ins);
  80. return $bool _has;
  81. }
  82. Static Private Function Tag_dealtag ($attr, $html, $list) {
  83. Preg_match_all ('/'. Self:: $rule. ' /ism ', $html, $out _matches);
  84. if (!empty ($out _matches[0])) {
  85. $child _node = Array ();
  86. for ($i = 0; $i < count ($out _matches[0]); $i + +) {
  87. $child _node[] = $out _matches[3][$i];
  88. $html = Str_ireplace ($out _matches[3][$i], ' {-->>child_node_ '. $i. ' <<--} ', $html);
  89. }
  90. $html = Self::tag_getdata ($attr, $html, $list);
  91. for ($i = 0; $i < count ($out _matches[0]); $i + +) {
  92. $html = Str_ireplace (' {-->>child_node_ '. $i. ' <<--} ', $child _node[$i], $html);
  93. }
  94. Preg_match_all ('/'. Self:: $rule. ' /ism ', $html, $tmp _matches);
  95. if (!empty ($tmp _matches[0])) {
  96. for ($i = 0; $i < count ($tmp _matches[0]); $i + +) {
  97. $callFunc = self:: $prefix. Ucfirst ($tmp _matches[1][$i]);
  98. if (method_exists (' Tags ', $callFunc)) {
  99. $temp = self:: $callFunc ($tmp _matches[2][$i], $tmp _matches[3][$i]);
  100. $html = Str_ireplace ($tmp _matches[0][$i], $temp, $html);
  101. }
  102. }
  103. }
  104. unset ($tmp _matches);
  105. }
  106. unset ($out _matches); return $html;
  107. }
  108. Static Private Function Tag_getdata ($attr, $html, $list =false) {
  109. if (!empty ($attr)) {
  110. $attr _ins = new ATTBT ($attr);
  111. $attr _arr = $attr _ins->attrs;
  112. if (Is_array ($attr _arr)) {
  113. Extract ($attr _arr, extr_overwrite);
  114. $source = table_name ($source, $column);
  115. $rule = ' \[field:\s* (\w+) \s* ([^\]]*?) \s*\/?] ';
  116. Preg_match_all ('/'. $rule. ' /is ', $html, $out _matches);
  117. $data _str = ";
  118. $data _ins = new Datasql ();
  119. $attr _where = $attr _order = ";
  120. if (!empty ($where)) {
  121. $where = Str_replace (', ', ' and ', $where);
  122. $attr _where = ' where '. $where;
  123. }
  124. if (!empty ($order)) {
  125. $attr _order = ' ORDER by '. $order;
  126. } else {
  127. $fed _name = ";
  128. $fed _ins = $data _ins->getfedneedle ($source);
  129. $fed _cnt = $data _ins->getfedcount ($fed _ins);
  130. for ($i = 0; $i < $fed _cnt; $i + +) {
  131. $fed _flag = $data _ins->getfedflag ($fed _ins, $i);
  132. if (Preg_match ('/auto_increment/ism ', $fed _flag)) {
  133. $fed _name = $data _ins->getfedname ($fed _ins, $i);
  134. Break
  135. }
  136. }
  137. if (!empty ($fed _name))
  138. $attr _order = ' ORDER by '. $fed _name. ' Desc ';
  139. }
  140. if ($list = = True) {
  141. if (Empty ($source) && empty ($sql)) {
  142. Exit (' tag {list} must specify the Source property! ');
  143. }
  144. $attr _rows = $attr _page = ";
  145. if ($rows > 0) {
  146. $attr _rows = ' limit 0, '. $rows;
  147. }
  148. if (!empty ($sql)) {
  149. $data _sql = $sql;
  150. } else {
  151. $data _sql = ' select * from '. $source. ' `'.
  152. $attr _where. $attr _order. $attr _rows;
  153. }
  154. if ($pages = = ' true ' &&!empty ($size)) {
  155. $data _num = $data _ins->getrecnum ($data _sql);
  156. $page _cnt = ceil ($data _num/$size);
  157. Global $page;
  158. if (!isset ($page) | | $page < 1) $page = 1;
  159. if ($page > $page _cnt) $page = $page _cnt;
  160. $data _sql = ' select * from '. $source. ' ". $attr _where.
  161. $attr _order. ' Limit '. ($page-1) * $size. ', '. $size;
  162. $GLOBALS [' cfg_page_curr '] = $page;
  163. $GLOBALS [' cfg_page_prev '] = $page-1;
  164. $GLOBALS [' cfg_page_next '] = $page + 1;
  165. $GLOBALS [' cfg_page_nums '] = $page _cnt;
  166. if (function_exists (' List_pagelink ')) {
  167. $GLOBALS [' cfg_page_list '] = List_pagelink ($page, $page _cnt, 2);
  168. }
  169. }
  170. $data _idx = 0;
  171. $data _ret = $data _ins->sqlcmdexec ($data _sql);
  172. while ($row = $data _ins->getrecarr ($data _ret)) {
  173. if ($skip > 0 &&!empty ($flag)) {
  174. $data _idx! = 0 &&
  175. $data _idx% $skip = = 0 &&
  176. $data _str. = $flag;
  177. }
  178. $data _tmp = $html;
  179. $data _tmp = str_ireplace (' @idx ', $data _idx, $data _tmp);
  180. for ($i = 0; $i < count ($out _matches[0]); $i + +) {
  181. $data _tmp = str_ireplace ($out _matches[0][$i],
  182. $row [$out _matches[1][$i]], $data _tmp);
  183. }
  184. $data _str. = $data _tmp; $data _idx + +;
  185. }
  186. } else {
  187. if (empty ($source)) {
  188. Exit (' tag {data} must specify the Source property! ');
  189. }
  190. $data _sql = ' select * from '. $source.
  191. ". $attr _where. $attr _order;
  192. $row = $data _ins->getonerec ($data _sql);
  193. if (Is_array ($row)) {
  194. $data _tmp = $html;
  195. for ($i = 0; $i < count ($out _matches[0]); $i + +) {
  196. $data _val = $row [$out _matches[1][$i];
  197. if (Empty ($out _matches[2][$i])) {
  198. $data _tmp = str_ireplace ($out _matches[0][$i], $data _val, $data _tmp);
  199. } else {
  200. $attr _str = $out _matches[2][$i];
  201. $attr _ins = new ATTBT ($attr _str);
  202. $func _txt = $attr _ins->attrs[' function '];
  203. if (!empty ($func _txt)) {
  204. $func _tmp = Explode (' (', $func _txt);
  205. if (Function_exists ($func _tmp[0])) {
  206. Eval (' $func _ret = '. Str_ireplace (' @me ',
  207. ' \ '. $data _val. ' \ ', $func _txt));
  208. $data _tmp = str_ireplace ($out _matches[0][$i], $func _ret, $data _tmp);
  209. } else {
  210. Exit (' called a function that does not exist! ');
  211. }
  212. } else {
  213. Exit (' tag setting property is invalid! ');
  214. }
  215. }
  216. }
  217. $data _str. = $data _tmp;
  218. }
  219. }
  220. unset ($data _ins);
  221. return $data _str;
  222. } else {
  223. Exit (' tag setting property is invalid! ');
  224. }
  225. } else {
  226. Exit (' no tag attribute set! ');
  227. }
  228. }
  229. static public Function __callstatic ($name, $args) {
  230. Exit (' label {'. $name. '} does not exist! ');
  231. }
  232. }
  233. ?>
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.