Magento子html——获取父调用者
我在模板文件 price.phtml
中。我想要一些代码行来查看调用该块的父级并基于此执行一些行为。本质上,如果父级是目录列表页面,我希望将 from:
标记添加到价格中。如果父级是 configurable.phtml,我只想正常显示价格。
我已经有了将 from:
添加到价格的代码,但我需要 if 语句来告诉父调用者是什么。
我之前在阅读 Mage 文件时见过类似 ::parent
的东西,但我不知道这是否适用于这里......
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过调用从 phtml 获取对块的父块的引用
对
parent::someMethod
的调用与块或 Magento 无关。它们是 PHP 构造,用于指示您想要调用父类上的方法。YOu can get reference to a block's parent block from a phtml by calling
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.这里有很多方法可以帮助您了解在 Magento 中可视化的页面类型:
Mage::getSingleton('cms/page')->getIdentifier();
Mage::app()->getFrontController()->getRequest()->getRouteName();
Mage::app()->getFrontController()->getRequest()->getControllerName();
Mage::app()->getFrontController() ->getRequest()->getActionName();
因此,例如,如果您想了解自己位于目录列表页面,您可以使用最后三个,用于检查路由是否为目录,控制器是否为类别,操作是否为查看。
另一种方法包括检查当前布局句柄数组中是否存在 catalog_category_view 布局句柄,您可以通过以下方式从块或模板中检索该句柄:
$this->getLayout()->getUpdate()->getHandles()
希望有帮助。
问候, 亚历山德罗
here is a bunch of methods that can help you understanding which kind of page you are visualizing in Magento:
Mage::getSingleton('cms/page')->getIdentifier();
Mage::app()->getFrontController()->getRequest()->getRouteName();
Mage::app()->getFrontController()->getRequest()->getControllerName();
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