Zend Framework - use a common header for different actions in a controller?

1.1k views Asked by At

I've tried reading the question found HERE: Module configuration and layout configuration in zend framework It was described as being everything I need to know about layouts - but - I'm afraid I'm finding it a bit difficult to understand.

I've got a number of Zend controllers:

class FirstController extends Zend_Controller_Action {
   public function init()        { /* stuff */ } 
   public function indexAction() { /* stuff */ } 
   public function indexBrowse() { /* stuff */ } 
}

class SecondController extends Zend_Controller_Action {
    // stuff   
}

class ThirdController extends Zend_Controller_Action {
    // stuff
}

I need them to have the following arrangement.

  1. FirstController & Second Controller share a header
  2. FirstController - indexAction and browseAction share an additional header
  3. Thirdcontroller - has it's own header or possibly "no header" (for ajax)

As it stands, I'm getting tremendous replication in the view/script/<actionname>.phtml file http://framework.zend.com/manual/en/zend.layout.quickstart.html

Has more information but I've not been able to find the key pieces of information that bring this all home for me.

From the first document above I'm guessing the following goes into my application.ini file

 resources.layout.layout = "layout"
 resources.layout.layoutPath = APPLICATION_PATH "/layouts"

But am I expected to create a folder called "Layouts" or should I be using some sort of views/common folder? Is the file then called layout.php ?

Then, inside the layout.php am I understanding correctly that

    <div id="content"><?php echo $this->layout()->content ?></div>

Will render the individual action's PHTML file? and

   public function anotherAction() {
        $this->_helper->layout->setLayout('foobaz');
   }

Would make the entire action use a different layout file (one in the layouts folder called 'foobaz.php')?

Thanks for taking the time to clear this up for me.

1

There are 1 answers

0
Bodman On BEST ANSWER

Yea, you have it right,

 $this->_helper->layout->setLayout('foobaz');

Should render the page with a different layout, and simply output the views contents in the layout()->content place holder.

Another way would be to have a single layout, and use partials to load different headers.

In your action you could have

 public function indexAction() { 
       $layout = $this->_helper->layout();
       $layout->assign('header', 'header_file_name');
}

Then in your layout you could just do this

   <div id="header">
       <?php echo $this->partial($this->layout()->header);?>
 </div>

This is probably over the top.