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

Displaying an array recursively in smarty

 
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 -> General
View previous topic :: View next topic  
Author Message
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Tue Jul 01, 2003 3:43 pm    Post subject: Displaying an array recursively in smarty Reply with quote

I need to display an array that can hold infinite number of children arrays, such as:

Code:

array(
   array(
      array()
      array()
      array()
   )
   array(
      array(
         array()
       )
      array()
   )
)


The values are irrelevent, but you could imagine values like these:
Code:

array("page_id" => 1, "page_name" => "hello world");


Ok, now you got the basics, but how do I display this array in Smarty?? Neutral

All I want to loop is the first level of the array, and I want to "call" a function/method recursively for each children array, so you can have infinite number of children arrays.

This is easy in php, I just create a function to do this, but in smarty this evades me.

HELP! Very Happy
Back to top
View user's profile Send private message
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Wed Jul 02, 2003 9:15 am    Post subject: I managed to find a solution Reply with quote

Here is the solution if anyone is interested:

index.tpl
Code:

<ol>
{foreach from=$tree key=q item=a}
   <li>{$a.page_name}</li>

   {if sizeof($a.Childrens) > 0}
      {include file="sub.tpl" pages=$a.Childrens}
   {/if}
{/foreach}
</ol>



sub.tpl
Code:

<ol>
{foreach from=$pages key=w item=s}
   <li>{$s.page_name}</li>

   {if sizeof($s.Childrens) > 0}
      {include file="sub.tpl" pages=$s.Childrens}
   {/if}
{/foreach}
</ol>



Very simple and neat, might create an include overhead, but what doesn't!? Smile This does the job.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Jul 02, 2003 5:18 pm    Post subject: Reply with quote

appel, there is a nice thread on recursion / advanced looping in the Tips & Tricks forum. Your solution is perfectly valid but you will find a much faster and possibly neater solution there Smile

Greets
Back to top
View user's profile Send private message
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Wed Jul 02, 2003 5:58 pm    Post subject: Reply with quote

boots wrote:
appel, there is a nice thread on recursion / advanced looping in the Tips & Tricks forum. Your solution is perfectly valid but you will find a much faster and possibly neater solution there Smile

Greets


Yea, thanks, that compiler.defun.php did the trick.
Back to top
View user's profile Send private message
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 -> General All times are GMT
Page 1 of 1

 
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