0

每个块中的 Template.php 是否为属于这些模块的所有模板定义了通用方法?

我对此很感兴趣。我正在研究一个插件,有一个名为 init.phtml 的模板,其中使用了 /Block/Template.php 中定义的方法。我不知道为什么它可以直接在 init.phtml 中调用 $this->methodInTemplateBlock()。谁能给我解释一下?提前致谢!

4

1 回答 1

0

如果您查看方法Mage_Core_Block_Template::fetchView,您将看到以下内容:

public function fetchView($fileName)
{
    ...
    try {
        $includeFilePath = realpath($this->_viewDir . DS . $fileName);
        if (strpos($includeFilePath, realpath($this->_viewDir)) === 0 || $this->_getAllowSymlinks()) {
            include $includeFilePath;
        } else {
            Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
        }
    ...
}

如您所见,模板文件的内容直接包含在该类中的方法体中,这意味着您可以通过 $this 简单地引用该类实例。

于 2012-05-03T08:24:44.420 回答