Magento get which layout being used on phtml files

喜你入骨 提交于 2019-12-10 17:11:19

问题


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??


回答1:


If I read the question correctly, then try:

$this->getLayout()->getBlock('root')->getTemplate();



回答2:


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.



来源:https://stackoverflow.com/questions/11133522/magento-get-which-layout-being-used-on-phtml-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!