Page 1 of 1

Separation question?

Posted: Tue Mar 28, 2006 10:22 am
by asgerhallas
I have this simple class, that reads a hierarchical structure of elements from a database into an array and sorts it.

Now I would like to present that hierarchi in an HTML-list, but after reading a lot of posts in here, I've come to the conclusion, that it's best to separate the rendering part from the datahandling part (am I right?)...

It sounds so easy in theory, but how do I do this separation in practice?

I can't use a method in my class to render the HTML - that wouldn't be separation I think.
I can't make a simple foreach-loop in my HTML "template", cause it isn't that simple to make the list from the array (I have to use a recursive function).

My questions are:
1. Where should I put the code for rendering the list?
2. Should I make another class for that?
3. or should I just write the rendering code directly in the HTML?
4. or maybe something completely different?

Hope some of you can help me find a clean solution.

/Asger

Posted: Tue Mar 28, 2006 12:34 pm
by feyd
Since this is special data to handle, you may need to either create a flattening function to make it a single level, continuous array (no recursion needed) then have a basic array-to-HTML rendering function that fills a template entry or entries depending on how your template works and how you'd like the function to render.

Posted: Tue Mar 28, 2006 12:43 pm
by asgerhallas
Thanks a lot! That makes sense...

Just one thing: Would I put such a flattening-method in my view-, controller- or model-class?

/Asger

Posted: Tue Mar 28, 2006 12:57 pm
by feyd
If it's just flattening the array, I think model, but personally, I don't pay attention to OOP patterns I use, I just build what makes sense. :)

Posted: Tue Mar 28, 2006 4:02 pm
by Christopher
As this is PHP, it seem like the data access part could just return a multi-dimensional array that expressed the hierarchy. Then pass that to the rendering class.

Code: Select all

$db = new MySQL($dsn);
$list = new List($db);
$writer = new ListWriter($list->fetch());
$writer->useStylesheet('blue');
$output = $writer->toHtml();

Posted: Wed Mar 29, 2006 12:22 am
by asgerhallas
This was exactly what I was looking for - really hard to figure out how to do such a simple thing correct, I think.

Thank you very very much!