【问题标题】:magento 2column-right template where doe s $this refer too?magento 2 column-right 模板 $this 指的是哪里?
【发布时间】:2025-12-04 06:00:01
【问题描述】:

在 template.phtml 文件中有一行代码引用内容,使用 getChildHtml('内容') ?> 它回显了内容和包含的 div,我希望访问这个原始代码,以便我可以插入 一些自定义数据,但我一生都找不到它在哪里?!我在核心中找到了 Mage_Page_Block_Html 类,但看不到它抛出的任何 html!

提前致谢

【问题讨论】:

  • 我没有侮辱的意思,所以我提前道歉。这是一个非常新手的问题。你最好先阅读一些guides

标签: templates magento this php


【解决方案1】:

在管理员中,如果您转到系统>配置>高级>开发人员并从范围选择器(左上角)中选择您的商店视图,您可以打开模板和块提示。这将准确地告诉您哪个 PHP 块正在生成代码,因此 $this 与之相关。

一般来说,所有的基本布局模板都由Mage_Core_Block_Template 支持,所有块都继承自Mage_Core_Block_Abstract

干杯, 乔纳森

【讨论】:

  • 您好,谢谢您,Mage_Core_Block_Abstract 大概在哪里?
  • 类名翻译成它的位置。所以 Mage_Core_Block_Abstract 变成 'app/code/core/Mage/Core/Block/Abstract.php'
【解决方案2】:

与往常一样,我建议understanding what the entire layout system is doing 了解发生了什么。

至于您的具体问题,$this 始终指的是模板的 Block 类。 (每个 phtml 模板在 Magento 中都有一个 Block 类)。这是哪个类取决于您的布局 XML 文件中的内容。您总是可以在运行时使用类似这样的方式输出类

var_dump(get_class($this));

getChildHtml 方法是在基本抽象块类上定义的

app/code/core/Mage/Core/Block/Abstract.php

所有块都使用此方法,因此请确保在代码中考虑到这一点。

【讨论】:

    【解决方案3】:

    附带说明.. 如果您想知道 $this 指向的位置,您也可以使用

    echo get_class($this)
    

    帮我安静了几次

    【讨论】: