在Magento中创建一个新块

Ala*_*law 1 php xml layout block magento

我昨天在Magento的主页上问了这个问题静态块,它回答了我关于将cms /块挂钩到现有块的问题(在该示例中为内容).

但现在我想知道如何创建自己的块.

我在.phtml模板中有这个:

<?php echo $this->getChildHtml('home_flash') ?>
Run Code Online (Sandbox Code Playgroud)

这在我的cms.xml文件中

<reference name="home_flash">
  <block type="cms/block" name="home-page-flash" before="content">
    <action method="setBlockId"><block_id>home-page-flash</block_id></action>
  </block>
</reference>
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

我也曾尝试创建自己的块类型,(通过复制面包屑声明)在page.xml文件:

<block type="page/html_home_block" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
Run Code Online (Sandbox Code Playgroud)

该文件存在但未呈现.

但是当我像这样引用块时:

<block type="page/html_breadcrumbs" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
Run Code Online (Sandbox Code Playgroud)

它使得我的家乡块模板,但原来的CMS /块不重视它.

希望所有的不同的情况表明正在发生的事情,并强调在我的知识缺口不够好,有人来回答,我必须为"注册"我的新"home_block"类型的地方?

clo*_*eek 5

您可以使用许多不同的块,而无需创建自己的块.在这种情况下,我认为core/text_list它是合适的,因为它不需要模板,并且可以根据需要包含尽可能多的子块.

<?xml version="1.0"?>
<layout version="0.1.0"><!-- All layout files start with this -->
    <cms_index_index><!-- Index directive is the same as "home" page -->
        <reference name="root"><!-- For more blocks that can be referenced see "default" directive -->
            <block type="core/text_list" name="home_flash">
                <block type="cms/block" name="home-page-flash">
                    <action method="setBlockId"><block_id>home-page-flash</block_id></action>
                </block>
            </block>
        </reference>
    </cms_index_index>

    <!-- More directives might go here -->

</layout>
Run Code Online (Sandbox Code Playgroud)

其它值得了解有用的块类型是core/textcore/template对应于Mage_Core_Block_TextMage_Core_Block_Template分别.他们得到了最多的使用.
您的自制块类型page/html_home_block没有任何具有匹配名称的PHP类,如果您真正创建自己的类,则page由于Magento已经存在,您将无法使用该前缀.

要创建块,您只需要<block>布局文件中的标记.
要创建块类型,您需要编写一个PHP类,为其命名并将其声明为模块的一部分.
要添加到现有块,是使用<reference>标记的时间.

Magento知识库中有许多精美的文章,包括一些关于主题和设计的文章.