Esempio n. 1
0
 public function show($template, $data = array())
 {
     list($engine, $file) = $this->path($template, false);
     if ($engine) {
         $engine = self::$config[$engine];
     } else {
         $engine = null;
     }
     $this->init_engine($engine);
     $this->data = array_merge($this->data, $data);
     $data = $this->data;
     $this->path = $file;
     //echo $template; var_dump($this->path);
     if ($this->path == false) {
         if (starts_with($template, ['http:', 'https:'])) {
             $this->path = $template;
             return file_get_contents($template);
         }
         trigger_error(sprintf('模板加载出错:%s', $template), E_USER_WARNING);
         return false;
     }
     $cache = array_key_exists('cache', $this->engine_config) ? $this->engine_config['cache'] : $this->cache;
     if ($cache) {
         $compiled_path = $this->compiled();
         if ($this->path and !$this->expired()) {
             $output = $this->storage->get($compiled_path);
             if (!empty($output)) {
                 return $output;
             }
         }
     }
     if (in_array($this->engine, array('default', 'faddle', 'view'))) {
         $view = ViewEngine::make($this->path, $data);
         $view->config($engine);
         $output = $view->render();
     } else {
         if (in_array($this->engine, array('text', 'html'))) {
             if (is_file($this->path)) {
                 $output = file_get_contents($this->path);
             } else {
                 $output = false;
             }
         } else {
             if (array_key_exists(strtolower($this->engine), self::$extends)) {
                 $func = self::$extends[strtolower($this->engine)];
                 try {
                     $output = call_user_func_array($func, array($this->path, $data));
                 } catch (\Exception $e) {
                     $output = false;
                 }
             } else {
                 if ($this->engine) {
                     var_dump(self);
                 } else {
                     ob_start() and ob_clean();
                     include $this->path;
                     $output = ob_get_clean();
                 }
             }
         }
     }
     if ($cache and $output) {
         $this->storage->put($compiled_path, $output);
     }
     return $output;
 }