array(array("name" => "test1","id" => "1",
// "element" => array(array("name" => "test1.1","id" => "2"),
// array("name" => "test1.2","id" => "3",
// "element" => array(array("name" => "test1.2.1","id" => "4"),
// array("name" => "test1.2.2","id" => "5")))))));
//
//
// {defun name="recursion" list=$element}
//
// {if isset($element.name)}- {$element.name}
{/if}
// {if isset($element.element)}
// {foreach from=$element.element item=element}
// {fun name="recursion" list=$element}
// {/foreach}
// {/if}
//
// {/defun}
//
//
//
/* create code for a function call */
function smarty_compiler_fun($tag_args, &$compiler) {
$_attrs = $compiler->_parse_attrs($tag_args);
if (!isset($_attrs['name'])) $compiler->_syntax_error("fun: missing name parameter");
$_func_name = $compiler->_dequote($_attrs['name']);
$_func = 'smarty_fun_'.$_func_name;
$_params = 'array(';
$_sep = '';
unset($_attrs['name']);
foreach ($_attrs as $_key=>$_value) {
$_params .= "$_sep'$_key'=>$_value";
$_sep = ',';
}
$_params .= ')';
return "$_func(\$this, $_params); ";
}
$this->register_compiler_function('fun', 'smarty_compiler_fun');
/* create code for a function declaration */
function smarty_compiler_defun($tag_args, &$compiler) {
$attrs = $compiler->_parse_attrs($tag_args);
$func_key = '"' . md5('php-5') . '[[' . md5(uniqid('sucks')) . '";';
array_push($compiler->_tag_stack, array('defun', $attrs, $tag_args, $func_key));
if (!isset($attrs['name'])) $compiler->_syntax_error("defun: missing name parameter");
$func_name = $compiler->_dequote($attrs['name']);
$func = 'smarty_fun_'.$func_name;
return $func_key . "if (!function_exists('$func')) { function $func(&\$this, \$params) { \$_fun_tpl_vars = \$this->_tpl_vars; \$this->assign(\$params); ";
}
/* create code for closing a function definition and calling said function */
function smarty_compiler_defun_close($tag_args, &$compiler) {
list($name, $attrs, $open_tag_args, $func_key) = array_pop($compiler->_tag_stack);
if ($name!='defun') $compiler->_syntax_error("unexpected {/defun}");
return " \$this->_tpl_vars = \$_fun_tpl_vars; }} " . $func_key . smarty_compiler_fun($open_tag_args, $compiler);
}
$this->register_compiler_function('/defun', 'smarty_compiler_defun_close');
/* callback to replace all $this with $smarty */
function smarty_replace_fun($match) {
$tokens = token_get_all('=5.0) {
/* filter sourcecode. look for func_keys and replace all $this
in-between with $smarty */
while (1) {
$new_source = preg_replace_callback('/' . $search . '(.*)\\1/Us', 'smarty_replace_fun', $source);
if (strcmp($new_source, $source)==0) break;
$source = $new_source;
}
} else {
/* remove func_keys */
$source = preg_replace('/' . $search . '/', '', $source);
}
return $source;
}
$this->register_postfilter('smarty_postfilter_defun');
?>