我正在开发一个自定义 Magento (1.3) 主题,并且想添加一个左栏。
我已经创建了带有 html 的 template/page/html/left.phtml。
在 2columns-left.phtml 中,我添加了以下内容:
<?php echo $this->getChildHtml('left'); ?>
在 page.xml 中,我添加了以下内容:
<block type="page/html" name="left" as="left" template="page/html/left.phtml" />
我不太明白的是该 block 类型应该是什么 - 如果我执行 page/html、core/template 或 page/html_header 它似乎可以工作 - 这是为了什么以及这种情况下的正确值是什么,我在哪里只想有效地包含一个 phtml 文件 - page/html/left.phtml 等。
谢谢,
伊恩
最佳答案
这是正在发生的事情的简化版本,但会
希望足以让你继续前进。
特殊元素
Magento 认为三种类型的对象是“特殊的”。这些是模型、 block 和助手。 Magento 没有为这些对象使用类名,而是使用称为类别名的类似 URI 的字符串。所以这
page/html
对应于 Block 类
Mage_Page_Block_Html
这里的类是指 PHP 类,而不是 CSS 类。
Magento 页面渲染
一个布局对象负责为一个 Magento 页面创建所有的 HTML。
布局对象是嵌套 block 对象的集合。
大多数 block 对象都是模板 block ,也就是说, block 类继承自基本的 Magento 模板 block
Mage_Core_Block_Template
.模板 block 是负责渲染 phtml 模板文件的对象。因此,当您在 XML 布局文件中指定“类型”时,您就是在告诉 Magento。
I want to add a block object with the class foo/bar, using the template baz.phtml
在代码中,就是
<!-- "name" and "as" are used to identify the block in the layout, so that
PHP code can get a reference to the object. -->
<block type="foo/bar" name="myname" as="myname" template="path/to/baz.phtml" />
如果您只想渲染一个模板文件,您可以使用
type="core/template"
但是,通过使用不同的值,例如
type="page/html"
您的 phtml 模板文件可以访问
Mage_Page_Block_Html
这意味着你可以做类似的事情
File: template.phtml
<a href="<?php echo $this->getBaseUrl();?>"></a>
core/template
类没有 getBaseUrl
方法,但 page/html
上课。如果您正在进行自定义模块开发(而不仅仅是主题化),我通常会在我自己的模块中创建一个 block 对象,以扩展基本 Magento block 之一。这允许我在我认为合适的时候将我自己的方法添加到 block 中。如果您只是主题,
page/html
是一个不错的默认值。
关于magento - Magento 主题中左栏的 block 类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4287226/