当前位置:Gxlcms > PHP教程 > miniSmarty简易Smarty

miniSmarty简易Smarty

时间:2021-07-01 10:21:17 帮助过:19人阅读

简易的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. ?>

人气教程排行