Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Magento block template in layout xml

Tags:

magento

Having trouble setting a block template in Magento's layout xml. I'm attempting to set the template of a child block, not the entire page layout (almost all docs out there explain how to set template of the layout).

Background: I'm updating a layout handle in my custom action, using the <update /> tag in my module's layout xml.

Essentially, I want to reuse the layout and blocks of the built in product view action, but provide custom templates for a few blocks. (Not just overrides, these need to be brand new templates that are only triggered on my custom action and are themselves overrideable).

My layout html:

<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_product_index>
    <update handle="catalog_product_view" />
    <reference name="content">
        <block type="catalog/product_view" 
        name="product.info" output="toHtml" template="mymodule/product.phtml" />
        </reference>

    <reference name="product.info.bundle">
        <action method="setTemplate"><template>mymodule/customtemplate.phtml</template></action>
    </reference>
</mymodule_product_index>
</layout>

The setTemplate on product.info.bundle never works; it doesn't seem to affect layout at all. I've tried wrapping the <reference> in other <reference> nodes from parent blocks with no effect. Is it possible to replace block templates in this way? I feel that my problem stems from the fact I'm using an <update />.

By the way, I know my layout xml is being loaded and there are no errors, the rest of the file is working fine, caching is disabled, have cleared cache anyway, etc.

like image 431
Nick Daugherty Avatar asked Mar 03 '12 05:03

Nick Daugherty


1 Answers

Your approach is almost correct.
Two things:
1. Set a new template instead of instantiating a new block
Instead of just assigning a different template to the product.info block, you are creating a new instance with the same name, replacing the original instance, and then the new template is set on that. Instead use this:

<mymodule_product_index>
    <update handle="catalog_product_view" />
    <reference name="product.info">
        <action method="setTemplate">
            <template>mymodule/product.phtml</template>
        </action>
    </reference>
</mymodule_product_index>

That should take care of the product view template in a clean way.

2. Handle processing order
If you look at where the view block product.info.bundle for the bundled products is declared, you will see it happens in the bundle.xml file, in a layout update handle called <PRODUCT_TYPE_bundle>.

Your code is referencing the block from the <[route]_[controller]_[action]> layout handle, i.e. <mymodule_product_index>.

The thing to be aware of here is the processing order of layout handles. Roughly it is:

  1. <default>
  2. <[route]_[controller]_[action]>
  3. <custom_handles>

The <PRODUCT_TYPE_bundle> handle belongs to the third type of layout handles, which means it is processed after the <mymodule_product_index> handle.
In essence, you are referencing the block product.info.bundle before it has been declared.

To fix this you will need to use the <PRODUCT_TYPE_bundle> handle as well. Of course this will effect every bundled product display. Using layout XML only there is no clean way around that.

Here are a few suggestions how to solve that problem.

You could create a separate route in your module to show the bundled products, and then include the <PRODUCT_TYPE_bundle> handle using an update directive for that page, too.

In your custom action controller, you could add another layout update handle that is processed after <PRODUCT_TYPE_bundle>.

You could use an event observer to set the template on the product.info.bundle block if it is instantiated. One possibility would be the event controller_action_layout_generate_blocks_after.

You get the idea, there are many ways to work around this, but they require PHP.

like image 144
Vinai Avatar answered Oct 22 '22 03:10

Vinai