在 Magento 中创建一个新块

发布于 2024-10-06 22:37:30 字数 1072 浏览 4 评论 0原文

我昨天问了这个问题 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-10-13 22:37:30

有许多不同的块可供您使用,而无需创建自己的块。在这种情况下,我认为 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< /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.

<?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>

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文