What is the best way to generate HTML from an array of arrays

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the best way to generate HTML from an array of arrays

Andy Burnett
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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: What is the best way to generate HTML from an array of arrays

Randal L. Schwartz
>>>>> "Andy" == Andy Burnett <[hidden email]> 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.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: What is the best way to generate HTML from an array ofarrays

Ramon Leon-5
In reply to this post by Andy Burnett
> 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 mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners