本文实例讲述了PHP模版引擎原理、定义与用法。分享给大家供大家参考,具体如下:

template存放模版源文件

模版编译工具类

Compline.class.php

<"/<\";
     $this->T_R[] = "&lt;";
   }
   //{$var}
   $this->T_P[] = "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/";
   //{foreach $b}或者{loop $b}
   $this->T_P[] = "/\{(loop|foreach) \\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/i";
   //{[K|V]}
   $this->T_P[] = "/\{([K|V])\}/";
   //{/foreach}或者{\loop}或者{\if}
   $this->T_P[] = "/\{\/(loop|foreach|if)}/i";
   //{if (condition)}
   $this->T_P[] = "/\{if (.* ";
   //{(else if | elseif)}
   $this->T_P[] = "/\{(else if|elseif) (.* ";
   //{else}
   $this->T_P[] = "/\{else\}/i";
   //{#...# 或者 *...#,注释}
   $this->T_P[] = "/\{(\#|\*)(.* ";
   $this->T_R[] = "<";
   $this->T_R[] = "<";
   $this->T_R[] = "<";
   $this->T_R[] = "<";
   $this->T_R[] = "<";
   $this->T_R[] = "<";
   $this->T_R[] = "<";
   $this->T_R[] = "";
  }
  public function compile()
  {
   $this->c_all();
   $this->c_staticFile();
   file_put_contents($this->comfile, $this->content);
  }
  public function c_all()
  {
   $this->content = preg_replace($this->T_P, $this->T_R, $this->content);
  }
  /**
  * 加入对JavaScript文件的解析
  * @return [type] [description]
  */
  public function c_staticFile()
  {
   $this->content = preg_replace('/\{\!(.* "htmlcode">
<"template dir isn't found!");
   }
   if(!is_dir($this->arrayConfig['compileDir']))
   {
     if(strtoupper(substr(PHP_OS,0,3)) === 'WIN')
     {
      mkdir($this->arrayConfig['compileDir']);
     }
     else
     {
      mkdir($this->arrayConfig['compileDir'], 0770, true);
     }
   }
   include('Compile.class.php');
  }
  public function getPath()
  {
   $this->arrayConfig['templateDir'] = strstr(realpath($this->arrayConfig['templateDir']), '\\', '/').'/';
   $this->arrayConfig['compileDir'] = strstr(realpath($this->arrayConfig['compileDir'])), '\\', '/').'/';
  }
  /**
  * 取得模板引擎的实例
  */
  public static function getInstance()
  {
   if(is_null(self::$instance))
   {
     self::$instance = new Template();
   }
   return self::$instance;
  }
  /**
  * 单独设置引擎参数
  * 也支持一次性设置多个参数
  */
  public function setConfig($key, $value = null)
  {
   if(is_array($key))
   {
     $this->arrayConfig = $key + $this->arrayConfig;
   }
   else
   {
     $this->arrayConfig[$key] = $value;
   }
  }
  /**
  * 获取当前模板引擎配置,仅供调试使用
  */
  public function getConfig($key = null)
  {
   if($key && array_key_exists($key, $this->arrayConfig))
   {
     return $this->arrayConfig[$key];
   }
   else
   {
     return $this->arrayConfig;
   }
  }
  /**
  * 注入单个变量
  */
  public function assign($key, $value)
  {
   $this->value[$key] = $value;
  }
  /**
  * 注入数组变量
  */
  public function assignArray($array)
  {
   if(is_array($array))
   {
     foreach ($array as $k => $v) {
      $this->value[$k] = $v;
     }
   }
  }
  /**
  * 获取模板的位置
  * @return [type] [description]
  */
  public function path()
  {
   return $this->arrayConfig['templateDir'].$this->file.$this->arrayConfig['suffix'];
  }
  /**
  * 判断配置文件是否要求缓存
  */
  public function needCache()
  {
   return $this->arrayConfig['cache_html'];
  }
  /**
  * 判断是否需要缓存
  */
  public function reCache($file)
  {
   $flag = false;
   $cacheFile = $this->arrayConfig['compileDir'].md5($file).$this->arrayConfig['suffix_cache'];
   if($this->arrayConfig['cache_html'] === true)
   {
     //需要缓存
     $timeFlag = (time() - @filemtime($cacheFile)) < $this->arrayConfig['cache_time'] "<br/>", '-------------------- debug_info--------------', "<br/>";
     echo '程序运行日期:', date("Y-m-d h:i:s"), "<br/>";
     echo '模板解析耗时:', $this->debug['spend'], '秒', "<br/>";
     echo '模板包含标签数目:', $this->debug['count'], "<br/>";
     echo '是否使用静态缓存:', $this->debug['cached'], "<br/>";
     echo '模板引擎实例参数:', var_dump($this->getConfig());
   }
  }
  /**
  * 清楚缓存的html文件
  * @return [type] [description]
  */
  public function clean()
  {
   if($path === null)
   {
     $path = $this->arrayConfig['compileDir'];
     $path = glob($path.'* '.$this->arrayConfig['suffix_cache']);
   }
   else
   {
     $path = $this->arrayConfig['compileDir'].md5($path).$this->arrayConfig['suffix_cache'];
   }
   foreach ((array)$path as $v) {
     unlink($v);
   }
  }
}

test.php

<"_blank" href="//www.jb51.net/Special/350.htm">PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

标签:
PHP,模版引擎

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“PHP模版引擎原理、定义与用法实例”

暂无“PHP模版引擎原理、定义与用法实例”评论...