/** * Call template function * * @param string $name template function name * @param object|\Smarty_Internal_Template $_smarty_tpl template object * @param array $params parameter array * @param bool $nocache true if called nocache * * @throws \SmartyException */ public function callTemplateFunction($name, Smarty_Internal_Template $_smarty_tpl, $params, $nocache) { if (isset($_smarty_tpl->tpl_function[$name])) { if (!$_smarty_tpl->caching || $_smarty_tpl->caching && $nocache) { $function = $_smarty_tpl->tpl_function[$name]['call_name']; } else { if (isset($_smarty_tpl->tpl_function[$name]['call_name_caching'])) { $function = $_smarty_tpl->tpl_function[$name]['call_name_caching']; } else { $function = $_smarty_tpl->tpl_function[$name]['call_name']; } } if (function_exists($function)) { $function($_smarty_tpl, $params); return; } // try to load template function dynamically if (Smarty_Internal_Function_Call_Handler::call($name, $_smarty_tpl, $function)) { $function($_smarty_tpl, $params); return; } } throw new SmartyException("Unable to find template function '{$name}'"); }
/** * Call template function * * @param string $name template function name * @param \Smarty_Internal_Template $_smarty_tpl template object * @param array $params parameter array * @param $cm * * @throws \SmartyException * */ public function callTemplateFunction($name, Smarty_Internal_Template $_smarty_tpl, $params, $cm) { if (isset($_smarty_tpl->context->templateFunctions[$name])) { if ($cm) { $function = $_smarty_tpl->context->templateFunctions[$name]['methodCaching']; } else { $function = $_smarty_tpl->context->templateFunctions[$name]['method']; } $obj = $_smarty_tpl->context->templateFunctions[$name]['obj']; if (is_callable(array($obj, $function))) { $obj->{$function}($_smarty_tpl, $params); return; } // try to load template function dynamically if (Smarty_Internal_Function_Call_Handler::call($name, $_smarty_tpl, $params, $function, $_smarty_tpl->context->templateFunctions[$name]['compiled_filepath'])) { $obj = $_smarty_tpl->context->templateFunctions[$name]['obj']; if (is_callable(array($obj, $function))) { $obj->{$function}($_smarty_tpl, $params); return; } } } throw new SmartyException("Unable to find template function '{$name}'"); }
/** * Renders the Smarty template function. * * Thanks to Uwe Tews for providing information on Smarty inner workings * allowing the call to the template function from within the plugin: * {@link http://stackoverflow.com/questions/9152047/in-smarty3-call-a-template-function-defined-by-the-function-tag-from-within-a}. * * \Smarty_Internal_Function_Call_Handler is defined in file * smarty/libs/sysplugins/smarty_internal_function_call_handler.php. * * @note The template functions do not return the HTML output, but put it * directly into the output buffer. * * @param Smarty_Internal_Template|string $template A template object or resource path * @param string $name Function name * @param array $attributes Attributes to pass to the template function */ public function renderTemplateFunction($template, $name, array $attributes = array()) { if (!$template instanceof \Smarty_Internal_Template) { $template = $this->createTemplate($template); } if ($template->caching) { \Smarty_Internal_Function_Call_Handler::call($name, $template, $attributes, $template->properties['nocache_hash'], false); } else { if (is_callable($function = 'smarty_template_function_' . $name)) { $function($template, $attributes); } else { throw new RuntimeException(sprintf('Template function "%s" is not defined in "%s".', $name, $template->source->filepath), -1, null, $template); } } }