Home  >  Article  >  Backend Development  >  miniSmarty Simple Smarty

miniSmarty Simple Smarty

WBOY
WBOYOriginal
2016-07-25 09:08:09819browse
简易的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. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn