开发者

Magento child html -- get parent caller

I am in the template file price.phtml. I would like to have some line of code that looks at the parent that is calling the block and do some behavior based on that. Essentially if the parent is a catalog list page, I want a from: tag to be added to the price. If开发者_运维技巧 the parent is configurable.phtml, I want to simply display the price as normal.

I already have the code to add the from: to the price but I need the if statement to tell what the parent caller is.

I have seen something like ::parent before when perusing Mage files, but I don't know if that is applicable here...

Thanks!


YOu can get reference to a block's parent block from a phtml by calling

$parent = $this->getParentBlock();

The calls to parent::someMethod have nothing to do with blocks, or with Magento. They're PHP constructs, used to indicate you want to call a method on the parent class.


here is a bunch of methods that can help you understanding which kind of page you are visualizing in Magento:

  • Current CMS Page: Mage::getSingleton('cms/page')->getIdentifier();
  • Current Route: Mage::app()->getFrontController()->getRequest()->getRouteName();
  • Current Controller: Mage::app()->getFrontController()->getRequest()->getControllerName();
  • Current Action: Mage::app()->getFrontController()->getRequest()->getActionName();

So, for example, if you want to understand you are in a catalog list page you can use the last three in order to check whether the route is catalog, the controller is category and the action is view.

Another method would consist in checking for the presence of a catalog_category_view layout handle in the array of current layout handles that you can retrieve in the following way from a Block or a Template: $this->getLayout()->getUpdate()->getHandles()

Hope it helps.

Regards, Alessandro

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜