Magento child html -- get parent caller
Solution 1
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.
Solution 2
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
Nathaniel Wendt
Updated on June 04, 2022Comments
-
Nathaniel Wendt about 2 years
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 afrom:
tag to be added to the price. If the parent isconfigurable.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!