How to switch layout files in Zend Framework?

PhpZend FrameworkLayoutZend ViewZend Layout

Php Problem Overview


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'); ?>

Php Solutions


Solution 1 - Php

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/)

Solution 2 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - PhpJosh LindseyView Answer on Stackoverflow
Solution 2 - PhpMd Moin UddinView Answer on Stackoverflow