|
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 |
6 ANUR Smarty Rookie
Joined: 06 May 2003 Posts: 23 Location: Lille, France
|
Posted: Tue Aug 12, 2003 9:27 pm Post subject: Recursive array |
|
|
Hello,
I am trying to figure out how to render a recursive array with Smarty.
I have searched the forums for a clue and I found this thread but it did not help me enough.
What I have is an array of this kind (This can be slightly changed if needed):
Code: |
$tree = array(array('name' => 'Level 1',
'url' => 'level_1.php'
),
array('name' => 'Level 2',
'url' => 'level_2.php',
'sub_level' => array(array('name' => 'Level 2.1',
'url' => 'level_2_1.php'
),
array('name' => 'Level 2.2',
'url' => 'level_2_2.php',
'sub_level' => array(array('name' => 'Level 2.2.1',
'url' => 'level_2_2_1.php'
),
array('name' => 'Level 2.2.2',
'url' => 'level_2_2_2.php'
)
)
)
)
)
);
$tpl->assign('tree', $tree);
|
Sorry for the messy array but I could not make it better looking.
I would like to get something like that after processing :
Code: |
<ul>
<li>
<a href="level_1.php">Level 1</a>
</li>
<li>
<a href="level_2.php">Level 2</a>
<ul>
<li>
<a href="level_2_1.php">Level 2.1</a>
</li>
<li>
<a href="level_2_2.php">Level 2.2</a>
<ul>
<li>
<a href="level_2_1_1.php">Level 2.2.1</a>
</li>
<li>
<a href="level_2_1_2.php">Level 2.2.2</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
|
Does anybody has some clue on how to achieve this ?
Thanks a lot. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 12, 2003 10:44 pm Post subject: |
|
|
This thread is probably what you are looking for.
Good-luck! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Aug 12, 2003 10:51 pm Post subject: |
|
|
@boots: he already said in his post this didn't help him enough.
@6 ANUR: the most simple way to display a structure like yours is recursively including a template:
index.tpl:
Code: | {include file="tree.tpl" item=$tree} |
tree.tpl:
Code: | {if $item}
<ul>
{foreach from=$item item=sub_item}
<li>
<a href="{$sub_item.url}">{$sub_item.name}</a>
{include file="recurse/tree.tpl" item=$sub_item.sub_level}
</li>
{/foreach}
</ul>
{/if}
|
but this is slow with deep and big arrays. if you use smarty's caching features this is neglible if not: which part of Advanced: Recursion with Smarty is it you need help with? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 12, 2003 11:13 pm Post subject: |
|
|
sorry--I missed that. My bad!
I suppose I should try slowing down a bit |
|
Back to top |
|
6 ANUR Smarty Rookie
Joined: 06 May 2003 Posts: 23 Location: Lille, France
|
Posted: Tue Aug 12, 2003 11:22 pm Post subject: |
|
|
@Boots: Thanks but as Messju said I already checked that thread
@ Messju: Thanks for the answer, it works great.
I plan to use the caching features of Smarty and my array is quite small so it should be ok.
BTW I still have a little problem : when the level does not have a sub level then I have a notice because the array value does not exists.
I tried to change the code to this to avoid the problem but it does not work as expected ... in fact it does not work at all
Code: |
{if $item}
<ul>
{foreach from=$item item=sub_item}
<li>
<a href="{$sub_item.url}">{$sub_item.name}</a>
{if $sub_item.sub_level}
{include file="tree.tpl" item=$sub_item.sub_level}
{/if}
</li>
{/foreach}
</ul>
{/if}
|
Regarding the recursivity with Smarty in fact I did not have a clue on how to achieve this. I know how to walk an array which I know the size but not any array with any depth. Maybe you have some clue regarding this. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Aug 13, 2003 7:16 am Post subject: |
|
|
hmm, the code you posted works fine here.
regardings recursive structures and smarty: smarty is not made for that natively. i don't know better ways handling them than like above or in the other thread mentionend by you. |
|
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
|