2010-12-07 55 views
1

一个新的块我昨天Static block on home page in Magento问过这个问题,它回答了一个挂钩CMS /块,以现有的块(内容,在例子)我的问题。建立在Magento

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

我有这个在我的一个.phtml模板:

<?php echo $this->getChildHtml('home_flash') ?> 

这在我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> 

但是,这是行不通的。

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

<block type="page/html_home_block" name="home_block" as="home_block" template="page/template/home_block.phtml"/> 

该文件存在,但不被渲染。

然而,当我引用了块这样的:

<block type="page/html_breadcrumbs" name="home_block" as="home_block" template="page/template/home_block.phtml"/> 

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

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

+0

这并不是100%清楚你想要完成什么,这使得理解问题变得困难。你能解释一下你试图达到的最终结果是什么,以及更具体的事情吗? (即“我的.phtml模板”:哪个phtml模板?) – 2010-12-07 18:31:01

+0

@Alan Storm,感谢评论,我知道这个问题很长,我试图在最后一行问题中总结它! @clockworkgeek回答了我的问题,事实证明我错过了关于Magento的MVC位的观点! – 2010-12-08 11:41:56

回答

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> 

其它有用的嵌段类型都是core/textcore/template其分别对应于Mage_Core_Block_TextMage_Core_Block_Template。他们使用得最多。
page/html_home_block 你自制块类型没有具有匹配名称的任何PHP类,如果你是真正创建自己的,你将无法使用page前缀,因为Magento的已经这样做了。

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

有在Magento Knowledge Base许多优秀的文章,包括一些上Theming & Design