佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > php tpl模板引擎定義與使用示例

php tpl模板引擎定義與使用示例

熱門(mén)標(biāo)簽:Linux服務(wù)器 電子圍欄 Mysql連接數(shù)設(shè)置 服務(wù)器配置 團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云 銀行業(yè)務(wù)

本文實(shí)例講述了php tpl模板引擎定義與使用。分享給大家供大家參考,具體如下:

tpl.php

?php
namespace tpl;
/**
* Class Tpl
*/
class Tpl
{
  protected $view_dir;//模板文件
  protected $cache_dir;//緩存文件
  protected $lifetime;//過(guò)期時(shí)間
  protected $vars = [];//存放顯示變量的數(shù)組
   /**
   * Tpl constructor.
   * @param string $view_dir
   * @param string $cache_dir
   * @param string $lifetime
   */
  public function __construct($view_dir='', $cache_dir='', $lifetime='')
  {
    //如果模板文件不為空,則設(shè)置,為空則為默認(rèn)值
    if (!empty($view_dir)) {
      if ($this->check_dir($view_dir)) {
        $this->view_dir = $view_dir;
      }
    }
    //如果緩存文件不為空,則設(shè)置,為空時(shí)為默認(rèn)值
    if (!empty($cache_dir)) {
      if ($this->check_dir($cache_dir)) {
        $this->cache_dir = $cache_dir;
      }
    }
    //如果過(guò)期時(shí)間不為空,則設(shè)置,為空時(shí)為默認(rèn)值
    if (!empty($lifetime)) {
      $this->lifetime = $lifetime;
    }
  }
   /**
   * 對(duì)外公開(kāi)的方法
   * @param string $name
   * @param string $value
   */
  public function assign($name, $value)
  {
    $this->vars[$name] = $value;//將傳入的參數(shù)以鍵值對(duì)存入數(shù)組中
  }
   /**
   * 測(cè)試文件
   * @param $dir_path
   * @return bool
   */
  protected function check_dir($dir_path)
  {
    //如果文件不存在或不是文件夾,則創(chuàng)建
    if (!file_exists($dir_path) || !is_dir($dir_path)) {
      return mkdir($dir_path, 0777, true);
    }
    //如果文件不可讀或不可寫(xiě),則設(shè)置模式
    if (!is_writable($dir_path) || !is_readable($dir_path)) {
      return chmod($dir_path, 0777);
    }
    return true;
  }
   /**
   * 展示方法
   * @param $view_name
   * @param bool $isInclude
   * @param null $uri
   */
  public function display($view_name, $isInclude=true, $uri=null)
  {
    //通過(guò)傳入的文件名,得到模板文件路徑
    $view_path = rtrim($this->view_dir, '/') . '/' . $view_name;
    //判斷路徑是否存在
    if (!file_exists($view_path)) {
      die('文件不存在');
    }
    //通過(guò)傳入的文件名得到緩存文件名
    $cache_name = md5($view_name . $uri) . '.php';
    //緩過(guò)緩存文件名得到緩存路徑
    $cache_path = rtrim($this->cache_dir, '/') . '/' .$cache_name;
    //判斷緩存文件是否存在,如果不存在,重新生成
    if (!file_exists($cache_path)) {
      $php = $this->compile($view_path);//解析模板文件
      file_put_contents($cache_path, $php);//緩存文件重新生成
    } else {
      //如果緩存文件存在,判斷是否過(guò)期,判斷模板文件是否被修改
      $is_time_out = (filectime($cache_path) + $this->lifetime) > time() ? false : true;
      $is_change = filemtime($view_path) > filemtime($cache_path) ? true : false;
      //如果緩存文件過(guò)期或模板文件被修改,重新生成緩存文件
      if ($is_time_out || $is_change) {
        $php = $this->compile($view_path);
        file_put_contents($cache_path, $php);
      }
    }
    if ($isInclude) {
      extract($this->vars);//解析傳入變量的數(shù)組
      include $cache_path;//展示緩存
    }
  }
   /**
   * 正則解析模板文件
   * @param string $file_name
   * @return mixed|string
   */
  protected function compile($file_name)
  {
    $html = file_get_contents($file_name);//獲取模板文件
    //正則轉(zhuǎn)換數(shù)組
    $array = [
      '{$%%}' => '?=$\1?>',
      '{foreach %%}' => '?php foreach (\1): ?>',
      '{/foreach}' => '?php endforeach ?>',
      '{include %%}' => '',
      '{if %%}' => '?php if (\1): ?>',
      '{/if}' => '?php endif ?>',
      '{for %%}' => '?php for (\1): ?>',
      '{/for}' => '?php endfor ?>',
      '{switch %%}' => '?php switch (\1) ?>',
      '{/switch}' => '?php endswitch ?>'
    ];
    //遍歷數(shù)組,生成正則表達(dá)式
    foreach ($array AS $key=>$value) {
      //正則表達(dá)式,
      $pattern = '#' . str_replace('%%', '(.+?)' , preg_quote($key, '#')) . '#';
      if (strstr($pattern, 'include')) {
        $html = preg_replace_callback($pattern, [$this, 'parseInclude'], $html);
      } else {
        $html = preg_replace($pattern, $value, $html);
      }
    }
    return $html;
  }
   /**
   * 處理include表達(dá)式
   * @param array $data
   * @return string
   */
  protected function parseInclude($data)
  {
    $file_name = trim($data[1], '\'"');
    $this->display($file_name, false);
    $cache_name = md5($file_name) . '.php';
    $cache_path = rtrim($this->cache_dir, '/') . '/' . $cache_name;
    return '?php include "'.$cache_path.'" ?>';
  }
}

user_tpl,,,,從數(shù)據(jù)庫(kù)中取值,作為參數(shù)傳到模板文件,再解析模板文件

?php
include './sql/pdo.sql.php';
include 'tpl.php';
 $tpl = new tpl\Tpl('./view/', './cache/', 3000);
$link = new pdo_sql();
$dat = ['menu_name', 'menu_url'];
$res = $link->table('blog_menu')->field($dat)->order('id ASC')->select();
$tpl->assign('menu', $res);
$tpl->display('index.html');

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • ThinkPHP使用smarty模板引擎的方法
  • 需要使用php模板的朋友必看的很多個(gè)頂級(jí)PHP模板引擎比較分析
  • PHP模板引擎Smarty的緩存使用總結(jié)
  • PHP原生模板引擎 最簡(jiǎn)單的模板引擎
  • php模板引擎技術(shù)簡(jiǎn)單實(shí)現(xiàn)
  • PHP中MVC模式的模板引擎開(kāi)發(fā)經(jīng)驗(yàn)分享
  • 在Yii框架中使用PHP模板引擎Twig的例子
  • 簡(jiǎn)單的自定義php模板引擎
  • PHP模板引擎Smarty中變量的使用方法示例
  • TMDPHP 模板引擎使用教程
  • Pain 全世界最小最簡(jiǎn)單的PHP模板引擎 (普通版)

標(biāo)簽:衢州 蚌埠 棗莊 廣元 大理 萍鄉(xiāng) 江蘇 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php tpl模板引擎定義與使用示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    弥渡县| 滕州市| 靖江市| 南雄市| 美姑县| 阳原县| 威宁| 永仁县| 双柏县| 亚东县| 会理县| 揭阳市| 宁城县| 察雅县| 高唐县| 亚东县| 宁都县| 武胜县| 法库县| 准格尔旗| 剑阁县| 桃园县| 鄂尔多斯市| 海丰县| 沙雅县| 石景山区| 乾安县| 凉城县| 景谷| 玛多县| 历史| 木里| 自治县| 高雄市| 永和县| 南投县| 南宁市| 克东县| 桂平市| 特克斯县| 广丰县|