template function call handling

This commit is contained in:
Uwe Tews
2014-12-30 16:43:42 +01:00
parent 1d248d6349
commit d3e26fb679
2 changed files with 17 additions and 15 deletions

View File

@@ -22,11 +22,11 @@ class Smarty_Internal_Function_Call_Handler
* This function handles calls to template functions defined by {function} * This function handles calls to template functions defined by {function}
* It does create a PHP function at the first call * It does create a PHP function at the first call
* *
* @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 string $_function PHP function name * @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
* *
* @return bool * @return bool
*/ */
@@ -39,14 +39,12 @@ class Smarty_Internal_Function_Call_Handler
// grab template function // grab template function
if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) { if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
// grab source info from file dependency // grab source info from file dependency
preg_match("/\s*'{$funcParam['source_uid']}'([\S\s]*?)\),/", $code, $match1); preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1);
unset($code); unset($code);
$output = ''; $output = '';
// make PHP function known // make PHP function known
eval($match[0]); eval($match[0]);
if (function_exists($_function)) { if (function_exists($_function)) {
// call template function
$_function ($_smarty_tpl, $_params);
// search cache file template // search cache file template
$tplPtr = $_smarty_tpl; $tplPtr = $_smarty_tpl;
while (!isset($tplPtr->cached) && isset($tplPtr->parent)) { while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
@@ -57,8 +55,8 @@ class Smarty_Internal_Function_Call_Handler
$cache = $tplPtr->cached; $cache = $tplPtr->cached;
$content = $cache->read($tplPtr); $content = $cache->read($tplPtr);
if ($content) { if ($content) {
// check if we must update file dependency // check if we must update file dependency
if (!preg_match("/'{$funcParam['source_uid']}'([\S\s]*?)'nocache_hash'/", $content, $match2)) { if (!preg_match("/'{$funcParam['uid']}'([\S\s]*?)'nc_h'/", $content, $match2)) {
$content = preg_replace("/('file_dependency'([\S\s]*?)\()/", "\\1{$match1[0]}", $content); $content = preg_replace("/('file_dependency'([\S\s]*?)\()/", "\\1{$match1[0]}", $content);
} }
$cache->write($tplPtr, $content . "<?php " . $match[0] . "?>\n"); $cache->write($tplPtr, $content . "<?php " . $match[0] . "?>\n");

View File

@@ -383,12 +383,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
* @param object $_smarty_tpl template object * @param object $_smarty_tpl template object
* @param array $params parameter array * @param array $params parameter array
* @param bool $nocache true if called nocache * @param bool $nocache true if called nocache
*
* @throws SmartyException
*/ */
public function callTemplateFunction($name, $_smarty_tpl, $params, $nocache) public function callTemplateFunction($name, $_smarty_tpl, $params, $nocache)
{ {
if (isset($_smarty_tpl->properties['tpl_function']['param'][$name])) { if (isset($_smarty_tpl->properties['tpl_function']['param'][$name])) {
if (!$_smarty_tpl->caching || ($_smarty_tpl->caching && $nocache) || $_smarty_tpl->properties['type'] !== 'cache') { if (!$_smarty_tpl->caching || ($_smarty_tpl->caching && $nocache)) {
//$_smarty_tpl->properties['tpl_function']['to_cache'][$name] = true;
$function = $_smarty_tpl->properties['tpl_function']['param'][$name]['call_name']; $function = $_smarty_tpl->properties['tpl_function']['param'][$name]['call_name'];
} else { } else {
if (isset($_smarty_tpl->properties['tpl_function']['param'][$name]['call_name_caching'])) { if (isset($_smarty_tpl->properties['tpl_function']['param'][$name]['call_name_caching'])) {
@@ -399,12 +400,15 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} }
if (function_exists($function)) { if (function_exists($function)) {
$function ($_smarty_tpl, $params); $function ($_smarty_tpl, $params);
return;
}
// try to load template function dynamically
if (Smarty_Internal_Function_Call_Handler::call($name, $_smarty_tpl, $function, $params, $nocache)) {
return; return;
} }
if ($_smarty_tpl->caching) {
// try to load template function dynamically
if (Smarty_Internal_Function_Call_Handler::call($name, $_smarty_tpl, $function, $params, $nocache)) {
$function ($_smarty_tpl, $params);
return;
}
}
} }
throw new SmartyException("Unable to find template function '{$name}'"); throw new SmartyException("Unable to find template function '{$name}'");
} }