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;
 }
Esempio n. 2
0
 private function parse_filter()
 {
     $this->content = preg_replace_callback($this->_patten_filter, function ($match) {
         list($_, $args, $name, $params, $mods) = $match;
         $name = trim($name);
         if (!empty(ViewEngine::$modifiers) and ViewEngine::modifier_exists($name)) {
             //eval("\$args = \"$args\";");
             $params = $args . (empty($params) ? '' : ',' . $params);
             $ov = '\\Faddle\\View\\ViewEngine::call_modifier(\'' . $name . '\',' . $params . ')';
             if (!empty($mods)) {
                 $mods = addcslashes($mods, '\'');
                 $mods = preg_replace('/\\|(\\w+)(?:\\:[\\(]{0,1}([^\\)\\|]+)[\\)]{0,1}|)/u', '\'$1\'=>\'$2\',', $mods);
                 $mods = substr($mods, 0, -1);
                 try {
                     eval("\$mods = array({$mods});");
                     foreach ($mods as $name => $params) {
                         $name = trim($name);
                         $params = $ov . (empty($params) ? '' : ',' . $params);
                         $ov = '\\Faddle\\View\\ViewEngine::call_modifier(\'' . $name . '\',' . $params . ')';
                     }
                 } catch (\Exception $e) {
                     trigger_error("模板过滤器[" . var_export($mods, true) . "]\r\n" . sprintf('解析出错:%s', $e->getMessage()), E_USER_WARNING);
                 }
             }
             return '<?php try{ echo @' . $ov . '; } catch(\\Exception $e) { ' . 'echo "<!-- [filter error] : {$e->getMessage()} -->"; ' . 'trigger_error(sprintf(\'模板过滤器解析出错:%s\', $e->getMessage()), E_USER_WARNING); } ?>';
         } else {
             return "<?php echo {$args}; ?>";
         }
     }, $this->content);
     return $this->content;
 }