国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php tpl模板引擎定義與使用示例

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

2021-08-12 16:47人生如初見_張默 PHP教程

這篇文章主要介紹了php tpl模板引擎定義與使用,結合實例形式分析了php模板引擎的定義與使用相關操作技巧,需要的朋友可以參考下

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

tpl.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace tpl;
/**
* Class Tpl
*/
class Tpl
{
  protected $view_dir;//模板文件
  protected $cache_dir;//緩存文件
  protected $lifetime;//過期時間
  protected $vars = [];//存放顯示變量的數組
   /**
   * Tpl constructor.
   * @param string $view_dir
   * @param string $cache_dir
   * @param string $lifetime
   */
  public function __construct($view_dir='', $cache_dir='', $lifetime='')
  {
    //如果模板文件不為空,則設置,為空則為默認值
    if (!empty($view_dir)) {
      if ($this->check_dir($view_dir)) {
        $this->view_dir = $view_dir;
      }
    }
    //如果緩存文件不為空,則設置,為空時為默認值
    if (!empty($cache_dir)) {
      if ($this->check_dir($cache_dir)) {
        $this->cache_dir = $cache_dir;
      }
    }
    //如果過期時間不為空,則設置,為空時為默認值
    if (!empty($lifetime)) {
      $this->lifetime = $lifetime;
    }
  }
   /**
   * 對外公開的方法
   * @param string $name
   * @param string $value
   */
  public function assign($name, $value)
  {
    $this->vars[$name] = $value;//將傳入的參數以鍵值對存入數組中
  }
   /**
   * 測試文件
   * @param $dir_path
   * @return bool
   */
  protected function check_dir($dir_path)
  {
    //如果文件不存在或不是文件夾,則創建
    if (!file_exists($dir_path) || !is_dir($dir_path)) {
      return mkdir($dir_path, 0777, true);
    }
    //如果文件不可讀或不可寫,則設置模式
    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)
  {
    //通過傳入的文件名,得到模板文件路徑
    $view_path = rtrim($this->view_dir, '/') . '/' . $view_name;
    //判斷路徑是否存在
    if (!file_exists($view_path)) {
      die('文件不存在');
    }
    //通過傳入的文件名得到緩存文件名
    $cache_name = md5($view_name . $uri) . '.php';
    //緩過緩存文件名得到緩存路徑
    $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 {
      //如果緩存文件存在,判斷是否過期,判斷模板文件是否被修改
      $is_time_out = (filectime($cache_path) + $this->lifetime) > time() ? false : true;
      $is_change = filemtime($view_path) > filemtime($cache_path) ? true : false;
      //如果緩存文件過期或模板文件被修改,重新生成緩存文件
      if ($is_time_out || $is_change) {
        $php = $this->compile($view_path);
        file_put_contents($cache_path, $php);
      }
    }
    if ($isInclude) {
      extract($this->vars);//解析傳入變量的數組
      include $cache_path;//展示緩存
    }
  }
   /**
   * 正則解析模板文件
   * @param string $file_name
   * @return mixed|string
   */
  protected function compile($file_name)
  {
    $html = file_get_contents($file_name);//獲取模板文件
    //正則轉換數組
    $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 ?>'
    ];
    //遍歷數組,生成正則表達式
    foreach ($array AS $key=>$value) {
      //正則表達式,
      $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表達式
   * @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,,,,從數據庫中取值,作為參數傳到模板文件,再解析模板文件

?
1
2
3
4
5
6
7
8
9
<?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');

希望本文所述對大家PHP程序設計有所幫助。

原文鏈接:https://blog.csdn.net/qq_42176520/article/details/80426384

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 蜜臀久久精品99国产精品日本 | 成人av网站在线观看 | 国产精品久久久久久久久久久免费看 | 亚洲91 | 日本一区二区三区四区 | 成版人性视频 | 国产三级在线 | 国内精品久久久久久中文字幕 | 日本精品一区二区三区在线观看视频 | 久久国产精品视频 | 一区二区三区国产视频 | 精品久久久久久久久久久久久久 | 欧美日韩激情 | 欧美国产综合一区 | 日韩3级在线观看 | 黄色资源网站 | 欧美一区二区三区视频 | 精品亚洲一区二区 | 亚洲欧美激情精品一区二区 | 91在线观看 | 成人免费视频观看视频 | 欧美视频三区 | 国产精品免费一区二区 | 欧美中文字幕一区二区 | 99久久婷婷国产精品综合 | 国产精品三级久久久久久电影 | 国产一区二区三区在线观看免费 | 国产成人一区二区三区 | 九色 在线 | 欧美激情小视频 | 国产乱码精品一区二区三区五月婷 | 不卡视频一二三区 | 欧美日韩视频第一页 | 很黄很色很爽的视频 | 成人综合色区 | 久久免费精品视频 | 先锋资源av| 欧美成人综合在线 | 亚洲精品视频区 | 国产区区 | 成人三区 |