类似于smarty之类的模板引擎的模板引擎原型

原文: http://blog.csdn.net/changdazhong/article/details/7372468 

 

最近有点闲,又好长时间没写什么代码了,就随便想点什么东西来写一写。按着自己的想法写了个简易版的模板引擎原型,估计smarty之类的模板引擎也是差不多这样实现。模板引擎的好处是实现业务逻辑和页面的分离,比起各种MVC的框架,模板引擎这东西实用多了。所以研究一下它的实现原理,好处还是有不少的。废话少说,直接上代码。

模板引擎代码:

  1. <?php  
  2. class Template  
  3. {  
  4.     private $tpl_file_name;  
  5.     private $internal_vars = array();  
  6.     private $compile_contents;  
  7.     private $html_contents;  
  8.   
  9.       
  10.     public function __construct($tpl_file_name) {  
  11.         $this->tpl_file_name = $tpl_file_name;  
  12.         $handle = fopen($tpl_file_name'r');  
  13.   
  14.         while (!feof($handle)) {  
  15.             $this->compile_contents .= fread($handle, 8192);  
  16.         }  
  17.   
  18.         fclose($handle);  
  19.     }  
  20.   
  21.       
  22.     private function compile() {  
  23.         $tmp_content = $this->formatInternalVars();  
  24.         $tmp_content .= preg_replace('/\{\$(.*?)\}/',   
  25.             "<?php echo \$global_arr['\\1']; ?>"$this->compile_contents);  
  26.   
  27.         $handle = fopen($this->tpl_file_name.'.php''w');  
  28.         fwrite($handle$tmp_content);  
  29.         fclose($handle);  
  30.     }  
  31.   
  32.   
  33.     public function getHtmlContents() {  
  34.         $this->compile();  
  35.   
  36.         ob_start();  
  37.         require_once $this->tpl_file_name.'.php';  
  38.         $this->html_contents = ob_get_contents();  
  39.         ob_end_clean();  
  40.   
  41.         return $this->html_contents;  
  42.     }  
  43.           
  44.   
  45.     public function assign($name$value) {  
  46.         $this->internal_vars[$name] = $value;  
  47.     }  
  48.       
  49.   
  50.     private function formatInternalVars() {  
  51.         $str = '<?php ';  
  52.         $str .= 'header("Content-type: text/html; charset=utf-8");';   
  53.         $str .= '$global_arr = array(';  
  54.         $len = strlen($str);  
  55.   
  56.         foreach($this->internal_vars as $k => $v) {  
  57.             if(strlen($str) != $len) {  
  58.                 $str .= ',';  
  59.             }  
  60.             $str .= "'$k'=>'$v'";  
  61.         }  
  62.           
  63.         $str .= ');';  
  64.         $str .= ' ?>';  
  65.   
  66.         return $str;  
  67.     }  
  68.   
  69.   
  70.     private function outputHtml() {  
  71.         $handle = fopen($this->tpl_file_name.'.html''w');  
  72.         fwrite($handle$this->getHtmlContents());  
  73.         fclose($handle);  
  74.     }  
  75.   
  76.   
  77.     public function display() {  
  78.         $this->outputHtml();  
  79.         echo $this->html_contents;     
  80.     }  
  81. }  
  82. ?>  

模板代码:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <HTML>  
  3.  <HEAD>  
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.   <TITLE>{$title}</TITLE>  
  6.  </HEAD>  
  7.   
  8.  <BODY>  
  9.   {$body}  
  10.  </BODY>  
  11. </HTML>  

测试代码:

  1. <?php  
  2. require 'Template.class.php';  
  3.   
  4. $tpl = new Template('index.htm');  
  5.   
  6. $tpl->assign('title''我的模板引擎 - 听我的');  
  7. $tpl->assign('body''中华大地,天下畅达,哈哈哈哈');  
  8.   
  9. $tpl->display();  
  10. ?>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值