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
"Andy" == Andy Burnett andy.burnett@knowinnovation.com writes:
Andy> If there were no embedded arrays I would just do something like:
Andy> model do: [:each| html listItem: (each asString)]
Andy> But as soon as I have embedded arrays, my mind goes a bit blank and I start Andy> thinking about recursive routines, and that doesn't seem very Smalltalk at Andy> all! So, what is the OO way to do this?
Create a visitor pattern. Teach each of strings and arrays how to present themselves.
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
beginners@lists.squeakfoundation.org