views:

2391

answers:

3

What is best practice to create dynamic sidebar or other non content layout places with zend framework. At this moment I created controller witch i called WidgetsController. In this controller i defined some actions with 'sidebar' response segment for my sidebar and in IndexController i call them with $this->view->action(); function but I don't think that is a best practice to create dynamic sidebar. Thanks for your answers.

A: 

We'd like some more details, i.e what sort of content is shown in the sidebars, how you show them (i.e using a <ul>, <div> or something else), is the content retrieved from a database, etc.

In CodeIgniter which also uses the Model, View, Controller format, we create a view called 'sidebar.php' and in the Header view, we include a call to this sidebar view, i.e:

<html>
<head>.......</head>
<div id="header">....</div>
<?php $this->load->view('sidebar');?>

The sidebar view contains the logic for showing the menu items. Usually it is a static menu, but if it was dynamic and had to be fetched from the database, I would do this:

<ul>
   <?php
   $items=$this->some_model->getMenuItems();
   foreach ($items as $item):
   ?>
     <li><a href="<?=$item['url'];?>"><?=$item['text'];?></li>
   <?php endforeach;?>
</ul>
Click Upvote
Providing a CodeIgniter-based answer - or suggesting that the asker use CodeIgniter - doesn't really contribute to answering the question as asked.
Don Jones
+4  A: 

You question doesn't provide many details. Generally, I'd say load the sidebar as view template, via the render/partial methods of the view. So from inside a view:

//$data is dynamic data you want to pass to the sidebar
echo $this -> partial('/path/to/sidebar.phtml',array('menuitems' => $data));

And then sidebar could process that dynamic data:

//sidebar.phtml
<div id="sidebar">

    <?php foreach($this -> menuitems as $item) : ?>
    <a href="<?php echo $item['url']; ?>"><?php echo $item['title']; ?></a>
    <?php endforeach; ?>

</div>

If you need extra functionality, you could create a dedicated view helper to handle it.

Eran Galperin
A: 

Yes, folks, my bet is that the answer from the Code Igniter folks is the correct approach. Actually its helpful to see how CI does it simply, and to make comparisons. It's basically the same in ZF, except that instead of the "named view" ZF has "Partials".

ZF simply has more imposed discipline, such as layouts + views + partials, and more internal machinery to implement this, which actually does make it run half as fast, whereas code igniter just seems to have flattened this whole apparatus into "named views".

(I haven't yet made up my mind as to whether ZF has over-cooked it or whether some CI steaks have to remain raw in the middle.)

If you use $navigation->setPartial(blah blah) then the leadup array (technically this kind of data constitutes the "model" part of the MVC thing) and is made available to the partial.

So there you have it, the idea seems to be don't pull the display aspects of the model into the controller, push the model display stuff out to the view processing machinery.

I am just about to have a go at this myself, I did do a search on partials in the view helpers section of the Zend Manual to find this, even though the examples are a bit thin.

Wish me luck Keith

Keith