Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento get which layout being used on phtml files

Is there a way I could get which layout being used on a certain phtml files?

Here in my case, I want to check what layout being used on catalog/list.phtml, I used that information to make conditional "if" on the product image grid size.

I've tried to google it out. But all the result is just explaining about xml layout things. The closest clue I got is this thread

Magento get layout for given page

which stated the use of this snippet

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

but when I tried it on the phtml files, I got an exception error

UPDATE

joe's answer below has give me some more clue, the exception gone. But the behavior doesn't really what I need. That snippet of code seems to be just check whether the specified block is defined on the XML. What I really need is whether that block exist on a certain page.

In my case, I need to check what layout being used on catalog/product/list.phtml. if it's 3 columns, I'm gonna make the image resized smaller. If it 1 column, I'll make it bigger.

Is there any way I could do that??

like image 702
Kamal Avatar asked Jun 21 '12 07:06

Kamal


2 Answers

If I read the question correctly, then try:

$this->getLayout()->getBlock('root')->getTemplate();
like image 151
djdy Avatar answered Sep 28 '22 06:09

djdy


Remove loadLayout():

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

By the time you are in the PHTML file, the layout is already loaded.

In PHTML files, $this refers to the Mage_Core_Block_Template class (or a class that extends it). This class doesn't have a loadLayout() method defined, which is why you get an exception; instead, loadLayout() is part of Mage_Core_Controller_Varien_Action.

like image 33
Joe Avatar answered Sep 28 '22 04:09

Joe