[Newbies] What is the best way to generate HTML from an array ofarrays

Ramon Leon ramon.leon at allresnet.com
Tue Oct 14 00:10:14 UTC 2008


> Subject: [Newbies] What is the best way to generate HTML from 
> an array ofarrays
> 
> I want to display a threaded discussion. The threads will 
> probably be stored in an array, and n levels of comments 
> would be in nested arrays, e.g.:
> 
> model := #('one' 'two' ('two a' 'two b') 'three').
> 
> I want to generated
> 
> OL
>    LI  one  /LI
>    LI  two  /LI
>    OL
>        LI  two a /LI
> ...
> /OL
> 
> 
> If there were no embedded arrays I would just do something like:
> 
> model do: [:each| html listItem: (each asString)]
> 
> But as soon as I have embedded arrays, my mind goes a bit 
> blank and I start thinking about recursive routines, and that 
> doesn't seem very Smalltalk at all!  So, what is the OO way 
> to do this?
> 
> Thanks
> Andy

Doesn't seem very OO to me, nested arrays sounds kind of ugly.  What's wrong
with a #Comment having a collection of #Comments, and rendering them
recursively?  Why do you think recursion isn't Smalltalk'ish?  What's wrong
with (simplified example)...

renderPost: aPost on: html
    html render: aPost.
    aPost comments do: 
        [ :each | self renderComment: each on: html ]

renderComment: aComment on: html 
    html html: aComment body.
    aComment comments do: 
        [ :each | self renderComment: each on: html ]

Ramon Leon
http://onsmalltalk.com



More information about the Beginners mailing list