本文实例讲述了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程序设计有所帮助。

风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。