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?
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
.