Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

ADVANCED: Recursion with Smarty
Goto page Previous  1, 2, 3 ... 7, 8, 9, 10, 11  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Mar 01, 2006 6:19 pm    Post subject: Reply with quote

majglow wrote:
messju wrote:
I updated the pluging at http://lammfellpuschen.de/compiler.defun/ and added a more robust implementation of removing the trailing '<?php '. this version should work around the tokenizer bug.

please try that and tell me if it works for you.


This link isn't working anymore. What is the latest status of this function? I am on PHP5 & the latest smarty.


Yes it is. Messju's site was probably down when you tried. Try again Wink
Back to top
View user's profile Send private message
yusufdestina
Smarty Rookie


Joined: 07 Apr 2005
Posts: 23

PostPosted: Sat May 13, 2006 12:58 am    Post subject: Reply with quote

Fatal error: Using $this when not in object context in C:...compiler.defun.php on line 21

How can I fix this error? I'm using php5 and the latest smarty. tnx!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat May 13, 2006 1:56 pm    Post subject: Reply with quote

yusufdestina: don't require the file explicitly, let smarty autoload it. then it should be required in a class context and the $this in line 21 will be valid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Fri Jul 07, 2006 6:35 am    Post subject: Reply with quote

Hi,

I have built a multdimensional array ($tree) for which this function you have developed seems ideal.

I have added the compiler.defun.php file to /libs/plugins/

But while trying the following script as a test (drawing from boots' template allowing tracking of levels supplied earlier), I get an internal server error (unless I take out the "fun" call).

{defun name="menu" list=$tree level=0}
{assign var=level value=$level+1}
<div style="padding-left:{$level*2};">my data</div>
{fun name="menu" list=$i level=$level}
{/defun}

Do you have any idea what's going on?

thanks much in advance,
Brett

p.s. I'm working with PHP 4.4.1 and Smarty 2.6.14 (and other templates not calling this function are ok)
p.s.s. If you need to see the array data, it is listed in the middle of the page at http://bahai-library.com/browser/test-browse-18/recurs.php
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Jul 07, 2006 2:57 pm    Post subject: Reply with quote

Don't forget to bail out of the recursion. You have to test $list (btw: where is $i coming from?) to see if it is an array. If it isn't, you shouldn't be recursing back into the menu function.
Back to top
View user's profile Send private message
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Sat Jul 08, 2006 5:17 am    Post subject: Reply with quote

Oops...forget to esesescape recursion... Smile

So, this function is not just for cycling through a multi-dimensional array in its entirety? It is for any kind of recursion?

I don't know where $i is coming from. You had it listed in your example at http://www.phpinsider.com/smarty-forum/viewtopic.php?t=291&postdays=0&postorder=asc&start=45#9462

thanks,
Brett
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Jul 08, 2006 7:22 am    Post subject: Reply with quote

@brettz9: recursion (especially in Smarty) is at least an intermediate technique -- not suited for cut & paste. Try reading the thread over again and see what you come up with Wink
Back to top
View user's profile Send private message
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Sat Jul 08, 2006 7:44 am    Post subject: Reply with quote

Yeah, I know.

I actually figured it out now. I just thought in the beginning that this was something to automatically handle a multi-dimensional array till its end, and didn't want to try to unravel the coding of the plugin itself before I tested to make sure a simple run would work. Now that I have looked over the posts more carefully, successfully tested it, and understand it, I see it is not necessary to understand the internals of the plugin.

Thank you guys by the way so much for coming up with this (including your level tracking addition). After I had developed the array (and had recently switched to Smarty), I was despairing that Smarty might not be sophisticated enough to handle this kind of data. I guess I was wrong. It's especially cool that it can take any number of arguments...

By the way, if you ever package this standardly, I put in my vote for your {macro} and {macro_call} names (or what about {recurs} and {recurs_call} if somebody didn't suggest that already).

take care,
Brett
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Jul 08, 2006 8:30 am    Post subject: Reply with quote

brettz9 wrote:
{macro} and {macro_call} names


Or even, {func} and {call} ... but I think that maybe messju is reveling in his LISPy glory Smile
Back to top
View user's profile Send private message
smak
Smarty Rookie


Joined: 15 Jun 2006
Posts: 5

PostPosted: Tue Jul 11, 2006 1:19 pm    Post subject: Assigning variables Reply with quote

Variables assigned within a {defun} aren't asigned to the whole template.

This won't work for example:
[php:1:b4385c7827]<ul>
{defun name="tree" list=$tree lists=1}
{foreach from=$list item=element}
<li>
{$element.name}
{if $element.element}
{assign var=lists value=$lists+1}
<ul>{fun name="tree" list=$element.element lists=$lists}</ul>
{/if}
</li>
{/foreach}
{/defun}
</ul>

Number of lists: {$lists}[/php:1:b4385c7827]

Is there a workaround for this? Because I need to know the number of lists.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jul 11, 2006 3:17 pm    Post subject: Re: Assigning variables Reply with quote

smak wrote:
Is there a workaround for this? Because I need to know the number of lists.


Well, maybe consider writing a proper plugin. I don't think that these template functions were ever intended to return anything or have side-effects other than outputing some text.

...however, as a hacky, hacky workaround, you can define a parameter that only returns your value of interest:

Code:
{defun name=foo lists=1 return_data=false}
    {if $return_data}
        {$lists}
    {else}
        foo
    {/if}
{/defun}

{fun name=foo}
{fun name=foo return_data=true}


yuck.
Back to top
View user's profile Send private message
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Tue Jul 11, 2006 5:44 pm    Post subject: Reply with quote

I think this is ok, no? At least it can be done...

[php:1:0c2a5a8ed0]<ul>
{defun name="tree" list=$tree lists=1}
{foreach from=$list item=element}
<li>
{$element.name}
{if $element.element}
{assign var=lists value=$lists+1}
<ul>{fun name="tree" list=$element.element lists=$lists}</ul>
{/if}
</li>
{/foreach}
{capture name=num_lists}
{$lists}
{/capture}
{/defun}
</ul>

Number of lists: {$smarty.capture.num_lists}[/php:1:0c2a5a8ed0]
Back to top
View user's profile Send private message
m-lee
Smarty Rookie


Joined: 17 Mar 2004
Posts: 31

PostPosted: Tue Jul 25, 2006 3:16 pm    Post subject: problem with an index / id Reply with quote

First of, great plugin, thanks!

All works as accepted but im still have a little problem.
Im looping throug a directory structure and need to set an unique numeric autoincrement index for each file.

Is there any way to do it? I failed so far by trying to pass variables and using section iteration.

The node_id must be unique.

Thank you for your help.
Regards m-lee

[php:1:ef7e2afbd2]
{defun name="dirmenu" list=$ARRAY_DIRS level=0 node_id=$nodie_id previous_node_id=$previous_node_id}
{assign var=level value=$level+1}

{foreach from=$list item=o_menu name=co}
<li>

{assign var=node_id value=$node_id+1}

{if $o_menu.name != "system_cache" && $o_menu.name != "system_compile"}

node_id: {$node_id}
previous_node_id: {$previous_node_id}

{if $o_menu.kind == "file"}
<a href="javascript:SelectFile('{$o_menu.name}');">{$o_menu.name}</a>
{else}
{$o_menu.name}
{/if}

{/if}
{if $o_menu.content && $level <=3}
<ul>{fun name="dirmenu" list=$o_menu.content level=$level node_id=$node_id previous_node_id=$node_id}</ul>
{/if}

</li>
{/foreach}
{/defun} [/php:1:ef7e2afbd2]
Back to top
View user's profile Send private message
restak
Smarty Rookie


Joined: 10 Aug 2006
Posts: 14

PostPosted: Fri Aug 11, 2006 9:29 am    Post subject: Reply with quote

I have a serious problem with the defun-plugin if the template is created out of a function. see here
Back to top
View user's profile Send private message
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Sun Sep 24, 2006 10:11 pm    Post subject: Reply with quote

Many many thanks for this great work !
I dont know exactly how it works...but it does Very Happy
and nice fast Smile
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page Previous  1, 2, 3 ... 7, 8, 9, 10, 11  Next
Page 8 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