Magento extension adding and positioning new blocks in existing/core pages

I'm trying to create a module that adds new content to existing pages. For testing purposes I'm trying to add to the checkout/cart/index page, but I want a general solution as there are other pages I need to modify.

I've examined the code of free extensions which achieve this by copying all the code of an existing template file into an override template and then using getChildHtml('some_block') to show a new block at the appropriate place. I'd like to add my blocks without having to override any of the core template files to make my module as compilable as possible.

This thread Magento - Add custom block using custom module on Shopping Cart page, shows how to add a block but the accepted answer doesn't work for me. I've followed the link to Alan Storms's answer to another Stackoverflow question Magento XML using before/after to place blocks hardly ever works and I sort of understand that most block types will only render sub blocks if there are direct calls to getChildHtml('subblockname') within the blocks template file.

Is there a general way of adding blocks to any part of an existing page without having to override the template files? What is the preferred way in Magneto modules of adding to existing content on a page as I cannot believe that overriding core files is the way to do this.

Thanks.


You don't need to override magento native template files. Just create your own theme. Magento will search markup file in your theme and if he did not find it, then he will be using base theme.

Also you can set your own template file name or path for this template using xml layouts. All what you need for this - make referense for existing block name and add action setTemplate. Example:

<layout>
    <checkout_onepage_review>
        <reference name="root">
            <action method="setTemplate">
                <template>my/path/to/template/file.phtml</template>
            </action>
        </reference>
    </checkout_onepage_review>
</layout>

You need two things to insert a block using only your own layout and have the block be output in the page.

Firstly, you need your layout to insert your block into an existing block that has the type="core/text_list" attribute - or a block which extends the Mage_Core_Block_Text_List class. Examples of core/text_list blocks include left, right, content, before_body_end and so on.

Secondly, you need the template assigned to that block to include this call:

echo $this->getChildHtml();
//note that there is no block alias parameter passed in to this function

Calling getChildHtml() without a block alias will output all children blocks, in order. If your block is a child of this one's layout, then it will be output, too.

I'm assuming that your module has its own layout file linked from the it's etc/config.xml , in which case you can try adding this layout update:

<default><!-- On all pages -->

  <reference name="content"><!-- in the content (middle) column -->

    <block type="your_module/your_block" name="my.test.module" template="your/template/path.phtml" before="-" /><!-- insert your block -->

  </reference>

</default>

Once you can see that this is working, you can start digging through the default layouts to find blocks into which you can insert yours.

Note the before="-" attribute. This causes the block the be inserted before all other blocks in that container. You can also use before="block.alias" to position your block before a particular block in the list, and after="block.alias" to position it after a particular block. You could write after="-" to position your block at the bottom of the list.

There's a brief introduction to the Magento layout system here.

If you plan on distributing your module, you'll find that most people have modified and customised either the templates, layout, or both, of the pages you want to modify and your non-invasive approach will not work and you will have to, either, modify and override core templates, use the layout to change a block's template to your own modified one (without overriding files using the filesystem), or instruct end-users on how to add the right echo command into their existing templates.

I'd suggest, instead, this approach:

Add new templates into template folders for your module that are modified from the base/default or default/default templates just enough to make your module work in those themes.

Add custom layout to your module that makes these changes when it is installed - override the template for the blocks you need to modify in the layout:

<checkout_cart_index><!-- on the cart page -->
  <reference name="checkout.cart"><!-- in the cart block -->
    <action method="setTemplate"><!-- change the block's template -->
      <template>path/to/your/modified/template.phtml</template>
    </action>
  </reference>
</checkout_cart_index>

You should also provide documentation on how to integrate your changes into third-party templates by adding echo $this->getChildHtml('your.block.alias'); where necessary.

People who have customised their themes, have to accept that they must customise other features, or adapt their existing changes, to use them in their themes, too.

链接地址: http://www.djcxy.com/p/59486.html

上一篇: 在Magento中使用布局

下一篇: Magento扩展在现有/核心页面添加和定位新块