Re: printing tree structures with smarty
| From: | Linus McCabe | Date: | Wed, 02 Jan 2002 20:40:31 +0000 |
| Subject: | Re: printing tree structures with smarty | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-2476@lists.php.net to get a copy of this message | ||
I've done something like that, this is part of the template:
<select name="category_parent_id">
<%section name=category loop=$categories %>
<option value="<%$categories[category].category_id%>"
<%if $categories[category].category_id == $db[0].category_parent_id %>
SELECTED
<%/if%> >
<%section name=spc loop=$categories[category].level %>
<%if not %spc.first% %>
<%if %spc.last% %>
<%if $categories[category].last[spc.index] %>
'-
<% else %>
|-
<%/if %>
<% else %>
<%if $categories[category].last[spc.index] %>
<% else %>
|
<%/if %>
<%/if%>
<%/if%>
<%/section%>
<%$categories[category].category_name%>
</option>
<%/section%>
</select>
Result:
<select name="category_parent_id">
<option value="0" SELECTED > </option>
<option value="2">Internal</option>
<option value="2004"> Places</option> <option value="2005" > |- NYC </option>
<option value="2006" > |- Sweden </option>
<option value="2007"> '- Ireland </option>
<option value="2001"> Nature</option>
<option value="2008"> |- Sunsets </option>
<option value="2009" > '- Animals </option>
<option value="2002" > BW</option>
<option value="2003"> People </option>
<option value="2010" > Artist</option>
</select>
The part within the '<%section name=spc loop=$categories[category].level %>' section could just ba a simple
But I wanted the lines as well...
/linus
Monte Ohrt wrote:
What I'm proposing is not to complicate the template with recursion/relation logic. The template should only care about the displaying of the data, not its relation in a tree. So, think about how you want to display the data, maybe something like this: item1 item1.1 item1.2 item2 item2.1 item2.1.1 item2.1.2 item2.2 item2.2.1 item3 So the template engine basically needs to know three things about each item: the order, the depth, and the content. The order is easy, you just assign the items in the order you want them displayed, then when you go through a section loop it will already be in the correct order. Then you use the item depth to determine how far over the current item is indented, and the item content is what is printed. If you want to make it work with ordered lists, you might want to pass another piece of information like when to open/close your tags, but you get the idea. Monte Manuel Holtgrewe wrote:Monte Ohrt wrote:That wouldn't be the function of the template engine would it?Okay...Loop through your values when you assign them, and also assign the info the template needs to display them like the current depth, etc.Sorry, I have absolutely no idea what you mean by that. Could you give me an example or an URL with examples, please ? Manuel -- what privilege do we have under the sun, that gives us the right to the throne ? In Flames - "Coerced Coexistence" --- You are currently subscribed to smarty as: monte@ispi.net To unsubscribe send a blank email to $subst('Email.Unsub') Visit Smarty's homepage at http://www.phpinsider.com/php/code/Smarty/-- Monte Ohrt <monte@ispi.net> http://www.ispi.net/ --- You are currently subscribed to smarty as: linus@mccabe.nu To unsubscribe send a blank email to $subst('Email.Unsub') Visit Smarty's homepage at http://www.phpinsider.com/php/code/Smarty/