|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
appel Smarty Rookie
Joined: 27 May 2003 Posts: 29
|
Posted: Tue Jul 01, 2003 3:43 pm Post subject: Displaying an array recursively in smarty |
|
|
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??
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! |
|
Back to top |
|
appel Smarty Rookie
Joined: 27 May 2003 Posts: 29
|
Posted: Wed Jul 02, 2003 9:15 am Post subject: I managed to find a solution |
|
|
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!? This does the job. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jul 02, 2003 5:18 pm Post subject: |
|
|
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
Greets |
|
Back to top |
|
appel Smarty Rookie
Joined: 27 May 2003 Posts: 29
|
Posted: Wed Jul 02, 2003 5:58 pm Post subject: |
|
|
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
Greets |
Yea, thanks, that compiler.defun.php did the trick. |
|
Back to top |
|
|
|
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
|