当前位置:首页 > PHP教程 > php应用 > 列表

php tpl模板引擎定义与使用示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-10 16:13:47 浏览: 评论:0 

这篇文章主要介绍了php tpl模板引擎定义与使用,结合实例形式分析了php模板引擎的定义与使用相关操作技巧,需要的朋友可以参考下。

本文实例讲述了php tpl模板引擎定义与使用,分享给大家供大家参考,具体如下:

tpl.php

  1. <?php 
  2. namespace tpl; 
  3. /** 
  4. * Class Tpl 
  5. */ 
  6. class Tpl 
  7. { 
  8.   protected $view_dir;//模板文件 
  9.   protected $cache_dir;//缓存文件 
  10.   protected $lifetime;//过期时间 
  11.   protected $vars = [];//存放显示变量的数组 
  12.    /** 
  13.    * Tpl constructor. 
  14.    * @param string $view_dir 
  15.    * @param string $cache_dir 
  16.    * @param string $lifetime 
  17.    */ 
  18.   public function __construct($view_dir='', $cache_dir='', $lifetime='') 
  19.   { 
  20.     //如果模板文件不为空,则设置,为空则为默认值 
  21.     if (!emptyempty($view_dir)) { 
  22.       if ($this->check_dir($view_dir)) { 
  23.         $this->view_dir = $view_dir; 
  24.       } 
  25.     } 
  26.     //如果缓存文件不为空,则设置,为空时为默认值 
  27.     if (!emptyempty($cache_dir)) { 
  28.       if ($this->check_dir($cache_dir)) { 
  29.         $this->cache_dir = $cache_dir; 
  30.       } 
  31.     } 
  32.     //如果过期时间不为空,则设置,为空时为默认值 
  33.     if (!emptyempty($lifetime)) { 
  34.       $this->lifetime = $lifetime; 
  35.     } 
  36.   } 
  37.    /** 
  38.    * 对外公开的方法 
  39.    * @param string $name 
  40.    * @param string $value 
  41.    */ 
  42.   public function assign($name, $value) 
  43.   { 
  44.     $this->vars[$name] = $value;//将传入的参数以键值对存入数组中 
  45.   } 
  46.    /** 
  47.    * 测试文件 
  48.    * @param $dir_path 
  49.    * @return bool 
  50.    */ 
  51.   protected function check_dir($dir_path) 
  52.   { 
  53.     //如果文件不存在或不是文件夹,则创建 
  54.     if (!file_exists($dir_path) || !is_dir($dir_path)) { 
  55.       return mkdir($dir_path, 0777, true); 
  56.     } 
  57.     //如果文件不可读或不可写,则设置模式 
  58.     if (!is_writable($dir_path) || !is_readable($dir_path)) { 
  59.       return chmod($dir_path, 0777); 
  60.     } 
  61.     return true; 
  62.   } 
  63.    /** 
  64.    * 展示方法 
  65.    * @param $view_name 
  66.    * @param bool $isInclude 
  67.    * @param null $uri 
  68.    */ 
  69.   public function display($view_name, $isInclude=true, $uri=null) 
  70.   { 
  71.     //通过传入的文件名,得到模板文件路径 
  72.     $view_path = rtrim($this->view_dir, '/') . '/' . $view_name; 
  73.     //判断路径是否存在 
  74.     if (!file_exists($view_path)) { 
  75.       die('文件不存在'); 
  76.     } 
  77.     //通过传入的文件名得到缓存文件名 
  78.     $cache_name = md5($view_name . $uri) . '.php'; 
  79.     //缓过缓存文件名得到缓存路径 
  80.     $cache_path = rtrim($this->cache_dir, '/') . '/' .$cache_name; 
  81.     //判断缓存文件是否存在,如果不存在,重新生成 
  82.     if (!file_exists($cache_path)) { 
  83.       $php = $this->compile($view_path);//解析模板文件 
  84.       file_put_contents($cache_path, $php);//缓存文件重新生成 
  85.     } else { 
  86.       //如果缓存文件存在,判断是否过期,判断模板文件是否被修改 
  87.       $is_time_out = (filectime($cache_path) + $this->lifetime) > time() ? false : true; 
  88.       $is_change = filemtime($view_path) > filemtime($cache_path) ? true : false; 
  89.       //如果缓存文件过期或模板文件被修改,重新生成缓存文件 
  90.       if ($is_time_out || $is_change) { 
  91.         $php = $this->compile($view_path); 
  92.         file_put_contents($cache_path, $php); 
  93.       } 
  94.     } 
  95.     if ($isInclude) { 
  96.       extract($this->vars);//解析传入变量的数组 
  97.       include $cache_path;//展示缓存 
  98.     } 
  99.   } 
  100.    /** 
  101.    * 正则解析模板文件 
  102.    * @param string $file_name 
  103.    * @return mixed|string 
  104.    */ 
  105.   protected function compile($file_name) 
  106.   { 
  107.     $html = file_get_contents($file_name);//获取模板文件 
  108.     //正则转换数组 
  109.     $array = [ 
  110.       '{$%%}' => '<?=$\1?>', 
  111.       '{foreach %%}' => '<?php foreach (\1): ?>', 
  112.       '{/foreach}' => '<?php endforeach ?>', 
  113.       '{include %%}' => '', 
  114.       '{if %%}' => '<?php if (\1): ?>', 
  115.       '{/if}' => '<?php endif ?>', 
  116.       '{for %%}' => '<?php for (\1): ?>', 
  117.       '{/for}' => '<?php endfor ?>', 
  118.       '{switch %%}' => '<?php switch (\1) ?>', 
  119.       '{/switch}' => '<?php endswitch ?>' 
  120.     ]; 
  121.     //遍历数组,生成正则表达式 
  122.     foreach ($array AS $key=>$value) { 
  123.       //正则表达式, 
  124.       $pattern = '#' . str_replace('%%', '(.+?)' , preg_quote($key, '#')) . '#'; 
  125.       if (strstr($pattern, 'include')) { 
  126.         $html = preg_replace_callback($pattern, [$this, 'parseInclude'], $html); 
  127.       } else { 
  128.         $html = preg_replace($pattern, $value, $html); 
  129.       } 
  130.     } 
  131.     return $html; 
  132.   } 
  133.    /** 
  134.    * 处理include表达式 
  135.    * @param array $data 
  136.    * @return string 
  137.    */ 
  138.   protected function parseInclude($data) 
  139.   { 
  140.     $file_name = trim($data[1], '\'"'); 
  141.     $this->display($file_name, false); 
  142.     $cache_name = md5($file_name) . '.php'; 
  143.     $cache_path = rtrim($this->cache_dir, '/') . '/' . $cache_name; 
  144.     return '<?php include "'.$cache_path.'" ?>'; 
  145.   } 
  146. } 

user_tpl,,,,从数据库中取值,作为参数传到模板文件,再解析模板文件

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

Tags: tpl模板引擎定义

分享到: