60

I'm sure it's a simple one-liner, but I can't seem to find it.

How can I use a different layout file for a particular action?

Update: This worked for me, thanks!

// Within controller
$this->_helper->_layout->setLayout('other-layout') //other-layout.phtml

//Within view script
<?php $this->layout()->setLayout('other-layout'); ?>
Andrew
  • 227,796
  • 193
  • 515
  • 708

2 Answers2

62

From inside a Controller:

$this->_helper->layout->setLayout('/path/to/your/layout_script');

(via these docs)

EDIT: I should mention that the path is relative to whatever your layout directory is (by default, it's application/layouts/scripts/)

Josh Lindsey
  • 8,455
  • 3
  • 24
  • 25
  • 4
    Or, if the layoutpath is set correctly (`$layout->setLayoutPath('application/layouts/scripts')`), use `$this->_helper_layout->setLayout('alternative_layout')` to reference `application/layouts/scripts/alternative_layout.phtml`. – chelmertz Oct 23 '09 at 21:29
  • From inside a view: layout()->setLayout('/path/to/your/layout_script') ?> – Travis Oct 23 '09 at 22:05
  • @Travis: I wouldn't want that kind of logic in a view script unless you only got a really small number of view scripts so you know where to debug for these application critical settings. – chelmertz Oct 23 '09 at 22:52
  • Do you know how to switch this from the bootstrap class instead. – Starx May 13 '11 at 06:42
  • Best to do an absolute path if you are having problems with pathing: `$layout->setLayoutPath(APPLICATION_PATH . 'layouts/scripts');` – Zachary Schuessler Aug 23 '12 at 14:12
13

You can also use like this

// Within controller
Zend_Layout::getMvcInstance()->setLayout('layout_name');

//Within view script

<?php $this->layout()->setLayout('layout_name'); ?>

Your layout must be in /layouts/scripts/ folder, otherwise you need to specify the path also. No need to write .phtml, just name of the layout

Md Moin Uddin
  • 355
  • 5
  • 12