[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: Build unordered list in HTML from a python list
From: Daniel Fetchinson <fetchinson () googlemail ! com>
Date: 2010-06-30 16:01:43
Message-ID: AANLkTin7miim9XU2vvsSszV8iDBc9VRPqn5kZJ-7DCZY () mail ! gmail ! com
[Download RAW message or body]
> I have this python list that represets a sitemap:
>
> tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False},
> {'indent': 1, 'title':'Item 2', 'hassubfolder':False},
> {'indent': 1, 'title':'Folder 1', 'hassubfolder':True},
> {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False},
> {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False},
> {'indent': 1, 'title':'Item 3', 'hassubfolder':False},
> {'indent': 1, 'title':'Folder 2', 'hassubfolder':True},
> {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False},
> {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True},
> {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False},
> {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False},
> ]
>
> From that list I want to create the following HTML code:
>
> <ul id="tree">
> <li>Item 1</li>
> <li>Item 2</li>
> <li>Folder 1
> <ul>
> <li>Sub Item 1.1</li>
> <li>Sub Item 1.2</li>
> </ul>
> </li>
> <li>Item 3</li>
> <li>Folder 2
> <ul>
> <li>Sub Item 2.1</li>
> <li>Folder 2.1
> <ul>
> <li>Sub Item 2.1.1</li>
> <li>Sub Item 2.1.2</li>
> </ul>
> </li>
> </ul>
> </li>
> </ul>
>
> If an item of the list has 'True' for the 'hassubfolder' key than a new
> "<ul><li>" must be created instead of "</li>" after its title. (See
> "Folder 2" node in the HTML code above.
>
> My problem is: How do I keep track of the closing tags while iterating
> over the python list?
Elementtree has already been mentioned as a solution, but if you need
something more lightweight, give markup.py a try:
http://markup.sourceforge.net/ it's a single module and very easy to
use for the sort of things you are after.
Cheers,
Daniel
--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic