久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP實(shí)現(xiàn)簡單的模板引擎功能示例

這篇文章主要介紹了PHP實(shí)現(xiàn)簡單的模板引擎功能,結(jié)合實(shí)例形式詳細(xì)分析了PHP實(shí)現(xiàn)模板引擎功能的模版類、編譯類、控制器類及模板文件等實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)簡單的模板引擎功能。分享給大家供大家參考,具體如下:

php web開發(fā)中廣泛采取mvc的設(shè)計(jì)模式,controller傳遞給view層的數(shù)據(jù),必須通過模板引擎才能解析出來。實(shí)現(xiàn)一個簡單的僅僅包含if,foreach標(biāo)簽,解析$foo變量的模板引擎。

編寫template模板類和compiler編譯類。代碼如下:

<?php
namespace foo\base;
use foo\base\Object;
use foo\base\Compiler;
/**
* 
*/
class Template extends Object
{
  private $_config = [
    'suffix' => '.php',//文件后綴名
    'templateDir' => '../views/',//模板所在文件夾
    'compileDir' => '../runtime/cache/views/',//編譯后存放的目錄
    'suffixCompile' => '.php',//編譯后文件后綴
    'isReCacheHtml' => false,//是否需要重新編譯成靜態(tài)html文件
    'isSupportPhp' => true,//是否支持php的語法
    'cacheTime' => 0,//緩存時間,單位秒
  ];
  private $_file;//帶編譯模板文件
  private $_valueMap = [];//鍵值對
  private $_compiler;//編譯器
  public function __construct($compiler, $config = [])
  {
    $this->_compiler = $compiler;
    $this->_config = array_merge($this->_config, $config);
  }
  /**
   * [assign 存儲控制器分配的鍵值]
   * @param [type] $values [鍵值對集合]
   * @return [type]     [description]
   */
  public function assign($values)
  {
    if (is_array($values)) {
      $this->_valueMap = $values;
    } else {
      throw new \Exception('控制器分配給視圖的值必須為數(shù)組!');
    }
    return $this;
  }
  /**
   * [show 展現(xiàn)視圖]
   * @param [type] $file [帶編譯緩存的文件]
   * @return [type]    [description]
   */
  public function show($file)
  {
    $this->_file = $file;
    if (!is_file($this->path())) {
      throw new \Exception('模板文件'. $file . '不存在!');
    }
    $compileFile = $this->_config['compileDir'] . md5($file) . $this->_config['suffixCompile'];
    $cacheFile = $this->_config['compileDir'] . md5($file) . '.html';
    //編譯后文件不存在或者緩存時間已到期,重新編譯,重新生成html靜態(tài)緩存
    if (!is_file($compileFile) || $this->isRecompile($compileFile)) {
      $this->_compiler->compile($this->path(), $compileFile, $this->_valueMap);
      $this->_config['isReCacheHtml'] = true;
      if ($this->isSupportPhp()) {
        extract($this->_valueMap, EXTR_OVERWRITE);//從數(shù)組中將變量導(dǎo)入到當(dāng)前的符號表
      }
    }
    if ($this->isReCacheHtml()) {
      ob_start();
      ob_clean();
      include($compileFile);
      file_put_contents($cacheFile, ob_get_contents());
      ob_end_flush();
    } else {
      readfile($cacheFile);
    }
  }
  /**
   * [isRecompile 根據(jù)緩存時間判斷是否需要重新編譯]
   * @param [type] $compileFile [編譯后的文件]
   * @return boolean       [description]
   */
  private function isRecompile($compileFile)
  {
    return time() - filemtime($compileFile) > $this->_config['cacheTime'];
  }
  /**
   * [isReCacheHtml 是否需要重新緩存靜態(tài)html文件]
   * @return boolean [description]
   */
  private function isReCacheHtml()
  {
    return $this->_config['isReCacheHtml'];
  }
  /**
   * [isSupportPhp 是否支持php語法]
   * @return boolean [description]
   */
  private function isSupportPhp()
  {
    return $this->_config['isSupportPhp'];
  }
  /**
   * [path 獲得模板文件路徑]
   * @return [type] [description]
   */
  private function path()
  {
    return $this->_config['templateDir'] . $this->_file . $this->_config['suffix'];
  }
}

<?php
namespace foo\base;
use foo\base\Object;
/**
* 
*/
class Compiler extends Object
{
  private $_content;
  private $_valueMap = [];
  private $_patten = [
    '#\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}#',
    '#\{if (.*?)\}#',
    '#\{(else if|elseif) (.*?)\}#',
    '#\{else\}#',
    '#\{foreach \\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}#',
    '#\{\/(foreach|if)}#',
    '#\{\\^(k|v)\}#',
  ];
  private $_translation = [
    "<?php echo \$this->_valueMap['\\1']; ?>",
    '<?php if (\\1) {?>',
    '<?php } else if (\\2) {?>',
    '<?php }else {?>',
    "<?php foreach (\$this->_valueMap['\\1'] as \$k => \$v) {?>",
    '<?php }?>',
    '<?php echo \$\\1?>'
  ];
  /**
   * [compile 編譯模板文件]
   * @param [type] $source  [模板文件]
   * @param [type] $destFile [編譯后文件]
   * @param [type] $values  [鍵值對]
   * @return [type]      [description]
   */
  public function compile($source, $destFile, $values)
  {
    $this->_content = file_get_contents($source);
    $this->_valueMap = $values;
    if (strpos($this->_content, '{$') !== false) {
      $this->_content = preg_replace($this->_patten, $this->_translation, $this->_content);
    }
    file_put_contents($destFile, $this->_content);
  }
}

【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。

相關(guān)文檔推薦

這篇文章主要介紹了PHP有序表查找之插值查找算法,簡單分析了插值查找算法的概念、原理并結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)針對有序表插值查找的相關(guān)操作技巧,需要的朋友可以參考下
下面小編就為大家分享一篇ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP實(shí)現(xiàn)APP微信支付的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP實(shí)現(xiàn)的多維數(shù)組排序算法,結(jié)合實(shí)例形式對比分析了php針對多維數(shù)組及帶有鍵名的多維數(shù)組進(jìn)行排序相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
這篇文章主要為大家詳細(xì)介紹了php結(jié)合ajaxuploadfile實(shí)現(xiàn)無刷新文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇文章給大家詳細(xì)介紹了PHP開發(fā)接口使用RSA進(jìn)行加密解密方法,對此有興趣的朋友可以學(xué)習(xí)下。
主站蜘蛛池模板: 玖玖精品 | 久久精品国产99国产精品亚洲 | 国产在线二区 | 免费午夜剧场 | 亚洲成人av一区二区 | av在线伊人| 97国产爽爽爽久久久 | 国产精品五区 | 亚洲精品日韩视频 | 亚洲精品一区二区三区在线 | 国产在线观看一区二区三区 | 在线看亚洲 | 91av小视频 | 国产视频中文字幕在线观看 | 久草新在线 | 激情欧美一区二区三区中文字幕 | 一级片在线免费看 | 国产精品久久九九 | 日本精品一区二区三区视频 | 91综合网| 天天曰天天曰 | 欧美黄色一区 | www.jizzjizz | 成人精品福利 | 欧美电影大全 | www.久久国产精品 | 97av视频 | 久久丝袜视频 | 国产综合久久 | 国产 日韩 欧美 在线 | 亚洲大片 | 91国在线视频 | 在线观看国产网站 | 久久国内精品 | 97av视频 | 91色网站| 国产日韩欧美在线一区 | 精品欧美一区二区三区免费观看 | 亚洲 欧美 日韩 精品 | 久久精品国产亚洲夜色av网站 | 久久久久久黄 |