Page 1 of 1
Unlimited Nesting using Template-Lite
Posted: Sun Jun 04, 2006 8:31 pm
by Sphenn
Hi,
I'm trying to figure out a way to use Template-lite to display nested categories, however, there is no preset number of sub-categories. My code looks like this:
Code: Select all
//DB call here
while ( $row = mysql_fetch_assoc($result) )
{
if ( empty($row['child']) )
{
$catInfo[$row['id']]['name'] = $row['name'];
// etc for all the properties
}
else
{
$catInfo[$row['parent_id']][$row['id']]['name'] = $row['name'];
// etc
}
}
I'm unsure how to proceed from here. I don't know how to extend this to support unlimited sub-categories. As well, I'm not sure how I would template it.
Any thoughts or comments would be appreciated.
Thanks,
Sphenn
Posted: Mon Jun 05, 2006 2:06 am
by RobertGonzalez
The easiest thing to do would be to use the { section } taf in conjunction with the { if } tag within the template file (.tpl) if you are using Template Lite.
The beauty of the TL App is that you can leave the presentation logic up to TL. I just did this a few weeks ago with a two nest array read and it does with no problems.
Here is a brief snippet of a few different types of loops I am doing with a TL tpl file...
Code: Select all
{foreach key=di value=div from=$divisions}
<div class="relatedLinks">
<h3>{ $div } Division</h3>
<ul>
{section name=ti loop=$teams}
{if $teams[ti].team_division == $di}
<li><a href="{$u_teams}/{ $teams[ti].team_domain_short }/" title="Get more information on the { $teams[ti].team_name }">{ $teams[ti].team_name }</a></li>
{/if}
{/section}
</ul>
</div>
{/foreach}
<div id="linkBox">
<div class="relatedLinks">
<h3>Stay Up To Speed</h3>
<ul>
{section name=sidenav loop=$sidebar_links}
{ if $templatelite.section.sidenav.index lt $trigger_sidebar_strong }
<li class="highlight"><a href="{$sidebar_links[sidenav].link_url}" title="{$sidebar_links[sidenav].link_text}">{$sidebar_links[sidenav].link_text}</a></li>
{ else }
<li><a href="{$sidebar_links[sidenav].link_url}" title="{$sidebar_links[sidenav].link_text}">{$sidebar_links[sidenav].link_text}</a></li>
{ /if }
{/section}
</ul>
</div>
</div>
Re: Unlimited Nesting using Template-Lite
Posted: Mon Jun 05, 2006 5:19 am
by Weirdan
Sphenn wrote:
I'm unsure how to proceed from here. I don't know how to extend this to support unlimited sub-categories. As well, I'm not sure how I would template it.
Any thoughts or comments would be appreciated.
I would represent it with an array with the following structure:
Code: Select all
array(
array("item_name1", "nesting_level1"),
array("item_name2", "nesting_level2"),
// ....
array("item_nameN", "nesting_levelN"),
);
and then template it with simple {section} loop. The problem here is that you need to build ordered and 'indented' list outside the template.
Posted: Mon Jun 05, 2006 8:20 am
by RobertGonzalez
I think everything you want to do can be accomplished within the template. You can use the{ section } or the { foreach } tag to loop an array, then, inside of that loop, do an { if } to see if there is anything else to loop through for each array and level you go through.
To speed things up a bit you could use a counter within the code to tell you how may nests to make and then use that as a var in the template for generating the needed number of nests.