首頁 >後端開發 >php教程 >miniSmarty 簡易Smarty

miniSmarty 簡易Smarty

WBOY
WBOY原創
2016-07-25 09:08:09845瀏覽
简易的smarty 对新手理解smarty有帮助 源于itcast韩顺平老师smarty第2、3讲
  1. class MyMiniSmarty{
  2. public $template_dir = "./templates";
  3. public $complie_dir = "./templates_c";
  4. public $tpl_vars = array();
  5. public function assign($tpl_var,$val = NULL){
  6. if(!empty($tpl_var)){
  7. $this->tpl_vars[$tpl_var] = $val;
  8. }
  9. }
  10. public function display($tpl_file){
  11. $tpl_file_path = $this->template_dir.$tpl_file;
  12. $complie_file_path = $this->complie_dir."com_".$tpl_file.".php";
  13. if (file_exists($tpl_file_path) || filemtime($tpl_file_path) < filemtime($complie_file_path)) {
  14. $tpl_file_content = file_get_contents($tpl_file_path);
  15. $pattern = array(
  16. '/{s*$([a-zA-Z0-0_]*)s*}/i'
  17. );
  18. $replace = array(
  19. 'tpl_vars["$程序猿闯子"] ?>'
  20. );
  21. $new_content = preg_replace($pattern, $replace, $tpl_file_content);
  22. try {
  23. file_put_contents($complie_file_path, $new_content);
  24. } catch (Exception $e) {
  25. echo $e->getMessage();
  26. }
  27. include $complie_file_path;
  28. }else {
  29. return FALSE;
  30. }
  31. }
  32. }
  33. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn