10
votes

I'm working on a Magento module where i want to fetch the layout from a frontend page. By frontend page i mean all types of pages, i.e. cms page, category, product, cart, my account, etc.

I've read the excellent magento-nofrills ebook from Alan Storm and i'm using his Commercebug plugin to help me develop my magento site. I've also spent some time on stackoverflow looking for a similair question, please forgive me if i have missed it.

If for example i have a page id or category id, how do i fetch the layout of that frontend page from code run in the backend?

I've been playing with the request and when i run this code in the frontend i can see that the module, controller, layouthandles etc are changed when for instance looking at a catalog page

$request = Mage::app()->getRequest();
$request->setModuleName('cms');
$request->setRouteName('cms');
$request->setControllerName('page');
$request->setActionName('view');
$request->setParam('page_id', 6);

But when i inspect the xml from the layout, i can't seem to force magento to show me the xml for (in this example) cms page with id=6:

Mage::app()->getLayout()->getNode()->asXml();

Maybe i'm thinking way too complicated though. I would like to check if a certain page has sidebars, which blocks and elements are shown on the page and in the sidebars (only active blocks, not the ignored ones).

Thanks in advance! Tim

1
Did you figure this out? i'm curious myself.chrisjlee

1 Answers

7
votes

Call loadLayout() before getLayout().

You can check if a block is active in the layout by calling getBlock($block_name):

$left_block = $this->loadLayout()->getLayout()->getBlock('left');

If the block isn't part of the layout (or is ignored) then $left_block will be false. You can use getBlock() in conjunction with getSortedChildren() to see its child blocks:

$this->loadLayout()->getLayout()->getBlock('content')->getSortedChildren();

The above will return a string array of block names within the "content" block. Only names of active blocks will be returned.