Hello,
Do you spoon-feed your views from your controller? In other words, do you
provide your view with every piece of information it needs to render, or do
you just pass the full model to the view and let the view pull from the
model on its own?
For example:
*Spoon-fed views*
// in controller
public function viewAction()
{
$blog = {aquire blog entry model somehow}
$this->view->title = $blog->getTitle();
$this->view->description = $blog->getDescription();
$this->view->content = $blog->getContent();
}
// in view
<h1><?= $this->escape($this->title) ?></h1>
<div class="description"><?= $this->escape($this->description) ?></div>
<div class="content"><?= $this->escape($this->content) ?></div>
*Full Model Alternative*
**// in controller
public function viewAction()
{
$blog = {aquire blog entry model somehow}
$this->view->blog = $blog;
}
// in view
<h1><?= $this->escape($this->blog->getTitle()) ?></h1>
<div class="description"><?= $this->escape($this->blog->getDescription())
?></div>
<div class="content"><?= $this->escape($this->blog->getContent()) ?></div>
I personally have been using the alternative and pass the entire model to
the view, but I have seen a blog posts where views are spoon fed. Usually
with something along the lines of:
$this->view->assign($model->toArray());
I can see advantages and disadvantages to each approach. For example, I
believe the view should be responsible for deciding what content to display.
If later we decide we need to also display the date, then it's a simple
change in the view layer only.
But spoon-fed views seem like they'd be easier for designers to work with
and can prevent the view from doing things it shouldn't be allowed to do
(like modifying the model).
Which do you use, or is there a better way?
--
Hector