Last week I showed you how to take an adjacency list in a database and turn it into a nicely indented textual output. Today I will show you how you can take that same (flat) data structure and turn it into an HTML unordered list (UL) using Template Toolkit. The script is available here: gen_ul_tree.pl. If you run the code below it will generate the HTML for the data from last week.
$ wget -q -O - http://files.smidsrod.no/gen_ul_tree.pl.txt | perl - It should look something like this:
- United States
The template code code is fairly well commented, so it shouldn't be necessary to explain here what it does.
Now go out and make some nice treeviews on your web pages. And while you're at it, sprinkle some jQuery on top of it to make it interactive.
Update (2020-10-09): Ben Brumm contacted me and suggested linking to an article about storing hierarchical data in SQL. It describes several different methods of storing and querying the tables, with different pros and cons.