3 * Smarty Internal Plugin Compile Function
4 * Compiles the {function} {/function} tags
12 * Smarty Internal Plugin Compile Function Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
21 * Attribute definition: Overwrites base class.
24 * @see Smarty_Internal_CompileBase
26 public $required_attributes = array('name');
29 * Attribute definition: Overwrites base class.
32 * @see Smarty_Internal_CompileBase
34 public $shorttag_order = array('name');
37 * Attribute definition: Overwrites base class.
40 * @see Smarty_Internal_CompileBase
42 public $optional_attributes = array('_any');
45 * Compiles code for the {function} tag
47 * @param array $args array with attributes from parser
48 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
49 * @param array $parameter array with compilation parameter
52 * @throws \SmartyCompilerException
54 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
56 // check and get attributes
57 $_attr = $this->getAttributes($compiler, $args);
59 if ($_attr[ 'nocache' ] === true) {
60 $compiler->trigger_template_error('nocache option not allowed', null, true);
62 unset($_attr[ 'nocache' ]);
63 $_name = trim($_attr[ 'name' ], "'\"");
64 $compiler->parent_compiler->tpl_function[ $_name ] = array();
65 $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
66 $compiler->template->caching);
67 $this->openTag($compiler, 'function', $save);
68 // Init temporary context
69 $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
70 $compiler->template->compiled->has_nocache_code = false;
76 * Smarty Internal Plugin Compile Functionclose Class
79 * @subpackage Compiler
81 class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
89 private $compiler = null;
92 * Compiles code for the {/function} tag
94 * @param array $args array with attributes from parser
95 * @param object|\Smarty_Internal_TemplateCompilerBase $compiler compiler object
96 * @param array $parameter array with compilation parameter
100 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
102 $this->compiler = $compiler;
103 $saved_data = $this->closeTag($compiler, array('function'));
104 $_attr = $saved_data[ 0 ];
105 $_name = trim($_attr[ 'name' ], "'\"");
106 $compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
107 $compiler->parent_compiler->template->compiled->filepath;
108 $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
109 $_parameter = $_attr;
110 unset($_parameter[ 'name' ]);
112 $_paramsArray = array();
113 foreach ($_parameter as $_key => $_value) {
115 $_paramsArray[] = "$_key=>$_value";
117 $_paramsArray[] = "'$_key'=>$_value";
120 if (!empty($_paramsArray)) {
121 $_params = 'array(' . implode(",", $_paramsArray) . ')';
122 $_paramsCode = "\$params = array_merge($_params, \$params);\n";
126 $_functionCode = $compiler->parser->current_buffer;
127 // setup buffer for template function code
128 $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
130 $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
131 $_funcNameCaching = $_funcName . '_nocache';
132 if ($compiler->template->compiled->has_nocache_code) {
133 $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
135 $output .= "/* {$_funcNameCaching} */\n";
136 $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
137 $output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
138 $output .= "ob_start();\n";
139 $output .= "\$_smarty_tpl->compiled->has_nocache_code = true;\n";
140 $output .= $_paramsCode;
141 $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}";
142 $output .= "\$params = var_export(\$params, true);\n";
143 $output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
144 $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
145 $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
146 $compiler->parser->current_buffer->append_subtree($compiler->parser,
147 new Smarty_Internal_ParseTree_Tag($compiler->parser,
149 $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
150 $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
151 $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
152 $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
153 $output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
155 $output .= "/*/ {$_funcName}_nocache */\n\n";
157 $compiler->parser->current_buffer->append_subtree($compiler->parser,
158 new Smarty_Internal_ParseTree_Tag($compiler->parser,
160 $_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser,
161 preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
162 array($this, 'removeNocache'),
163 $_functionCode->to_smarty_php($compiler->parser)));
165 $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
167 $output .= "/* {$_funcName} */\n";
168 $output .= "if (!function_exists('{$_funcName}')) {\n";
169 $output .= "function {$_funcName}(\$_smarty_tpl,\$params) {\n";
170 $output .= $_paramsCode;
171 $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}?>";
172 $compiler->parser->current_buffer->append_subtree($compiler->parser,
173 new Smarty_Internal_ParseTree_Tag($compiler->parser,
175 $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
176 $output = "<?php\n}}\n";
177 $output .= "/*/ {$_funcName} */\n\n";
179 $compiler->parser->current_buffer->append_subtree($compiler->parser,
180 new Smarty_Internal_ParseTree_Tag($compiler->parser,
182 $compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
183 // nocache plugins must be copied
184 if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
185 foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
186 foreach ($tmp as $type => $data) {
187 $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
192 // restore old buffer
194 $compiler->parser->current_buffer = $saved_data[ 1 ];
195 // restore old status
196 $compiler->template->compiled->has_nocache_code = $saved_data[ 2 ];
197 $compiler->template->caching = $saved_data[ 3 ];
202 * Remove nocache code
208 function removeNocache($match)
211 preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
213 $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);