Zend Framework: Passing objects to Partial Views

Hector Virgen gave some hints about how to pass object into partial views and partial loops;

You can pass objects to partials, just pass them in an array:

<?= $this->partial('mypartial.phtml', array('topic' => $topic')) ?>

The partial helper will take the keys of that array and create view vars out of them, so you can access it like this from within the partial:

<?= $this->topic->getDescription() ?>

If you want to use objects with a partial loop, you can call setObjectKey() on the partialLoop helper and pass in an array of objects:

<?= $this-partialLoop()->setObjectKey('topic')
->partialLoop('mypartial.phtml', $topics) ?>

Editors remark: Easy once you seen it done 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.