php模板引擎技術簡單實現_php執行個體

來源:互聯網
上載者:User
用了smarty,tp過後,也想瞭解瞭解其模板技術是怎麼實現,於是寫一個簡單的模板類,大致就是讀模數板檔案->替換模板檔案的內容->儲存或者靜態化

tpl.class.php主要解析

  assign 方法實現

/**     * 模板賦值操作     * @param mixed $tpl_var 如果是字串,就作為數組索引,如果是數組,就迴圈賦值     * @param mixed $tpl_value 當$tpl_var為string時的值,預設為 null     */    public function assign($tpl_var,$tpl_value=null){      if(is_array($tpl_var) && count($tpl_var) > 0){        foreach ($tpl_var as $k => $v) {          $this->tpl_vars[$k] = $v;        }      }elseif($tpl_var){        $this->tpl_vars[$tpl_var] = $tpl_value;      }    }

fetch 方法實現

     /**      * 產生編譯檔案      * @param string $tplFile 模板路徑      * @param string $comFile 編譯路徑      * @return string     */    private function fetch($tplFile,$comFile){      //判斷編譯檔案是否需要重建(編譯檔案是否存在或者模板檔案修改時間大於編譯檔案的修改時間)      if(!file_exists($comFile) || filemtime($tplFile) > filemtime($comFile)){        //編譯,此處也可以使用ob_start()進行靜態化        $content = $this->tplReplace(file_get_contents($tplFile));        file_put_contents($comFile, $content);      }    }  

簡單編譯方法:按照規則進行正則替換

 /**     * 編譯檔案     * @param string $content 待編譯的內容     * @return string     */    private function tplReplace($content){      //轉義左右定界符 Regex字元      $left = preg_quote($this->left_delimiter,'/');      $right = preg_quote($this->right_delimiter,'/');      //簡單類比編譯 變數      $pattern = array(          //例如{$test}          '/'.$left.'\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)'.$right.'/i'        );      $replace = array(          '<?php echo $this->tpl_vars[\'${1}\']; ?>'        );      //正則處理      return preg_replace($pattern, $replace, $content);    }

display = fetch+echo

 /**     * 輸出內容     * @param string $fileName 模板檔案名稱     */    public function display($fileName){      //模板路徑      $tplFile = $this->template_dir.'/'.$fileName;      //判斷模板是否存在      if(!file_exists($tplFile)){        $this->errorMessage = '模板檔案不存在';        return false;      }      //編譯後的檔案      $comFile = $this->compile_dir.'/'.md5($fileName).'.php';      $this->fetch($tplFile,$comFile);             include $comFile;    }

其他屬性

 //模板檔案存放位置    private $template_dir = 'templates';     //編譯檔案存放位置    private $compile_dir = 'compiles';    //左定界符    private $left_delimiter = '{';    //右定界符     private $right_delimiter = '}';     //內部臨時變數,儲存使用者賦值    private $tpl_vars = array();    //錯誤資訊    private $errorMessage = '';    /**     * 修改類屬性的值     * @param array $configs 需要修改的相關屬性及值     * @return bool     */    public function setConfigs(array $configs){      if(count($configs) > 0){        foreach ($configs as $k => $v) {          if(isset($this->$k))            $this->$k = $v;        }        return true;      }      return false;    }

測試

模板檔案 testTpl.html

    test_tpl_demo  {$name}:{$age}:{$message}運行檔案 test_tpl.php<?php  require 'Tpl.class.php';    $tpl = new Tpl();  $tplarr = array(      'name'=>'waited',      'age'=>'100'    );  $tpl->assign($tplarr);  $tpl->assign('message','this is a demo');  $tpl->display('testTpl.html');?>

輸出:waited:100:this is a demo

產生編譯檔案:972fa4d270e295005c36c1dbc7e6a56c.php

以上就是本文的全部內容,希望對大家的學習有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.