在 Magento 中创建一个新块
我昨天问了这个问题 Magento 主页上的静态块,它回答了我关于将 cms/block 挂接到现有块的问题(内容,在该示例中)。
但现在我想知道如何创建我自己的块。
我的 .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”类型?
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.
I have this in my .phtml template:
<?php echo $this->getChildHtml('home_flash') ?>
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>
But this is not working.
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"/>
It renders my home block template, but the original cms/block is not attached to it.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有许多不同的块可供您使用,而无需创建自己的块。在这种情况下,我认为
core/text_list
比较合适,因为它不需要模板,并且可以根据需要在其中包含任意多个子块。其他值得了解的有用的方块类型是
core/text
和core/template
,它们分别对应于Mage_Core_Block_Text
和Mage_Core_Block_Template
。他们用得最多。您自制的
page/html_home_block
块类型没有任何具有匹配名称的 PHP 类,如果您真正创建自己的块类型,则将无法使用page< /code> 前缀,因为 Magento 已经这样做了。
要创建块,您只需要在布局文件中添加
标记。要创建块类型,您需要编写一个 PHP 类,为其指定命名空间并将其声明为模块的一部分。
添加到现有块的时候就是使用
标签的时候。Magento 知识库 上有许多精彩文章,其中包括 主题和主题设计。
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.Other useful block types worth knowing are
core/text
andcore/template
which correspond toMage_Core_Block_Text
andMage_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 thepage
prefix since Magento already does.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.There are many fine articles at Magento Knowledge Base including some on Theming & Design.