Working with arrays recursively

View: New views
2 Messages — Rating Filter:   Alert me  

Working with arrays recursively

by Christoph Boget-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I know you can set up nested {foreach} blocks in the template but that only
works if you know that the array you are working with has a predefined
number of levels.  Is there a way to set up a block so that you can work
with the array recursively?  So no matter how many levels the array has,
your smarty block can handle and display all the data?

thnx,
Christoph

Parent Message unknown Re: Working with arrays recursively

by Ken Snyder :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Christoph Boget wrote:

>> To recurse, you must include a template that includes itself to
>> trek deeper into the array.
>>    
>
> Do you have an example?  How do you keep the variables from overwriting
> one another?
>
> thnx,
> Christoph
>  
parameters can be assigned to the include file using {include} to avoid
overwriting--example below. - ken


main.tpl:

{include file="recurse.tpl" parents=$tree level=0}


recurse.tpl

{foreach from=$parents item=child}
  {$child.name}
  {if $child.children}
    {include file="recurse.tpl" parents=$child.children level=$level+1}
  {/if}
{/if}