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
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
On Dec 11, 2003, at 11:01 AM, Justin Johnson wrote:
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
Yes, this works too, and really shows off how stan expressions can shorten complex HTML generation code. By the way, you don't need the \ character to continue the line there, because python will allow you to have linebreaks while there is an outstanding brace that hasn't been closed. (), {}, and [] dp
On Dec 11, 2003, at 10:35 AM, Justin Johnson wrote:
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.
First, let me start by saying that nevow is all about flexibility. You have an implementation of a function which does exactly what you want, except it uses string concatenation instead of stan to generate some HTML. If it is going to be easier for you to debug and understand this code, then by all means use it. I have shown an example of using your string-concatenation code as a nevow renderer, by calling your code and wrapping the output in an xml object, before returning it. Second, stan doesn't have to be about huge python expressions. It's just as easy to use in a standard procedural way, creating objects and then appending them to lists. I have constructed an example of rendering your menu structure using stan with clarity and readability in mind. dp
participants (2)
-
Donovan Preston
-
Justin Johnson