53 public function compile($args, $compiler, $parameter)
58 if ($_attr[
'nocache'] ===
true) {
59 $compiler->trigger_template_error(
'nocache option not allowed', $compiler->lex->taglineno);
61 unset($_attr[
'nocache']);
62 $_name = trim($_attr[
'name'],
"'\"");
63 $compiler->parent_compiler->templateProperties[
'tpl_function'][$_name] = array();
64 $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->has_nocache_code, $compiler->template->required_plugins, $compiler->template->caching);
65 $this->
openTag($compiler,
'function', $save);
67 $compiler->compiles_template_function =
true;
69 $compiler->template->required_plugins = array(
'compiled' => array(),
'nocache' => array());
71 $compiler->template->has_nocache_code =
false;
72 $compiler->template->caching =
true;
105 $saved_data = $this->
closeTag($compiler, array(
'function'));
106 $_attr = $saved_data[0];
107 $_name = trim($_attr[
'name'],
"'\"");
109 $compiler->compiles_template_function =
false;
110 $compiler->parent_compiler->templateProperties[
'tpl_function'][$_name][
'called_functions'] = $compiler->called_functions;
111 $compiler->parent_compiler->templateProperties[
'tpl_function'][$_name][
'compiled_filepath'] = $compiler->parent_compiler->template->compiled->filepath;
112 $compiler->parent_compiler->templateProperties[
'tpl_function'][$_name][
'uid'] = $compiler->template->source->uid;
113 $compiler->called_functions = array();
114 $_parameter = $_attr;
115 unset($_parameter[
'name']);
117 $_paramsArray = array();
118 foreach ($_parameter as $_key => $_value) {
120 $_paramsArray[] =
"$_key=>$_value";
122 $_paramsArray[] =
"'$_key'=>$_value";
125 if (!empty($_paramsArray)) {
126 $_params =
'array(' . implode(
",", $_paramsArray) .
')';
127 $_paramsCode =
"\$params = array_merge($_params, \$params);\n";
131 $_functionCode = $compiler->parser->current_buffer;
135 $_funcName =
"smarty_template_function_{$_name}_{$compiler->template->properties['nocache_hash']}";
136 $_funcNameCaching = $_funcName .
'_nocache';
137 if ($compiler->template->has_nocache_code) {
138 $compiler->parent_compiler->templateProperties[
'tpl_function'][$_name][
'call_name_caching'] = $_funcNameCaching;
140 $output .=
"/* {$_funcNameCaching} */\n";
141 $output .=
"if (!function_exists('{$_funcNameCaching}')) {\n";
142 $output .=
"function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
144 if (!empty($compiler->template->required_plugins[
'compiled'])) {
145 foreach ($compiler->template->required_plugins[
'compiled'] as $tmp) {
146 foreach ($tmp as $data) {
147 $output .=
"if (!is_callable('{$data['function']}')) require_once '{$data['file']}';\n";
151 if (!empty($compiler->template->required_plugins[
'nocache'])) {
152 $output .=
"echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
153 foreach ($compiler->template->required_plugins[
'nocache'] as $tmp) {
154 foreach ($tmp as $data) {
155 $output .=
"if (!is_callable(\'{$data['function']}\')) require_once \'{$data['file']}\';\n";
158 $output .=
"?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';\n";
160 $output .=
"ob_start();\n";
161 $output .= $_paramsCode;
162 $output .=
"\$_smarty_tpl->properties['saved_tpl_vars'][] = \$_smarty_tpl->tpl_vars;\n";
163 $output .=
"foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}";
164 $output .=
"\$params = var_export(\$params, true);\n";
165 $output .=
"echo \"/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
166 $output .=
"\\\$saved_tpl_vars = \\\$_smarty_tpl->tpl_vars;\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value);\n}\n?>";
167 $output .=
"/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/\n\";?>";
169 $compiler->parser->current_buffer->append_subtree($_functionCode);
170 $output =
"<?php echo \"/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
171 $output .=
"foreach (Smarty::\\\$global_tpl_vars as \\\$key => \\\$value){\n";
172 $output .=
"if (\\\$_smarty_tpl->tpl_vars[\\\$key] === \\\$value) \\\$saved_tpl_vars[\\\$key] = \\\$value;\n}\n";
173 $output .=
"\\\$_smarty_tpl->tpl_vars = \\\$saved_tpl_vars;?>\n";
174 $output .=
"/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/\";\n?>";
175 $output .=
"<?php echo str_replace('{$compiler->template->properties['nocache_hash']}', \$_smarty_tpl->properties['nocache_hash'], ob_get_clean());\n";
176 $output .=
"\$_smarty_tpl->tpl_vars = array_pop(\$_smarty_tpl->properties['saved_tpl_vars']);\n}\n}\n";
177 $output .=
"/*/ {$_funcName}_nocache */\n\n";
180 $_functionCode =
new Smarty_Internal_ParseTree_Tag($compiler->parser, preg_replace_callback(
"/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%\*\/';(\?>\n)?)/", array($this,
'removeNocache'), $_functionCode->to_smarty_php()));
182 $compiler->parent_compiler->templateProperties[
'tpl_function'][$_name][
'call_name'] = $_funcName;
184 $output .=
"/* {$_funcName} */\n";
185 $output .=
"if (!function_exists('{$_funcName}')) {\n";
186 $output .=
"function {$_funcName}(\$_smarty_tpl,\$params) {\n";
188 if (!empty($compiler->template->required_plugins[
'nocache'])) {
189 $compiler->template->required_plugins[
'compiled'] = array_merge($compiler->template->required_plugins[
'compiled'], $compiler->template->required_plugins[
'nocache']);
191 if (!empty($compiler->template->required_plugins[
'compiled'])) {
192 foreach ($compiler->template->required_plugins[
'compiled'] as $tmp) {
193 foreach ($tmp as $data) {
194 $output .=
"if (!is_callable('{$data['function']}')) require_once '{$data['file']}';\n";
198 $output .=
"\$saved_tpl_vars = \$_smarty_tpl->tpl_vars;\n";
199 $output .= $_paramsCode;
200 $output .=
"foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}?>";
202 $compiler->parser->current_buffer->append_subtree($_functionCode);
203 $output =
"<?php foreach (Smarty::\$global_tpl_vars as \$key => \$value){\n";
204 $output .=
"if (\$_smarty_tpl->tpl_vars[\$key] === \$value) \$saved_tpl_vars[\$key] = \$value;\n}\n";
205 $output .=
"\$_smarty_tpl->tpl_vars = \$saved_tpl_vars;\n}\n}\n";
206 $output .=
"/*/ {$_funcName} */\n\n";
209 $compiler->parent_compiler->templateFunctionCode .= $compiler->parser->current_buffer->to_smarty_php();
211 $compiler->parser->current_buffer = $saved_data[1];
213 $compiler->template->has_nocache_code = $saved_data[2];
214 $compiler->template->required_plugins = $saved_data[3];
215 $compiler->template->caching = $saved_data[4];
226 $code = preg_replace(
"/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->properties['nocache_hash']}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->properties['nocache_hash']}%%\*\/';(\?>\n)?)/",
'', $match[0]);
227 $code = str_replace(array(
'\\\'',
'\\\\\''), array(
'\'',
'\\\''), $code);