tags:

views:

39

answers:

2

What is the correct (recommended method) for passing information to a view in a MVC environment.

Currently we use Zend Framework, where we write classes to handle specific things EG

a Book class with a save and load method to retrieve info from the DB, which is called from a particular nameAction().

What I would like to know is what is the best way to pass this information to the view, in some cases we do $this->view->book_name = $book->getBookName();

and in other cases we do the following

$this->view->book = $book; OR $this->view->books = Book_Manager::getAllBooks(); and then access the object(s) properties in the view.

Information on-line suggests we try limit what access a view has to information, i.e pass them only what they need and in some cases people say its ok to pass stuff through as long as nothing is done to that information.

Regards

A: 

I agree with the second viewpoint; I think it's fine to pass along entire objects to the view, so long as the template writer is unable or unlikely to mutate the object. There are several ways to ensure this, but on a small enough team, I'd hope a stern warning is enough =)

Triptych
A: 

I usually tend to use the second form (passing the full object to the view) because, let's face it: it's easier to pass the object and then in the view to render the needed information.

As a good practice, I'd probably go for the first option: pass to the view only what's really needed, but in a world where we never stop developing, I vote for the second.

Bogdan Constantinescu