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

Display array as nested list

 
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
OA_maarten
Smarty n00b


Joined: 16 Aug 2004
Posts: 2

PostPosted: Mon Aug 16, 2004 2:03 pm    Post subject: Display array as nested list Reply with quote

Hello,

I have some problems displaying an array as a nested list in HTML using Smarty.

The array has the following structure:

Code:
Array (42)
   0 => Array (5)
      categoryID => 6
      parentID => 0
      name => BOAG
      active => 1
      lvl => 0
   1 => Array (5)
      categoryID => 7
      parentID => 6
      name => vergaderschema
      active => 1
      lvl => 1
   2 => Array (5)
      categoryID => 8
      parentID => 6
      name => agenda
      active => 1
      lvl => 1
   3 => Array (5)
      categoryID => 48
      parentID => 6
      name => communicatiedag
      active => 1
      lvl => 2


and so on..

As you can see, an element contains a categoryID and a parentID. If the parentID equals 0, the element is a parent, else it's a child. For a better overview, there's also a field 'lvl'. This field gives more information about the level of the element. According to the array above, the structure should look like this:

Code:
0: BOAG
  1: vergaderschema
  1: agenda
    2: communicatiedag


In HTML, the list should look like this:

Code:
<ul>
   <li>BOAG
   <ul>
      <li>vergaderschema</li>
      <li>agenda
      <ul>
         <li>communicatiedag</li>
      </ul>
      </li>
   </ul>
</ul>

I have tried to display my results as an unordered list, but it won't work. The following code only displays the parent elements, this works fine.

Code:
<ul>
{section name=cList loop=$aCategory}
{if $aCategory[cList].lvl == 0}
<li>PARENT: {$aCategory[cList].name}</li>
{/if}
{/section}
</ul>


When I also want to display the elements with 'lvl' 1, it goes wrong. This Smart code:

Code:
<ul>
{section name=cList loop=$aCategory}
   {if $aCategory[cList].lvl == 0}
   <li>PARENT: {$aCategory[cList].name}
   <ul>
   {/if}
   {if $aCategory[cList].lvl == 1}
      {section name=lvl loop=$aCategory[cList].lvl}
      <li>CHILD: {$aCategory[cList].name}</li>
   {/section}
   {/if}
   </ul>
   </li>
{/section}
</ul>


results in something like this:

Code:
<ul>
   <li>PARENT: BOAG
   
   <ul>
   </ul>
   
   </li>
   <li>CHILD: vergaderschema</li>
</ul>
</li>
</ul>


Could anyone please take a look at my problem? I would be very thankful.

Thanks in advance,

Maarten Wierda
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 16, 2004 9:17 pm    Post subject: Reply with quote

Pass your data to Smarty in the order you want it displayed, and supply a depth parameter with each entry so you can indent them appropriately. I'd try to keep the recursive relations logic out of the templates if possible, otherwise there are other ideas that have been posted before, just do a search.
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: Mon Aug 16, 2004 9:35 pm    Post subject: Reply with quote

Interestingly, this data does not have nested sets. Further, the lvl is pre-computed and passed to the template, so things should work nicely with a simple linear-loop. You can use simple divs instead of html lists that means a slightly simpler layout since you can rely on CSS to make the spacing:
Code:
<div>
{section name=cList loop=$aCategory}
    <div style="padding-left: {$aCategory[cList].lvl*4};">
       {$aCategory[cList].name}
   </div>
{/section}
</div>

If you do want actual contained children (instead of a layout fake like above) then you will likely need a recursive function as Mohrt indicated. See: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=291
Back to top
View user's profile Send private message
OA_maarten
Smarty n00b


Joined: 16 Aug 2004
Posts: 2

PostPosted: Tue Aug 17, 2004 7:48 am    Post subject: Reply with quote

boots wrote:

If you do want actual contained children (instead of a layout fake like above) then you will likely need a recursive function as Mohrt indicated. See: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=291


Pfew, looks pretty hard to me ;]

And you're right, I do want to use a HTML-list instead of using DIV's because I want my webpages to be semantic correct.

BTW the example on the other topic you gave me doesn't use a correct list.

Example of a correct nested list:

Code:

<ul>
   <li>item 1</li>
   <li>item 2
   <ul>
      <li>item 2a</li>
      <li>item 2b</li>
   </ul>
   </li>
</ul>


A wrong nested list:

Code:

<ul>
   <li>item 1</li>
   <li>item 2</li>
   <ul>
      <li>item 2a</li>
      <li>item 2b</li>
   </ul>
</ul>


Nothing much, but I thought it could be interesting.

I will take a look at the thread you gave me and if that doesn't work I'll try to change the structure of the array, as mohrts suggested.

Thanks!
Back to top
View user's profile Send private message Send e-mail
dke
Smarty n00b


Joined: 28 Feb 2019
Posts: 2

PostPosted: Thu Feb 28, 2019 9:41 am    Post subject: Reply with quote

Have someone found a solution for that issue ? I want to do the exact same thing.

Edit : well it's fine I've found a way to do that Smile it's not very "clean" and there is obviously something better to do (for exemple here it works only for a fixed number of levels) but it works for me and what I have to do.
If that can help someone :

Code:

<ul>
{foreach $list as $p => $page}   
    <li>item
    {if !$page@last}
    {if $list[$p+1].lvl> $page.lvl}
        <ul>
    {elseif $liste_pages[$p+1].lvl< $page.lvl}
        {math equation="y - x" x=$list[$p+1].lvl y=$page.lvl assign=dif}
        {if $dif == 1}
                </li>
            </ul>
        </li>
        {elseif $dif == 2}
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        {elseif $dif == 3}
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        {elseif $dif == 4}
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        {/if} 
    {/if}
    {/if}
{/foreach}
</ul>
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Fri Mar 01, 2019 3:42 am    Post subject: Reply with quote

Do you have access to, and can edit the source PHP code that populates the Smarty variables?

If so, you can build your $list with child branches. Then pass the whole tree to Smarty.
Back to top
View user's profile Send private message
dke
Smarty n00b


Joined: 28 Feb 2019
Posts: 2

PostPosted: Fri Mar 01, 2019 8:28 am    Post subject: Reply with quote

bsmither wrote:
Do you have access to, and can edit the source PHP code that populates the Smarty variables?

If so, you can build your $list with child branches. Then pass the whole tree to Smarty.


Sadly not, otherwise it would be better for sure.
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