views:

128

answers:

3

I have a template for a "blog preview" - which is basically just a thumbnail, title, and short excerpt of said blog in a nice concise structure built for repetition in a list.

As hinted above, I intend to pull the top 10 blogs on my site from the DB in my model, transfer them to the controller, which will supply them as for the view. In the view, I will need to loop through the results and populate a new "blog preview" for each blog.

My current solution (which I think may break the rules of MVC) is to do this in the view template:

foreach($this->blogs as $blog) {
    $tpl = new Output_Html();
    $tpl->title = $blog['title'];
    // ...assign other vars

    $tpl->render();
}

Somehow this feels like something the view shouldnt be allowed to do? But, how else would I be able to loop through the "preview" templates inside of the main page template?

Help?

+1  A: 

The controller's reponsibility is to handle input and to set the Model into a certain state. Your View's reponsibility is to render the Model. So, I'd say, this is okay to do. It's like rendering a Partial View or using a ViewHelper. View Helper encapsulate presentation logic for dynamic business data.

Gordon
+1  A: 

Considering the View is responsible for the generation of the output, what you are doing here seems OK : you are not doing any "calculation / business thing / anything like that" in your View.

The only problem I have is that you are writing a lot of code here ; I would rather like to pass the $blog array/object to the View a whole, and let the View deal with it -- instead of assigning each property of the $blog to the View.


i.e. something like this seems (just an idea -- up to you to see how this can fit with your View class) more pretty :

foreach($this->blogs as $blog) {
    $tpl = new Output_Html();
    $tpl->blog = $blog;
    $tpl->render();
}

This means that, if your blog object ever changes, you only have one View to edit (to add or remove stuff), and you don't have to modify each call to that view to add/remove one component/property of $blog.

Pascal MARTIN
well done, i like that. actually - that is how i was doing it, but i neglected to illustrate the object being passed because i wasn't sure if that was kosher either. thanks for saving me another question!
johnnietheblack
You're welcome :-) Have fun !
Pascal MARTIN
A: 

I agree with Pascal. Instead of performing that operation on your controller, simply pass it to the View and do the rest of stuffs there. Let the Controller take in the inputs, preprocess it via Models, then prepare everything for the View. That way, you still ensure the maintainability of your code.

swastika