Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
ADVANCED: Recursion with Smarty
Goto page Previous  1, 2, 3 ... 9, 10, 11
 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4596
Location: Hamburg / Germany

PostPosted: Wed Feb 17, 2010 5:41 pm    Post subject: Reply with quote

Alex

Thanks for your test case and I think I know where the problem is. I will look into optimizations for this.

Uwe
Back to top
View user's profile Send private message
dough boy
Smarty Rookie


Joined: 31 Mar 2006
Posts: 32

PostPosted: Sun Oct 03, 2010 10:34 pm    Post subject: Reply with quote

Sorry to be revisiting this, but it appears it no longer works with 3.0. Does anyone know the trick to get it working? If this is not possible then I guess I will revert back to old.

Code:

Notice: function call 'register_compiler_function' is unknown or deprecated. in /path/to/includes/smarty/libs/sysplugins/smarty_internal_wrapper.php on line 57

Notice: function call 'register_compiler_function' is unknown or deprecated. in /path/to/includes/smarty/libs/sysplugins/smarty_internal_wrapper.php on line 57

Notice: function call 'register_postfilter' is unknown or deprecated. in /path/to/includes/smarty/libs/sysplugins/smarty_internal_wrapper.php on line 57

Notice: function call '_parse_attrs' is unknown or deprecated. in /path/to/includes/smarty/libs/sysplugins/smarty_internal_wrapper.php on line 57

Fatal error: Uncaught exception 'Exception' with message 'unknown method '_parse_attrs'' in /path/to/includes/smarty/libs/sysplugins/smarty_internal_wrapper.php:117 Stack trace: #0 /path/to/includes/smarty/libs/Smarty.class.php(753): Smarty_Internal_Wrapper->convert('_parse_attrs', Array) #1 /path/to/includes/smarty/libs/plugins/compiler.defun.php(26): Smarty->__call('_parse_attrs', Array) #2 /path/to/includes/smarty/libs/plugins/compiler.defun.php(26): Smarty->_parse_attrs(Array) #3 /path/to/includes/smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php(179): smarty_compiler_defun(Array, Object(Smarty)) #4 /path/to/includes/smarty/libs/sysplugins/smarty_internal_templateparser.php(2154): Smarty_Internal_TemplateCompilerBase->compileTag('defun', Array) #5 /path/to/includes/smarty/libs/sysplugins/smarty_internal_templateparser.php(2536): Smarty_Internal_Templateparser->yy_r40() #6 / in /path/to/includes/smarty/libs/sysplugins/smarty_internal_wrapper.php on line 117
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4596
Location: Hamburg / Germany

PostPosted: Sun Oct 03, 2010 11:24 pm    Post subject: Reply with quote

Some custom plugins written for Smarty2 may not run with Smarty3 if they make use of Smarty2 internals.

This is the case for the 'defun' plugin once written for Smarty2.

Smarty3 has a new {function} tag which provide same functionallity.
You will find a brief decription in the README file.
Back to top
View user's profile Send private message
s0rr0w
Smarty Rookie


Joined: 27 Oct 2003
Posts: 7
Location: Kiev, Ukraine

PostPosted: Tue Mar 22, 2011 12:36 pm    Post subject: Reply with quote

After little modification you may use variables as fun name

Code:

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.'\'';

    if( preg_match( "/_tpl_vars/", $_func_name ) ) $_func = '\'smarty_fun_\'.'.$_func_name;

    $_params = 'array(';
    $_sep = '';
    unset($_attrs['name']);
    foreach ($_attrs as $_key=>$_value) {
        $_params .= "$_sep'$_key'=>$_value";
        $_sep = ',';
    }
    $_params .= ')';
    return "call_user_func( $_func, &\$this, $_params); ";

}

Usage
Code:

{defun name="foo"}foo{/defun}
{assign var="myFunc" value="foo"}
{fun name=$myFunc}
Back to top
View user's profile Send private message
seyfer
Smarty n00b


Joined: 23 May 2012
Posts: 3

PostPosted: Wed May 23, 2012 7:02 am    Post subject: Reply with quote

messju wrote:
transistor: this sounds challenging. i like challenges Smile

i don't know if this is overall good, as a little test i did 2 compiler-functions.

the pity of compiler-functions is, you cannot write reasonable ones without relying heavily on some of smarty's internals. this can send you easily to upgrade/maintenaince-hell Sad. anyway these should work with 2.4.2 and after for now.

define a function with {defun name="...."}...{/defun}
call a function with {fun name="..." param1=... param2=...}
(the defined function is called implicitely once after definition)

(edit: this should be in compiler.defun.php in your plugins_dir-path of course)

[php]<?php
function smarty_compiler_defun($tag_args, &$smarty) {
$_attrs = $smarty->_parse_attrs($tag_args);
$smarty->_tag_stack[] = array('defun', $_attrs, $tag_args);
if (!isset($_attrs['name'])) $smary->syntax_error("defun: missing name parameter");

$_func_name = $smarty->_dequote($_attrs['name']);
$_func = 'smarty_fun_'.$_func_name;
return "if (!function_exists('$_func')) { function $_func(&\$this, \$params) { \$_fun_tpl_vars = \$this->_tpl_vars; \$this->assign(\$params); ";

}


function smarty_compiler_defun_close($tag_args, &$smarty) {
list($_name, $_attrs, $_open_tag_args) = array_pop($smarty->_tag_stack);
if ($_name!='defun') $smarty->_syntax_error("unexpected {/defun}");
return " \$this->_tpl_vars = \$_fun_tpl_vars; }} ".smarty_compiler_fun($_open_tag_args, $smarty);
}


function smarty_compiler_fun($tag_args, &$smarty) {
$_attrs = $smarty->_parse_attrs($tag_args);

if (!isset($_attrs['name'])) $smarty->_syntax_error("fun: missing name parameter");
$_func_name = $smarty->_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('/defun', 'smarty_compiler_defun_close');
$this->register_compiler_function('fun', 'smarty_compiler_fun');

?>[/php]

your example would read then:
Code:
{defun name="testrecursion" list=$tree}
{foreach from=$list item=element}
<li>{$element.name}
   {if $element.element}
   <ul>{fun name="testrecursion" list=$element.element}</ul>
   {/if}
</li>
{/foreach}
{/defun}


i had to put one array(...) around your $tree, but then it worked Smile


This don't work with SMARTY 3+. Please, how can i fix it to work with SMARTY3+ ?
Help!
Back to top
View user's profile Send private message
seyfer
Smarty n00b


Joined: 23 May 2012
Posts: 3

PostPosted: Wed May 23, 2012 7:22 am    Post subject: Reply with quote

http://www.smarty.net/docs/en/language.function.function.tpl

there is solution with new tag.
Back to top
View user's profile Send private message
dupko
Smarty n00b


Joined: 06 Aug 2012
Posts: 1

PostPosted: Mon Aug 06, 2012 8:48 am    Post subject: Reply with quote

Thanks,new solution WORKS! Wink




_____________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page Previous  1, 2, 3 ... 9, 10, 11
Page 11 of 11

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP