views:

39

answers:

3

Hello, I have a recursive function being used to generate a menu on my site. The function is calling a database for each level of children in the menu, and generating html for them.

I've currently put this function in a Model part of the code, however, I feel that generating html in the model goes against the MVC. I didn't put it in a Controller because I didn't want to have database calls or HTML generation there. I didn't put it in a View because I didn't want database calls there either.

Is the 'correct' way of tackling this problem to have a Controller call a recursive function in a Model that returns a 2d array representing the menu. Then pass the array to a view which has it's own recursive function for generating html from the array?

+2  A: 

As you suggest the best way would probably be to have the model fetch all the data. And then supply that to the view in a appropriate format. Then generate the html recursively in the view.

Mattias Jakobsson
A: 

You must return from model complete menu array/nested arrays.

Svisstack
A: 

I created a component in my project that retrieves the menu data from the database, formats the array containing menu items and caches it. Then I created a helper that takes that array and creates an html list with menu items which can be used in views and layouts.

So, in the layout I have: echo $databaseMenus->makeMenu($mainMenu); where makeMenu is the helper's method and $mainMenu is the array provided by the component.

bancer