[Twisted-web] Menu flattening

Justin Johnson twisted-web@twistedmatrix.com
Thu, 11 Dec 2003 09:35:48 -0600


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