donglan9517 2014-02-05 02:56
浏览 30
已采纳

Magento:如何使用覆盖功能的块和另一个新的自定义块来编写扩展?

I was in the middle of writing some code for a Magento website, and I feel like I'm at odds with what I am trying to accomplish.

I am trying to write an extension which inserts 2 blocks:

  1. Hello_Catalog_Block_Category_View: which overrides the Mage_Catalog_Block_Category_View Block with some extra functionality.

  2. Hello_Catalog_Block_Custom: which is a customised class I want to create for this extension

Here's what I have tried to write in the config.xml file:

<blocks>
            <catalog>
                <rewrite>
                    <category_view>Hello_Catalog_Block_Category_View</category_view>
                </rewrite>
                <class>
                     <custom>Hello_Catalog_Block_Custom</custom>
                </class>
            </catalog>
        </blocks>

Obvously if I tried this code when I refresh the browser, this doesn't work because I must have initialised the custom block the wrong way.

Now if I tried to write in this fashion:

<blocks>
            <catalog>
                <rewrite>
                    <category_view>Hello_Catalog_Block_Category_View</category_view>
                </rewrite>
                <class>Hello_Catalog_Block</class>
            </catalog>
        </blocks>

Now when I refresh the browser, the templates for Catalog Category view don't get rendered and I get the feeling it gets overridden by <class>Hello_Catalog_Block</class>.

My question is, is there a way to write an extension that allows these 2 blocks to be used or together or would it just be a case where either you write an extension that overrides blocks or you write an extension that creates new blocks only, not both?

Thanks.

  • 写回答

1条回答 默认 最新

  • dreinuqm992401 2014-02-05 04:16
    关注

    I think there's a disconnect between what you think a "custom block" will do and what they actually do. There's no way to just add something to config.xml and have the block show up on the page.

    If you want to create a custom block for your module, the first step is to configure a new top level section under blocks

    <blocks>
        <hello_catalog>
            <class>Hello_Catalog_Block</class>
        </hello_catalog>
    </blocks>
    

    The <hello_catalog> node is you block's group name. When you use the above configuration, you're telling Magento

    Hey Magento, if you see a block in the hello_catalog group, it's class name should start with Hello_Catalog_Block.

    With the above in place, you'll be able to do things in Magento's layout update XML files (the XML files in app/design) like this

    <block type="hello_catalog/custom" name="me_custom_block" />
    

    The above XML is creating a block of type hello_catalog/custom. That's a block in the hello_catalog group, with its class name being custom. Magento will translate this into the full class name Hello_Catalog_Block_Custom (using the information from config.xml for the base name, and then lead-word-casing custom.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化