/**
  * 单件模式调用方法
  *
  * @static
  * @return object Template
  */
 public static function getInstance()
 {
     if (!self::$_instance instanceof self) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Exemple #2
0
 /**
  * 解析处理一个模板文件
  *
  * @param  string $filePath  模板文件路径
  * @param  array  $vars 需要给模板变量赋值的变量
  * @return void
  */
 public function renderFile($filePath, $vars)
 {
     try {
         //加载DiscuzTemplate
         load_plugin("DiscuzTemplate");
         $template = DiscuzTemplate::getInstance();
         //使用单件模式实例化模板类
         //判断是否传递配置参数
         if (empty($this->params) || !isset($this->params['cache_dir'])) {
             throw new TM_Exception("Discuz template engine configure [cache_dir] not set, please  TM_View->factory() entry params");
         }
         //设置模板参数
         $this->params['cache_dir'] = $this->params['cache_dir'];
         $this->params['template_dir'] = isset($this->params['template_dir']) ? $this->params['template_dir'] : APP_VIEW_DIR;
         $this->params['auto_update'] = isset($this->params['auto_update']) ? $this->params['auto_update'] : true;
         $this->params['cache_lifetime'] = isset($this->params['cache_lifetime']) ? $this->params['cache_lifetime'] : 1;
         $template->setOptions($this->params);
         //设置模板参数
         //检查模板文件
         if (!is_file($filePath) || !is_readable($filePath)) {
             throw new TM_Exception("View file " . $filePath . " not exist or not readable");
         }
         //设置模板变量
         if (!empty($vars)) {
             foreach ($vars as $key => $value) {
                 ${$key} = $value;
             }
         }
         //输出到模板文件
         include $template->getfile($filePath);
     } catch (Exception $e) {
         throw new TM_Exception($e->getMessage());
     } catch (TM_Exception $e) {
         throw $e;
     }
 }