溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何實(shí)現(xiàn)模板引擎技術(shù)

發(fā)布時(shí)間:2021-08-27 10:37:33 來(lái)源:億速云 閱讀:138 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“php如何實(shí)現(xiàn)模板引擎技術(shù)”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“php如何實(shí)現(xiàn)模板引擎技術(shù)”這篇文章吧。

用了smarty,tp過(guò)后,也想了解了解其模板技術(shù)是怎么實(shí)現(xiàn),于是寫一個(gè)簡(jiǎn)單的模板類,大致就是讀取模板文件->替換模板文件的內(nèi)容->保存或者靜態(tài)化 

tpl.class.php主要解析

  assign 方法實(shí)現(xiàn)     

/**
     * 模板賦值操作
     * @param mixed $tpl_var 如果是字符串,就作為數(shù)組索引,如果是數(shù)組,就循環(huán)賦值
     * @param mixed $tpl_value 當(dāng)$tpl_var為string時(shí)的值,默認(rèn)為 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 方法實(shí)現(xiàn)

 /**
      * 生成編譯文件
      * @param string $tplFile 模板路徑
      * @param string $comFile 編譯路徑
      * @return string
     */
    private function fetch($tplFile,$comFile){

      //判斷編譯文件是否需要重新生成(編譯文件是否存在或者模板文件修改時(shí)間大于編譯文件的修改時(shí)間)
      if(!file_exists($comFile) || filemtime($tplFile) > filemtime($comFile)){
        //編譯,此處也可以使用ob_start()進(jìn)行靜態(tài)化
        $content = $this->tplReplace(file_get_contents($tplFile));
        file_put_contents($comFile, $content);
      }

    }

簡(jiǎn)單編譯方法:按照規(guī)則進(jìn)行正則替換

 /**
     * 編譯文件
     * @param string $content 待編譯的內(nèi)容
     * @return string
     */
    private function tplReplace($content){
      //轉(zhuǎn)義左右定界符 正則表達(dá)式字符
      $left = preg_quote($this->left_delimiter,'/');
      $right = preg_quote($this->right_delimiter,'/');

      //簡(jiǎn)單模擬編譯 變量
      $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

 /**
     * 輸出內(nèi)容
     * @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 = '}'; 

    //內(nèi)部臨時(shí)變量,存儲(chǔ)用戶賦值
    private $tpl_vars = array();

    //錯(cuò)誤信息
    private $errorMessage = '';

    /**
     * 修改類屬性的值
     * @param array $configs 需要修改的相關(guān)屬性及值
     * @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;
    }

測(cè)試

模板文件 testTpl.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>test_tpl_demo</title>
</head>
<body>
  {$name}:{$age}:{$message}
</body>
</html>
運(yùn)行文件 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

以上是“php如何實(shí)現(xiàn)模板引擎技術(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI