首頁 >後端開發 >php教程 >有關php mvc模式的模板引擎開發經驗分享

有關php mvc模式的模板引擎開發經驗分享

WBOY
WBOY原創
2016-07-25 09:05:291259瀏覽
  1. /*
  2. * 類別:模板解析類別
  3. * 作者:51JS.COM-ZMM
  4. * 日期: 2011.3 .1
  5. * 信箱:304924248@qq.com
  6. * 部落格:http://www.cnblogs.com/cnzmm/
  7. * 連結:http://bbs.it-home.org
  8. */
  9. 類別模板{
  10. public $html, $vars, $bTag, $eTag;
  11. public $bFlag='{', $eFlag='}', $pfix='zmm:' ;
  12. 私人$資料夾,$ 檔;
  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. 空(標籤::$vars) && 標籤::$vars = &$this-> vars;
  20. }
  21. public function LoadTpl($tpl) {
  22. $this->file = $this->GetTplPath($tpl);
  23. 標籤::$file = &$this-> file;
  24. if (is_file($this->file)) {
  25. if ($this->GetTplHtml()) {
  26. $this->SetTplTags();
  27. } else {
  28. exit('範本檔案載入失敗!');
  29. }
  30. } else {
  31. exit('範本檔案['.$this->file.']不存在!');
  32. }
  33. }
  34. 私有函數GetTplPath($tpl) {
  35. $this->folder = WEBSITE_DIRROOT.
  36. $GLOBALS['cfg_tpl_root'];
  37. return $this->folder.'/this->folder.'/ '.$tpl;
  38. }
  39. 私有函數GetTplHtml() {
  40. $html = self::FmtTplHtml(file_get_contents($this->file));
  41. if (!empty($html) ) {
  42. $callFunc = Tags::$prefix.'語法';
  43. $this->html = 標籤::$callFunc($html, new Template());
  44. } else {
  45. exit('範本檔案內容為空! ');
  46. } 回傳true;
  47. }
  48. 靜態公用函數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. 標籤::$vars = &$this->vars;
  55. }
  56. }
  57. 公用函數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 = 新快取($time);
  62. $cache->Set($name, $this->html);
  63. }
  64. echo $this->html;沖洗();
  65. } else {
  66. exit('模板檔案內容為空!');
  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('範本文件內容為空!');
  74. }
  75. }
  76. 私有函數SetTplTags() {
  77. $this->SetPanelTags(); $this->SetTrunkTags(); $this->RegHatchVars();
  78. SetTrunkTags(); $this->RegHatchVars();
  79. SetTrunkTags(); $this->RegHatchVars();
  80. }
  81. 私有函數SetPanelTags() {
  82. $rule = $this->bTag.'([^'.$this->eFlag.']+)/'.$this-> ;電子標誌;
  83. preg_match_all('/'.$rule.'/ism', $this->html, $out_matches);
  84. $this->TransTag($out_matches, '面板');取消設定($out_matches);
  85. }
  86. 私有函數SetTrunkTags() {
  87. $rule = $this->bTag.'(w+)s*([^'.$this->eFlag.']*?)' .$ this->eFlag。
  88. '((?:(?!'.$this->bTag.')[Ss]*?|(?R))*)'.$this->eTag.'\1s*'。 $this->eFlag;
  89. preg_match_all('/'.$rule.'/ism', $this->html, $out_matches);
  90. $this->TransTag($out_matches, 'trunk');取消設定($out_matches);
  91. }
  92. 私人函數TransTag($result, $type) {
  93. if (!empty($result[0])) {
  94. switch ($type) {
  95. case '面板' : {
  96. for ($i = 0; $i $strTag = Explode(' ', $result [1][$i ], 2);
  97. if (strpos($strTag[0], '.')) {
  98. $itemArg =explode('.', $result[1][$i ], 2);
  99. $callFunc = Tags::$prefix.ucfirst($itemArg[0]);
  100. if (method_exists('Tags', $callFunc)) {
  101. $html = Tags: :$callFunc(chop($itemArg[1]));
  102. if ($html !== false) {
  103. $this->html = str_ireplace($result[0][$i], $html , $this->html);
  104. }
  105. }
  106. } else {
  107. $rule = '^([^s]+)s*([Ss]+)$';
  108. preg_match_all('/'.$rule.'/is', trim($result[1][$i]), $tmp_matches);
  109. $callFunc = Tags::$prefix.ucfirst($tmp_matches[1 ][0]);
  110. if (method_exists('Tags', $callFunc)) {
  111. $html = Tags::$callFunc($tmp_matches[2][0]);
  112. if ($ html !== false) {
  113. $this->html = str_ireplace($result[0][$i], $html, $this->html);
  114. }
  115. } 取消設定($ tmp_matches);
  116. }
  117. } 休息;
  118. }
  119. case 'trunk' : {
  120. for ($i = 0; $i $callFunc = 標籤: :$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. } 休息;
  126. }
  127. 預設:break;
  128. }
  129. } else {
  130. 回傳false;
  131. }
  132. }
  133. 私有函數RegHatchVars() {
  134. $this->SetPanelTags();
  135. }
  136. function __destruct() {}
}
?>
複製程式碼

二、標籤解析類別:(暫時提供data、list兩種標籤的解析思路)

  1. /*
  2. * 類別:標籤解析類別
  3. * 作者:51JS.COM-ZMM
  4. * 日期: 2011.3 .2
  5. * 信箱:304924248@qq.com
  6. * 部落格:http://www.cnblogs.com/cnzmm/
  7. * 連結:http://bbs.it-home.org
  8. */
  9. 類別標籤{
  10. 靜態私人$attrs=null;
  11. static public $file, $vars, $rule, $prefix='TAG_';
  12. 靜態公用函數TAG_Syntax($html , $that) {
  13. $rule = $that->bTag.'ifs+([^'.$that->eFlag.']+)s*'。 $that->eFlag;
  14. $html = preg_replace('/'.$rule.'/ism', '', $html);
  15. $rule = $that->bTag.'elseifs+([^'.$that->eFlag.']+)s*'.$that->eFlag;
  16. $html = preg_replace('/'.$rule.'/ ism', '', $html);
  17. $rule = $that->bTag.'elses*'.$that->eFlag;
  18. $html = preg_replace('/'.$rule.'/ism', '', $html);
  19. $rule = $that->bTag.'loops+(S+)s+(S+ )s*'.$that->eFlag;
  20. $html = preg_replace('/'.$rule.'/ism', '', $html );
  21. $rule = $that->bTag.'loops+(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.'phps*'.$that->eFlag.'((?:(?!'.
  26. $that->bTag.')[Ss] *?|(?R))*) '.$that->eTag.'phps*'.$that->eFlag;
  27. $html = preg_replace('/'.$rule.'/ism', '', $ html);
  28. 回傳自我::TAG_Execute($html);
  29. }
  30. 靜態公用函數TAG_List($attr, $html) {
  31. if (!empty($html)) {
  32. if (self::TAG_HaveTag($html)) {
  33. 回傳self::TAG_DealTag($attr, $html, true);
  34. } else {
  35. return self::TAG_GetData($attr, $ html, true);
  36. }
  37. } else {
  38. exit('標籤{list}的內容為空! ');
  39. }
  40. }
  41. 靜態公用函數TAG_Data($attr, $html) {
  42. if (!empty($html)) {
  43. if (self::TAG_HaveTag($ html)) {
  44. 回傳self::TAG_DealTag($attr, $html, false);
  45. } else {
  46. return self::TAG_GetData($attr, $html, false);
  47. }
  48. } else {
  49. exit('標籤{data}的內容為空!');
  50. }
  51. }
  52. 靜態公用函數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. extract(self::$vars, EXTR_OVERWRITE);
  58. }
  59. $file_inc = WEBSITE_DIRINC.'/buffer/'。
  60. md5(uniqid(rand(), true)).'.php';
  61. if ($fp = fopen($file_inc, 'xb')) {
  62. fwrite($fp, $html) ;
  63. if (fclose($fp)) {
  64. include($file_inc);
  65. $html = ob_get_contents();
  66. } 取消設定($fp);
  67. } else { exit('模板解析文件生成失敗!');
  68. } ob_end_clean(); @unlink($file_inc);
  69. 回傳$html;
  70. }
  71. 私有香蕉函式 TAG_HaveTag($html) {
  72. $bool_has = false;
  73. $tpl_ins = 新模板();
  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.')[Ss]*?|(?R))*)'.$tpl_ins->eTag.'\1s*'。 ->電子標誌;
  78. $bool_has = $bool_has || preg_match('/'.self::$rule.'/ism', $html);
  79. 取消設定($tpl_ins);
  80. 回傳$bool_has;
  81. }
  82. 靜態真空函式 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 $child_node[] = $out_matches[3][$i];
  87. $html = str_ireplace($out_matches[3][$i], '{-->>child_node_'.$i.'}
  88. $html = self::TAG_GetData($attr, $html, $list);
  89. for ($i = 0; $i $html = str_ireplace('{-->>child_node_'.$i .'}
  90. preg_match_all('/'.self::$rule.'/ism', $html, $tmp_matches);
  91. if (!empty($tmp_matches[0])) {
  92. for ($i = 0; $i $callFunc = self::$prefix.ucfirst($tmp_matches[1][$i]);
  93. if (method_exists('Tags', $callFunc)) {
  94. $temp = self::$callFunc($tmp_matches[2][$i], $tmp_matches[3][$i]);
  95. $html = str_ireplace($tmp_matches[0][$i], $temp, $html);
  96. }
  97. }
  98. }
  99. 未設定($tmp_matches);
  100. }
  101. 未設定($out_matches);回傳$html;
  102. }
  103. 靜態真空函數 TAG_GetData($attr, $html, $list=false) {
  104. if (!empty( $attr)) {
  105. $attr_ins = new Attbt($attr);
  106. $attr_arr = $attr_ins->attrs;
  107. if (is_array($attr_arr)) {
  108. extract($attr_arr, EXTR_OVERWRITE);
  109. $source = table_name($source, $column);
  110. $rule = '[字段:s*(w+)s*([^]]*?)s*/?]';
  111. preg_match_all('/'.$rule.'/is', $html, $out_matches);
  112. $data_str = '';
  113. $data_ins = new DataSql();
  114. $attr_where = $attr_order = '';
  115. if (!empty($where)) {
  116. $where = str_replace(',', ' 和 ', $where);
  117. $attr_where = '哪裡'。
  118. } else {
  119. $fed_name = '';
  120. $fed_ins = $data_ins->GetFedNeedle($source);
  121. $fed_cnt = $data_ins->GetFedCount($fed_ins);
  122. for ($i = 0; $i $fed_flag = $data_ins->GetFedFlag($fed_ins, $i);
  123. if (preg_match('/auto_increment/ism', $fed_flag)) {
  124. $fed_name = $data_ins->GetFedName($fed_ins, $i);
  125. 休息;
  126. }
  127. }
  128. if (!empty($fed_name))
  129. $attr_order = '按'.$fed_name 排序。 if ($list == true) {
  130. if (empty($source) &&empty($sql)) {
  131. exit('標籤{list}必須指定來源屬性!' );
  132. }
  133. $attr_rows = $attr_page = '';
  134. if ($rows > 0) {
  135. $attr_rows = '限制0,'.$rows;
  136. }
  137. if (!empty(sql$ ) )) {
  138. $data_sql = $sql
  139. } else {
  140. $data_sql = '從 `'.$source.' 選擇 *'`'。 ;
  141. }
  142. if ($pages=='true' && !empty($size)) {
  143. $data_num = $data_ins->GetRecNum($data_sql);
  144. $page_cnt = ceil( $data_num / $size);
  145. 全域$Pages;
  146. if (!isset($page) || $page if ($page > $page_cnt) $page = $page_cnt;
  147. $data_sql = '從`'.$source.'`'.$attr_where中選擇*。 '.$size;
  148. $GLOBALS['cfg_page_curr'] = $page;
  149. $GLOBALS['cfg_page_prev'] = $page - 1;
  150. $GLOBALS['cfg_page_next'] = $page + 1 ; ;
  151. $GLOBALS['cfg_page_nums'] = $page_cnt;
  152. if (function_exists('list_pagelink')) {
  153. $GLOBALS['cfg_page_list'] = list_pagelink($page, $page_cnt, 2) ;
  154. }
  155. }
  156. $data_idx = 0;
  157. $data_ret = $data_ins->SqlCmdExec($data_sql);
  158. while ($row = $data_ins->GetRecArr($data_ret)) {
  159. if ($skip > 0 && !empty($flag)) {
  160. $data_idx != 0 &&
  161. $ data_idx % $skip == 0 &&
  162. $data_str .= $flag;
  163. }
  164. $data_tmp = $html;
  165. $data_tmp = str_ireplace('@idx', $data_idx, $data_tmp);
  166. for ($i = 0; $i $data_tmp = str_ireplace($out_matches[0][$i],
  167. $行[$out_matches[1][$i]], $data_tmp);
  168. }
  169. $data_str .= $data_tmp; $data_idx++;
  170. }
  171. } else {
  172. if (empty($source)) {
  173. exit('標籤{data}必須指定source屬性!');
  174. }
  175. $data_sql = '從 `'.$source 選取 *。
  176. $row = $data_ins->GetOneRec($data_sql);
  177. if (is_array($row)) {
  178. $data_tmp = $html;
  179. for ($i = 0; $i $data_val = $row[$out_matches[1][$i]];
  180. if (empty($out_matches[2][$i])) {
  181. $data_tmp = str_ireplace($out_matches[0][$i], $data_val, $data_tmp);
  182. } else {
  183. $attr_str = $out_matches[2][$i];
  184. $attr_ins = new Attbt($attr_str);
  185. $func_txt = $attr_ins->attrs['function'];
  186. if (!empty($func_txt)) {
  187. $func_tmp =explode('(', $func_txt);
  188. if (function_exists($func_tmp[0])) {
  189. eval(' $func_ret ='.str_ireplace('@me',
  190. '''.$data_val.''', $func_txt));
  191. $data_tmp = str_ireplace($out_matches[0][$i], $ func_ret, $data_tmp);
  192. } else {
  193. exit('呼叫了不存在的函數!');
  194. }
  195. } else {
  196. exit('標籤設定屬性無效!' );
  197. }
  198. }
  199. $data_str .=
  200. }
  201. }
  202. 取消設定($data_ins);
  203. 回傳$data_str >} else {
  204. 退出; ('標籤設定屬性無效!')
  205. }
  206. } else {
  207. exit('沒有設定標籤屬性!');
  208. static public function __callStatic($name, $args) {
  209. exit('標籤{'.$name.'}不!');
  210. }
  211. }
  212. 存在嗎?');
  213. }
  214. }
  215. 存在嗎?
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn