예제 #1
0
 /**
  * render template
  *
  * @param  bool $merge_tpl_vars   if true parent template variables merged in to local scope
  * @param  bool $no_output_filter if true do not run output filter
  * @param  bool $display          true: display, false: fetch null: subtemplate
  *
  * @throws Exception
  * @throws SmartyException
  * @return string rendered template output
  */
 public function render($merge_tpl_vars = false, $no_output_filter = true, $display = null)
 {
     $parentIsTpl = $this->parent instanceof Smarty_Internal_Template;
     if ($this->smarty->debugging) {
         Smarty_Internal_Debug::start_template($this, $display);
     }
     // checks if template exists
     if (!$this->source->exists) {
         if ($parentIsTpl) {
             $parent_resource = " in '{$this->parent->template_resource}'";
         } else {
             $parent_resource = '';
         }
         throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
     }
     $save_tpl_vars = null;
     $save_config_vars = null;
     // merge all variable scopes into template
     if ($merge_tpl_vars) {
         // save local variables
         $save_tpl_vars = $this->tpl_vars;
         $save_config_vars = $this->config_vars;
         $ptr_array = array($this);
         $ptr = $this;
         while (isset($ptr->parent)) {
             $ptr_array[] = $ptr = $ptr->parent;
         }
         $ptr_array = array_reverse($ptr_array);
         $parent_ptr = reset($ptr_array);
         $tpl_vars = $parent_ptr->tpl_vars;
         $config_vars = $parent_ptr->config_vars;
         while ($parent_ptr = next($ptr_array)) {
             if (!empty($parent_ptr->tpl_vars)) {
                 $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars);
             }
             if (!empty($parent_ptr->config_vars)) {
                 $config_vars = array_merge($config_vars, $parent_ptr->config_vars);
             }
         }
         if (!empty(Smarty::$global_tpl_vars)) {
             $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars);
         }
         $this->tpl_vars = $tpl_vars;
         $this->config_vars = $config_vars;
     }
     $_smarty_old_error_level = isset($this->smarty->error_reporting) ? error_reporting($this->smarty->error_reporting) : null;
     // check URL debugging control
     if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') {
         Smarty_Internal_Debug::debugUrl($this);
     }
     // disable caching for evaluated code
     if ($this->source->recompiled) {
         $this->caching = false;
     }
     // read from cache or render
     $isCacheTpl = $this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED;
     if ($isCacheTpl) {
         if (!isset($this->cached)) {
             $this->loadCached();
         }
         $this->cached->isCached($this);
     }
     if (!$isCacheTpl || !$this->cached->valid) {
         if ($isCacheTpl) {
             $this->properties['tpl_function'] = array();
         }
         // render template (not loaded and not in cache)
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::start_render($this);
         }
         if (!$this->source->uncompiled) {
             // render compiled code
             if (!isset($this->compiled)) {
                 $this->loadCompiled();
             }
             $content = $this->compiled->render($this);
         } else {
             $content = $this->source->renderUncompiled($this);
         }
         if (!$this->source->recompiled && empty($this->properties['file_dependency'][$this->source->uid])) {
             $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->getTimeStamp(), $this->source->type);
         }
         if ($parentIsTpl) {
             $this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']);
             //$this->parent->properties['tpl_function'] = array_merge($this->parent->properties['tpl_function'], $this->properties['tpl_function']);
         }
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::end_render($this);
         }
         // write to cache when necessary
         if (!$this->source->recompiled && $isCacheTpl) {
             if ($this->smarty->debugging) {
                 Smarty_Internal_Debug::start_cache($this);
             }
             $this->cached->updateCache($this, $content, $no_output_filter);
             $compile_check = $this->smarty->compile_check;
             $this->smarty->compile_check = false;
             if ($parentIsTpl) {
                 $this->properties['tpl_function'] = $this->parent->properties['tpl_function'];
             }
             if (!$this->cached->processed) {
                 $this->cached->process($this, true);
             }
             $this->smarty->compile_check = $compile_check;
             $content = $this->getRenderedTemplateCode();
             if ($this->smarty->debugging) {
                 Smarty_Internal_Debug::end_cache($this);
             }
         } else {
             if ($this->parent instanceof Smarty_Internal_Template && !empty($this->properties['nocache_hash']) && !empty($this->parent->properties['nocache_hash'])) {
                 // replace nocache_hash
                 $content = str_replace("{$this->properties['nocache_hash']}", $this->parent->properties['nocache_hash'], $content);
                 $this->parent->has_nocache_code = $this->parent->has_nocache_code || $this->has_nocache_code;
             }
         }
     } else {
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::start_cache($this);
         }
         $content = $this->cached->render($this);
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::end_cache($this);
         }
     }
     if ((!$this->caching || $this->has_nocache_code || $this->source->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
         $content = Smarty_Internal_Filter_Handler::runFilter('output', $content, $this);
     }
     if (isset($_smarty_old_error_level)) {
         error_reporting($_smarty_old_error_level);
     }
     // display or fetch
     if ($display) {
         if ($this->caching && $this->smarty->cache_modified_check) {
             $this->cached->cacheModifiedCheck($this, $content);
         } else {
             echo $content;
         }
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::end_template($this);
         }
         // debug output
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::display_debug($this, true);
         }
         if ($merge_tpl_vars) {
             // restore local variables
             $this->tpl_vars = $save_tpl_vars;
             $this->config_vars = $save_config_vars;
         }
         return '';
     } else {
         if ($merge_tpl_vars) {
             // restore local variables
             $this->tpl_vars = $save_tpl_vars;
             $this->config_vars = $save_config_vars;
         }
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::end_template($this);
         }
         if ($this->smarty->debugging == 2 and $display === false) {
             if ($this->smarty->debugging) {
                 Smarty_Internal_Debug::display_debug($this, true);
             }
         }
         if ($parentIsTpl) {
             $this->parent->properties['tpl_function'] = array_merge($this->parent->properties['tpl_function'], $this->properties['tpl_function']);
             foreach ($this->required_plugins as $code => $tmp1) {
                 foreach ($tmp1 as $name => $tmp) {
                     foreach ($tmp as $type => $data) {
                         $this->parent->required_plugins[$code][$name][$type] = $data;
                     }
                 }
             }
         }
         // return cache content
         return $content;
     }
 }