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

Another Method of building Trees

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
sage
Smarty Rookie


Joined: 09 May 2004
Posts: 20
Location: London, UK

PostPosted: Sun May 09, 2004 10:51 pm    Post subject: Another Method of building Trees Reply with quote

This was a method to create an list based tree that I came up with for my site (before reading anything on this forum about recursion in smarty Confused )

In the php script, the item data is transformed into a1D(ish) array, of the items as you would read them on the screen with strings to identify the begining and ending of blocks.

eg. For a list like this:

-level1_a
--- level2_a
--- level2_b
--- level2_c
-level1_b
--- level2_d
--- level2_e

would become:

[php:1:9cc6ba3715]<?php
array (
'SECTION',
'level1_a',
'SECTION',
'level2_a',
'level2_b',
'level2_c',
'ENDSECTION',
'level1_b',
'SECTION',
'level2_d',
'level2_e',
'ENDSECTION',
'ENDSECTION'
);
?>[/php:1:9cc6ba3715]

The array is assigned to smarty and is processed in a foreach loop with capture to allow the correct nesting of the items:

Code:

{foreach from=$section_data item=section_item}
{if $section_item=='SECTION'}
   <ul>{capture name=closing} {/capture}
{elseif $section_item=='ENDSECTION'}
   </li></ul>
{else}
   {$smarty.capture.closing}
        <li>{$section_item}
   {capture name=closing}</li>{/capture}
{/if}
{/foreach}


Code:

{foreach from=$section_data item=section_item}
{if $section_item=='SECTION'}
   <ul>{capture name=closing} {/capture}

For a new section a new list is required, capture is called to overwrite any already captured </li> items with whitespace.

Code:

{elseif $section_item=='ENDSECTION'}
   </li></ul>

For the end of the subsection the item end tag (as it has been captured) and the list are closed.

Code:

{else}
   {$smarty.capture.closing}
        <li>{$section_item}
   {capture name=closing}</li>{/capture}
{/if}

For the items, first any captured closing tags are put in for the previous item, then the new item is written. The closing list item is captured, just incase a new subsection is then required.

The output is HTML/XHTML complient, and works for any depth tree.

In my actual case the array passed to smarty is a mixture of the section deliminators as well as arrays for the list items, containing the name, and linkage data.
Back to top
View user's profile Send private message
BlackYeLL
Smarty n00b


Joined: 27 Oct 2006
Posts: 1

PostPosted: Fri Oct 27, 2006 1:36 pm    Post subject: Reply with quote

Wow, thank you for this ! Exactly what I want Smile And it works fine !

Thanks !
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 -> Tips and Tricks 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