/** * Returns the cached template output * * @param object $_template current template * @return string |booelan the template content or false if the file does not exist */ public function getCachedContents($_template, $no_render = false) { if (!$no_render) { ob_start(); } $_smarty_tpl = $_template; include $_template->getCachedFilepath(); if ($no_render) { return null; } else { return ob_GET_clean(); } }
/** * Render the output using the compiled template or the PHP template source * * The rendering process is accomplished by just including the PHP files. * The only exceptions are evaluated templates (string template). Their code has * to be evaluated */ public function renderTemplate() { if ($this->resource_object->usesCompiler) { if ($this->mustCompile() && $this->compiled_template === null) { $this->compileTemplateSource(); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($this); } $_smarty_tpl = $this; ob_start(); if ($this->resource_object->isEvaluated) { eval("?>" . $this->compiled_template); } else { include $this->getCompiledFilepath(); // check file dependencies at compiled code if ($this->smarty->compile_check) { if (!empty($this->properties['file_dependency'])) { $this->mustCompile = false; $resource_type = null; $resource_name = null; foreach ($this->properties['file_dependency'] as $_file_to_check) { if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'extends' || $_file_to_check[2] == 'php') { $mtime = filemtime($_file_to_check[0]); } else { $this->getResourceTypeName($_file_to_check[0], $resource_type, $resource_name); $resource_handler = $this->loadTemplateResourceHandler($resource_type); $mtime = $resource_handler->getTemplateTimestampTypeName($resource_type, $resource_name); } // If ($mtime != $_file_to_check[1]) { if ($mtime > $_file_to_check[1]) { $this->mustCompile = true; break; } } if ($this->mustCompile) { // recompile and render again ob_GET_clean(); $this->compileTemplateSource(); ob_start(); include $this->getCompiledFilepath(); } } } } } else { if (is_callable(array($this->resource_object, 'renderUncompiled'))) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($this); } ob_start(); $this->resource_object->renderUncompiled($this); } else { throw new SmartyException("Resource '{$this->resource_type}' must have 'renderUncompiled' methode"); } } $this->rendered_content = ob_GET_clean(); if (!$this->resource_object->isEvaluated && empty($this->properties['file_dependency'][$this->templateUid])) { $this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp(), $this->resource_type); } if ($this->parent instanceof Smarty_Internal_Template) { $this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']); 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; } } } } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_render($this); } // write to cache when nessecary if (!$this->resource_object->isEvaluated && ($this->caching == Smarty::CACHING_LIFETIME_SAVED || $this->caching == Smarty::CACHING_LIFETIME_CURRENT)) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_cache($this); } $this->properties['has_nocache_code'] = false; // get text between non-cached items $cache_split = preg_split("!/\\*%%SmartyNocache:{$this->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmartyNocache:{$this->properties['nocache_hash']}%%\\*/!s", $this->rendered_content); // get non-cached items preg_match_all("!/\\*%%SmartyNocache:{$this->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmartyNocache:{$this->properties['nocache_hash']}%%\\*/!s", $this->rendered_content, $cache_parts); $output = ''; // loop over items, stitch back together foreach ($cache_split as $curr_idx => $curr_split) { // escape PHP tags in template content $output .= preg_replace('/(<%|%>|<\\?php|<\\?|\\?>)/', '<?php echo \'$1\'; ?>', $curr_split); if (isset($cache_parts[0][$curr_idx])) { $this->properties['has_nocache_code'] = true; // remove nocache tags from cache output $output .= preg_replace("!/\\*/?%%SmartyNocache:{$this->properties['nocache_hash']}%%\\*/!", '', $cache_parts[0][$curr_idx]); } } if (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output'])) { $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $this); } // rendering (must be done before writing cache file because of {function} nocache handling) $_smarty_tpl = $this; ob_start(); eval("?>" . $output); $this->rendered_content = ob_GET_clean(); // write cache file content $this->writeCachedContent('<?php if (!$no_render) {?>' . $output . '<?php } ?>'); if ($this->smarty->debugging) { Smarty_Internal_Debug::end_cache($this); } } else { // var_dump('renderTemplate', $this->has_nocache_code, $this->template_resource, $this->properties['nocache_hash'], $this->parent->properties['nocache_hash'], $this->rendered_content); if ($this->has_nocache_code && !empty($this->properties['nocache_hash']) && !empty($this->parent->properties['nocache_hash'])) { // replace nocache_hash $this->rendered_content = preg_replace("/{$this->properties['nocache_hash']}/", $this->parent->properties['nocache_hash'], $this->rendered_content); $this->parent->has_nocache_code = $this->has_nocache_code; } } }