示例#1
0
 public function render($file = null, $onlyparse = false)
 {
     $this->_trace['begin'] = microtime();
     // 读取模板文件
     if (!$this->real_file($file)) {
         trigger_error(sprintf('未找到模板文件:%s', $file), E_USER_WARNING);
         return false;
     }
     if ($this->cache()) {
         $cached = $this->load_from_cache();
         if ($cached) {
             $this->_trace['end'] = microtime();
             $this->content = $cached;
             return $cached;
         }
     }
     $contents = $this->fetch($this->file, false);
     // 载入模板解析类
     $parser = new ViewParser($contents, $this);
     $contents = $parser->parse();
     if (!$onlyparse) {
         $contents = $this->padding($contents);
     }
     if ($this->cache()) {
         if ($this->cacher) {
             OneCacheHelper::save($this->cache_file, $contents, $this->cacher);
         } else {
             // 获取缓冲区内的数据,并且创建缓存文件
             file_put_contents($this->cache_file, $contents);
         }
     }
     $this->_trace['end'] = microtime();
     $this->content = $contents;
     return $contents;
 }
示例#2
0
 private function parse_import()
 {
     if (preg_match_all($this->_patten_import, $this->content, $matchs, PREG_SET_ORDER)) {
         foreach ($matchs as $match) {
             $name = $match[1];
             $as = $match[2];
             $config = $this->engine ? $this->engine->config() : [];
             $engine = $this->engine;
             $ve = new ViewEngine($config);
             $body = $ve->fetch($name, false);
             $macros = ViewParser::import($body);
             //var_dump($macros);
             //json_decode(json_encode(self::$macros), true);
             $obj = new ViewMacro();
             $self =& $this;
             foreach ($macros as $key => $val) {
                 $obj->{$key} = function () use($val) {
                     $args = combine_arr(array_keys($val[0]), func_get_args()[0]);
                     $args = array_merge($val[0], $args);
                     $body = $this->parse($val[1]);
                     ob_start() and ob_clean();
                     extract($args, EXTR_OVERWRITE);
                     try {
                         eval('?>' . $body);
                     } catch (\Exception $e) {
                         ob_end_clean();
                         throw $e;
                     }
                     $body = ob_get_clean();
                     //echo "\r\n|".$body."|\r\n";
                     return $body;
                 };
             }
             if ($engine) {
                 $engine->{$as} = $obj;
             }
         }
         $this->content = preg_replace($this->_patten_import, '', $this->content);
     }
     return $this->content;
 }