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

Recursive array

 
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
6 ANUR
Smarty Rookie


Joined: 06 May 2003
Posts: 23
Location: Lille, France

PostPosted: Tue Aug 12, 2003 9:27 pm    Post subject: Recursive array Reply with quote

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. Confused

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
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Tue Aug 12, 2003 10:44 pm    Post subject: Reply with quote

This thread is probably what you are looking for.

Good-luck!
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Aug 12, 2003 10:51 pm    Post subject: Reply with quote

@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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


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

PostPosted: Tue Aug 12, 2003 11:13 pm    Post subject: Reply with quote

sorry--I missed that. My bad!

I suppose I should try slowing down a bit Smile
Back to top
View user's profile Send private message
6 ANUR
Smarty Rookie


Joined: 06 May 2003
Posts: 23
Location: Lille, France

PostPosted: Tue Aug 12, 2003 11:22 pm    Post subject: Reply with quote

@Boots: Thanks but as Messju said I already checked that thread Smile

@ 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 Wink
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
View user's profile Send private message Visit poster's website
messju
Administrator


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

PostPosted: Wed Aug 13, 2003 7:16 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail 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 -> 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