magento web developer


Manchester based Magento and Zend Framework certified developer.

Returning HTML with ajax and layout handles

Not so long ago, when I wanted to serve HTML via ajax, my controller action would look something like this:

$myBlock = $this->getLayout()
    ->getBlock('my.block');

$this->getResponse()->setBody(
    $myBlock->toHtml()
);

However I have since discovered a much better way that is not ignorant to layout handles. With a module/controller/method with layout handle mymodule_index_index you can rewrite the node named ‘root’ as follows:

<mymodule_index_index>

    <block type="core/template" name="root" output="toHtml" template="mymodule/index.phtml" />

</mymodule_index_index>