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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?