4 * TplFunction Runtime Methods callTemplateFunction
7 * @subpackage PluginsInternal
11 class Smarty_Internal_Runtime_TplFunction
14 * Call template function
16 * @param \Smarty_Internal_Template $tpl template object
17 * @param string $name template function name
18 * @param array $params parameter array
19 * @param bool $nocache true if called nocache
21 * @throws \SmartyException
23 public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
25 $funcParam = isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] :
26 (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : null);
27 if (isset($funcParam)) {
28 if (!$tpl->caching || ($tpl->caching && $nocache)) {
29 $function = $funcParam[ 'call_name' ];
31 if (isset($funcParam[ 'call_name_caching' ])) {
32 $function = $funcParam[ 'call_name_caching' ];
34 $function = $funcParam[ 'call_name' ];
37 if (function_exists($function)) {
38 $this->saveTemplateVariables($tpl, $name);
39 $function ($tpl, $params);
40 $this->restoreTemplateVariables($tpl, $name);
43 // try to load template function dynamically
44 if ($this->addTplFuncToCache($tpl, $name, $function)) {
45 $this->saveTemplateVariables($tpl, $name);
46 $function ($tpl, $params);
47 $this->restoreTemplateVariables($tpl, $name);
51 throw new SmartyException("Unable to find template function '{$name}'");
55 * Register template functions defined by template
57 * @param \Smarty|\Smarty_Internal_Template|\Smarty_Internal_TemplateBase $obj
58 * @param array $tplFunctions source information array of template functions defined in template
59 * @param bool $override if true replace existing functions with same name
61 public function registerTplFunctions(Smarty_Internal_TemplateBase $obj, $tplFunctions, $override = true)
64 $override ? array_merge($obj->tplFunctions, $tplFunctions) : array_merge($tplFunctions, $obj->tplFunctions);
65 // make sure that the template functions are known in parent templates
66 if ($obj->_isSubTpl()) {
67 $obj->smarty->ext->_tplFunction->registerTplFunctions($obj->parent, $tplFunctions, false);
69 $obj->smarty->tplFunctions = $override ? array_merge($obj->smarty->tplFunctions, $tplFunctions) :
70 array_merge($tplFunctions, $obj->smarty->tplFunctions);
75 * Return source parameter array for single or all template functions
77 * @param \Smarty_Internal_Template $tpl template object
78 * @param null|string $name template function name
80 * @return array|bool|mixed
82 public function getTplFunction(Smarty_Internal_Template $tpl, $name = null)
85 return isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] :
86 (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : false);
88 return empty($tpl->tplFunctions) ? $tpl->smarty->tplFunctions : $tpl->tplFunctions;
94 * Add template function to cache file for nocache calls
96 * @param Smarty_Internal_Template $tpl
97 * @param string $_name template function name
98 * @param string $_function PHP function name
102 public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
104 $funcParam = $tpl->tplFunctions[ $_name ];
105 if (is_file($funcParam[ 'compiled_filepath' ])) {
106 // read compiled file
107 $code = file_get_contents($funcParam[ 'compiled_filepath' ]);
108 // grab template function
109 if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
110 // grab source info from file dependency
111 preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1);
113 // make PHP function known
115 if (function_exists($_function)) {
116 // search cache file template
118 while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
119 $tplPtr = $tplPtr->parent;
121 // add template function code to cache file
122 if (isset($tplPtr->cached)) {
123 /* @var Smarty_Template_Cached $cache */
124 $cache = $tplPtr->cached;
125 $content = $cache->read($tplPtr);
127 // check if we must update file dependency
128 if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) {
129 $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content);
131 $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr,
132 preg_replace('/\s*\?>\s*$/', "\n", $content) .
133 "\n" . preg_replace(array('/^\s*<\?php\s+/',
134 '/\s*\?>\s*$/',), "\n",
146 * Save current template variables on stack
148 * @param \Smarty_Internal_Template $tpl
149 * @param string $name stack name
151 public function saveTemplateVariables(Smarty_Internal_Template $tpl, $name)
153 $tpl->_cache[ 'varStack' ][] =
154 array('tpl' => $tpl->tpl_vars, 'config' => $tpl->config_vars, 'name' => "_tplFunction_{$name}");
158 * Restore saved variables into template objects
160 * @param \Smarty_Internal_Template $tpl
161 * @param string $name stack name
163 public function restoreTemplateVariables(Smarty_Internal_Template $tpl, $name)
165 if (isset($tpl->_cache[ 'varStack' ])) {
166 $vars = array_pop($tpl->_cache[ 'varStack' ]);
167 $tpl->tpl_vars = $vars[ 'tpl' ];
168 $tpl->config_vars = $vars[ 'config' ];