views:

117

answers:

2

Currently trying to come up with a "best practice" for a ZF site that is both elegant and efficient. What I am trying to do is just generate data for a sidebar.

In a couple of related questions and on other sources, the approach taken was to call another action from the sidebar part of your layout, or to add the sidebar action to an actionstack helper. These seemed very elegant, but add an extra request to the dispatch loop, which is very poor performance.

I've seen people suggest view helpers that do a lot more than just formatting... Which seems semantically pretty poor. Is there a way to automate the sidebar data generation before the layout gets to it?

A: 

Hi there are a lot of different ways. I would use an FrontController Plugin wich sends the sidebar content to the layout script. Something like this:

/**
 * layout script
 */
<?php echo $this->sidebar; ?>

/**
 * Plugin
 */
class My_Plugin_Sidebar extends Zend_Controller_Plugin_Abstract {

    /**
     * preDispatch()
     *
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();

        $nav = new My_Model_Menu();
        $entries = $nav->getEntries();

        $html = '<h2> my sidebar </h2>';

        foreach ($entries as $e) {
            $html .= '<li>' . $e . '</li>';
        }

        $layout->sidebar = $html;

    }

}
ArneRie
This looks good.I had settled on writing a view helper but I didn't feel at all satisfied with it on a semantic level.Going to give this a shot
JuhaniC
A: 

If by sidebar you mean navigation, do check out the relatively new component Zend_Navigation which is shipped with a view helper.

chelmertz
I'll check it out, thank you
JuhaniC
If you tried it yet, how did it go?
chelmertz
Sorry about the late response.It was helpful, but I decided to try to stick to the below method to keep down the number of various parts to keep track of. I intend to come back to it in more detail when I get some time
JuhaniC