Aight, basically, I have a database that looks like this:
id | parentid | type | name
---------------------------------------------
1 | 0 | heading | this is my heading
---------------------------------------------
2 | 1 | child | this is one of many child elements
I'm using Mako to go through that list of data and create nested UL's. It seems to work great... But I'm not sure if my code could be improved.
Here it is:
<%def name="getCategories(parentID)">
## This function creates the children LI for the main headings
## Parameters:
## parentID - Integer - ID of the heading you want to get the children for
<%
categories = [x for x in c.headings if x.type == 'category' and x.parentid == parentID]
categories.sort(key=lambda x: x.name)
%>
%for category in categories:
<li>
<ul>
<li>${category.name.title()}</li>
${getSubCategories(category.id)}
</ul>
</li>
%endfor
<%def name="getSubCategories(parentID)">
## Get the subcategories for the parent category
##
## This could eventually turn into a recursive function to get further subcategories
## which is why it has it's own function rather than just having another for loop above
##
## Parameters: Same as above
<%
categories = [x for x in c.headings if x.type == 'category' and x.parentid == parentID]
categories.sort(key=lambda x: x.name)
%>
%for category in categories:
<ul>
<li>${category.name.title()}</li>
</ul>
%endfor
</%def>
</%def>
If you're wondering why I have two functions that produce the same output, it's because they don't. The nested UL's produced from getSubCategories() are styled differently (HTML) than those produced from getCategories().
Is this slow? Will it die under a heavy load? Could it be improved?
I'd appreciate your advice. Cheers.