mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 19:04:27 +02:00
add code of template functions called in nocache mode dynamically to cache file
This commit is contained in:
@@ -8,7 +8,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class does call function defined with the {function} tag
|
* This class does handles template functions defined with the {function} tag missing in cache file.
|
||||||
|
* It can happen when the template function was called with the nocache option or within a nocache section.
|
||||||
|
* The template function will be loaded from it's compiled template file, executed and added to the cache file
|
||||||
|
* for later use.
|
||||||
*
|
*
|
||||||
* @package Smarty
|
* @package Smarty
|
||||||
* @subpackage PluginsInternal
|
* @subpackage PluginsInternal
|
||||||
@@ -21,7 +24,7 @@ class Smarty_Internal_Function_Call_Handler
|
|||||||
*
|
*
|
||||||
* @param string $_name template function name
|
* @param string $_name template function name
|
||||||
* @param Smarty_Internal_Template $_smarty_tpl
|
* @param Smarty_Internal_Template $_smarty_tpl
|
||||||
* @param $_function
|
* @param string $_function PHP function name
|
||||||
* @param array $_params Smarty variables passed as call parameter
|
* @param array $_params Smarty variables passed as call parameter
|
||||||
* @param bool $_nocache nocache flag
|
* @param bool $_nocache nocache flag
|
||||||
*
|
*
|
||||||
@@ -30,25 +33,38 @@ class Smarty_Internal_Function_Call_Handler
|
|||||||
public static function call($_name, Smarty_Internal_Template $_smarty_tpl, $_function, $_params, $_nocache)
|
public static function call($_name, Smarty_Internal_Template $_smarty_tpl, $_function, $_params, $_nocache)
|
||||||
{
|
{
|
||||||
$funcParam = $_smarty_tpl->properties['tpl_function']['param'][$_name];
|
$funcParam = $_smarty_tpl->properties['tpl_function']['param'][$_name];
|
||||||
|
if (is_file($funcParam['compiled_filepath'])) {
|
||||||
|
// read compiled file
|
||||||
$code = file_get_contents($funcParam['compiled_filepath']);
|
$code = file_get_contents($funcParam['compiled_filepath']);
|
||||||
|
// grab template function
|
||||||
if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
|
if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
|
||||||
$output = "\n";
|
$code = "\n";
|
||||||
$output .= $match[0];
|
$code .= $match[0];
|
||||||
$output .= "?>\n";
|
$code .= "?>\n";
|
||||||
}
|
unset($match);
|
||||||
unset($code, $match);
|
$output = '';
|
||||||
eval($output);
|
// make PHP function known
|
||||||
|
eval($code);
|
||||||
if (function_exists($_function)) {
|
if (function_exists($_function)) {
|
||||||
|
// call template function
|
||||||
$_function ($_smarty_tpl, $_params);
|
$_function ($_smarty_tpl, $_params);
|
||||||
|
// search cache file template
|
||||||
$tplPtr = $_smarty_tpl;
|
$tplPtr = $_smarty_tpl;
|
||||||
while (isset($tplPtr->parent) && !isset($tplPtr->parent->cached)) {
|
while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
|
||||||
$tplPtr = $tplPtr->parent;
|
$tplPtr = $tplPtr->parent;
|
||||||
}
|
}
|
||||||
if (isset($tplPtr->parent->cached)) {
|
// add template function code to cache file
|
||||||
$cached = $tplPtr->parent->cached;
|
if (isset($tplPtr->cached)) {
|
||||||
|
$cache = $tplPtr->cached;
|
||||||
|
$content = $cache->read($tplPtr);
|
||||||
|
if ($content) {
|
||||||
|
$cache->write($tplPtr, $content . "<?php " . $code);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user