Magento – How to set Block’s template by calling a helper from layout XML

email-templateshelperlayoutmagento-1

I am trying to dynamically assign a template based on a condition. My helper works when called one way, but not another, which makes me believe I am using layout XML incorrectly.

To clarify, my helper is working correctly, but I am having trouble calling the helper in this case.

This works

Calling getOrderEmailItemsTemplate()

<block type="sales/order_email_items" name="items" template="email/order/items.phtml">
    <action method="addItemRender">
        <type>default</type>
        <block>sales/order_email_items_order_default</block>
        <template helper="Emailbygroup/Data/getOrderEmailItemsTemplate"></template>
    </action>
...

This doesn't work

Calling getOrderEmailTotalsTemplate()

...
<block type="core/template" name="order_totals_wrapper" as="order_totals" template="email/order/totals/wrapper.phtml">
    <block type="sales/order_totals" name="order_totals" helper="Emailbygroup/Data/getOrderEmailTotalsTemplate">
...

Helper

The helper just checks a condition and returns the template path based on that. I've added a log command to test if the Helper is even being loaded, but it isn't.

Mage::log("getOrderEmailTotalsTemplate is loaded... distributor ".$isDistributor,null,"tax.log");
if($isDistributor) {
    return 'sales/order/totals-discount.phtml';
} else {
    return 'sales/order/totals.phtml';
}

Best Answer

xml file

<block type="core/template" name="order_totals_wrapper" as="order_totals" template="email/order/totals/wrapper.phtml">
    <block type="sales/order_totals" name="order_totals">

Extend sales/order_totals block and add the following

protected function _construct()
{
    parent::_construct();
    $this->setTemplate(Mage::helper('Emailbygroup')->getOrderEmailTotalsTemplate());
} 

The other way to do this without extending the block is on the xml file,

<block type="core/template" name="order_totals_wrapper" as="order_totals" template="email/order/totals/wrapper.phtml">
    <block type="sales/order_totals" name="order_totals">
        <action method="setTemplate"><template helper="Emailbygroup/Data/getOrderEmailTotalsTemplate"></template></action>
Related Topic