[Twisted-web] Menu flattening
Justin Johnson
twisted-web@twistedmatrix.com
Thu, 11 Dec 2003 10:01:11 -0600
This seems to do what I want. I should probably rethink this though,
since dict's aren't ordered.
def flatten_menu(menu_items):
if isinstance(menu_items, dict):
html_menu = ul[
[ (li[k], [flatten_menu(i) for i in menu_items[k]]) \
for k in menu_items.keys() if k]
]
else:
html_menu = ul[ li[menu_items] ]
return html_menu
On Thu, 11 Dec 2003 09:35:48 -0600, "Justin Johnson"
<justinjohnson@fastmail.fm> said:
> Hi,
>
> I'm trying to write a method that creates a menu given the following
> information.
>
> menu_items = {
> "A": [ "a", "b", "c" ],
> "B": [ "d", "e", "f" ],
> "C": [ "g", "h", {"i": ["x", "y", "z"]} ],
> }
>
> This dict contains the hierarchy of menus. Eventually I'll add another
> list as input that indicates what is currently selected so the menus will
> expand and collapse as appropriate.
>
> selected_menu_items = ["C","i"]
>
> I wrote the following method which displays s simple unordered list from
> the dictionary.
>
> def flatten_menu(menu_items_dict):
> html_menu = "<ul>"
> for k in menu_items_dict.keys():
> html_menu += "<li>%s</li>" % k
> html_menu += "<ul>"
> for i in menu_items_dict[k]:
> if isinstance(i, dict):
> html_menu += flatten_menu(i)
> else:
> html_menu += "<li>%s</li>" % i
> html_menu += "</ul>"
> html_menu += "</ul>"
> return html_menu
>
> Now I'm trying to implement it as a stan tree. But since I can't
> separate the starting and ending tags in stan I'm not sure how to
> implement this. This seems a bit too complex for list comprehension.
>
> Any ideas?
>
> Thanks.
> -Justin
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web@twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web