views:

624

answers:

2

How do I implement a sidebar in Zend Framework? I know that I can use a placeholder or something similar in Zend_layout, but how do I automatically generate the code for the sidebar in my controllers without having to call a sidebar class within every controller? My setup is as follows

Application
- modules
  - blog
  - other modules

I only want the sidebar for my blog module.

I have found this http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-layout-sidebar-etc-2677.html but I do not understand the last part "just inject your layout, register it with the front controller ..."

+2  A: 

You could just have a action and view in one of your controllers which renders the sidebar.

from the layout for the blog module you just call:

<? echo $this->action('action','controller','module',array('optionalparams'=>1); ?>

on the position where you want to have it. So one call to one action.

Rufinus
This is very expensive, I believe.
blockhead
what you mean by expensive ? AFAIK is this the way to do it. The code mentioned in the zfforums link, breaks with the MVC pattern IMHO.
Rufinus
Thank you, I knew that it should be easy. I just need to figure out how I can have different layouts per module. For now I am having the same layout for every module.Blockhead: How do you mean expensive? Would it be less expensive if I call the action within my controller? My first Idea was to create a default controller that fixed the sidebar and then inherit from that controller in all my blog controllers.
unkown
unfortunatly at the moment ZF doesnt support a layout per moudle out of the box. but as always its not to hard to implement it. see http://dustint.com/archives/28 for details.
Rufinus
Thank you for your quick answers.
unkown
http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/ - Action Stack and Zend_View_Helper_Action are evil - they kill faeries in #zftalk, and takes a lot of extra processor/memory to create a request object and then dispatch it. $this->partial() would be better to use. The Action view helper may be removed in ZF 2.0 because of some of these factors. http://framework.zend.com/issues/browse/ZF-5840
gnarf
@gnarf: good point, i actualy never had had a performance problem with it. but good to know, thanks.
Rufinus
+1  A: 

If you are using Zend_Layout, just add the sidebar with the Action viewhelper as Rufinus said.

in your layout script:

<div id="sidebar">
<?php echo $this->action('action', 'controller', 'module', array('optionalparams'=>1)); ?>
</div>
<div id="content">
<?php echo $this->layout()->content; ?>
</div>

This should meet the requirements posted in your question.

Karsten
Thank you for confirming Rufinus answer
unkown