views:

55

answers:

3

In the app I'm building, I'd like to have a nice footer with data from the database (tags) and a contact-form. All the parts are scripted, but now I'd like to put them all together. The elements of the footer are the same for every page loaded, only the content is different.

How can I do this, without having to put too much code on every controller?

A: 

Assign the content to a view variable where ever it differs, and then just print the footer's content based on that view variable.

reko_t
+1  A: 

Use view helpers. Create a helper for each of the dynamic info, like the tags. And then you can have it's output directly on the view. This avoids unnecessary code in the controller.

Here's an example for the tags. Create a Tags.php file containing:

class Zend_View_Helper_Tags extends Zend_View_Helper_Abstract {

    public function tags() {
        // build an array with all the tags and return it
    }
}

And now on your footer.phtml you can use this like:

<?php $allTags = $this->tags();
foreach ($allTags as $tag) { ?>
    <a href="<?php echo $tag['url']; ?>"><?php echo $tag['name']; ?></a>
<?php } ?>

You can use partialLoop() to get a cleaner view.

rogeriopvl
THanks for the very helpfull answer! I've done it this way :-)
koko
A: 

The best way to do this is actually to use the Zend_Layout component to implement the Two-step View Pattern. What that means is that you render your regular view as normal, but then you have Zend_Layout take that output and place it within an overall template. That overall template would be where you put your footer. Zend_Layout Quick Start

Kevin Schroeder