In an e-mail template I found this layout handle which is displaying an overview of purchased products:

{{layout handle="sales_email_order_items" order=$order}}

I'm trying to load this layout handle somewhere in an encapsulated environment. But I don't know how to render the content. This is what I have made so far:

require '../app/Mage.php';

ini_set('xdebug.var_display_max_depth', -1);
ini_set('xdebug.var_display_max_children', -1);
ini_set('xdebug.var_display_max_data', -1);

$orderId = 200001234;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

/* Rendering Layout Block, but how?! */
// echo {{layout handle="sales_email_order_items" order=$order}}

This is the XML-Defintion of the layout handle

    <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>email/order/items/order/default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>sales/order_email_items_order_grouped</block><template>email/order/items/order/default.phtml</template></action>
        <block type="sales/order_totals" name="order_totals" template="sales/order/totals.phtml">
            <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>
            <action method="setValueProperties"><value>align="right" style="padding:3px 9px"</value></action>
            <block type="tax/sales_order_tax" name="tax" template="tax/order/tax.phtml">
                <action method="setIsPlaneMode"><value>1</value></action>
    <block type="core/text_list" name="additional.product.info" />
  • That is not going to work there. Layouts are rendered by Magento's layout system, which is not available in your external file. You might need to replicate the logic of those template/block files in your external file. You can also try with making objects of block - template system, but not sure if it will work. – Prateek Jul 1 '16 at 12:38
  • @mohit-kumar-arora: This solution on that page will only work from within a controller action. Any other thoughts? – pbaldauf Jul 1 '16 at 13:03
  • Use $app = Mage::app(); and then replace $this with $app in your code. Please let me know if it worked. I have not checked it. – Mohit Kumar Arora Jul 1 '16 at 13:20

The following script in Magento root folder should do the trick.

require './app/Mage.php';

$order = Mage::getModel('sales/order')->loadByIncrementId(200001234);

$layout = Mage::app()->getLayout();
$update = $layout->getUpdate();



//set block from layout handle <block type="sales/order_email_items" name="items" template="email/order/items.phtml">
$layout->getBlock('content')->setChild('items', $layout->getBlock('items')); 

$block = $layout->getBlock('items')->setData('order', $order );

echo $block->toHtml();
// or echo $layout->getOutput(); for whole layout output

It's important invoking the functions in the right order. Means using the load()-function after addHandle() but before getBlock() calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.