在Magento中创建一个新块 [英] Create a new Block in Magento
问题描述
我昨天问了这个问题 Magento主页上的静态块,它回答了我关于将cms/块连接到现有块(在该示例中为内容).
I asked this question yesterday Static block on home page in Magento, which answered my question about hooking a cms/block to an existing block (content, in that example).
但是现在我想知道如何创建自己的块.
But now I would like to know how to create my own block.
我的.phtml模板中有此文件:
I have this in my .phtml template:
<?php echo $this->getChildHtml('home_flash') ?>
这在我的cms.xml文件中
And this in my cms.xml file
<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文件中创建自己的块类型(通过复制面包屑声明):
I have also tried to create my own block type, (by copying the breadcrumbs declaration) in the page.xml file:
<block type="page/html_home_block" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
该文件存在,但未呈现.
That file exists but isn't being rendered.
但是当我引用这样的代码块时:
However when I reference the block like this:
<block type="page/html_breadcrumbs" name="home_block" as="home_block" template="page/template/home_block.phtml"/>
它呈现了我的home块模板,但是原始cms/block没有附加到它.
It renders my home block template, but the original cms/block is not attached to it.
希望所有不同的情况都能显示正在发生的事情,并充分突出我的知识差距,以便有人回答,我是否必须在某个地方注册"新的"home_block"类型?
Hope all the different cases show what is happening and highlight the gap in my knowledge well enough for someone to answer, do I have to "register" my new "home_block" type somewhere?
推荐答案
您可以使用许多不同的块而无需创建自己的块.在这种情况下,我认为core/text_list
是合适的,因为它不需要模板,并且其中可以包含所需的任意数量的子块.
There are many different blocks available that you can use without creating your own. In this case I think core/text_list
would be suitable because it doesn't require a template and can have as many child blocks within it as you need.
<?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/text
和core/template
,它们分别对应于Mage_Core_Block_Text
和Mage_Core_Block_Template
.他们最习惯了.
您自己制作的page/html_home_block
块类型没有任何具有匹配名称的PHP类,如果您是真正创建自己的PHP类,则由于Magento已经使用了page
前缀,因此无法使用.
Other useful block types worth knowing are core/text
and core/template
which correspond to Mage_Core_Block_Text
and Mage_Core_Block_Template
respectively. They get used the most.
Your home made block type of page/html_home_block
didn't have any PHP class with a matching name, and if you were genuinely creating your own you wouldn't be able to use the page
prefix since Magento already does.
要创建块,只需在布局文件中使用<block>
标签.
要创建块类型,您需要编写一个PHP类,为其提供一个名称空间,并将其声明为模块的一部分.
添加到现有块的时间就是使用<reference>
标记的时间.
To create a block you only need a <block>
tag in the layout file.
To create a block type you need to write a PHP class, give it a namespace and declare it as part of a module.
To add to an existing block is the time when you use a <reference>
tag.
Magento知识库上有很多精美的文章,其中包括
There are many fine articles at Magento Knowledge Base including some on Theming & Design.
这篇关于在Magento中创建一个新块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!