Layout – How to Render a Particular Block Instead of Complete Layout in Magento 1.8

layoutmagento-1.8render

If we call the below code in a controller, it renders the complete layout handle related to the controller.

$this->loadLayout();
$this->renderLayout();

But what I want is to render only a particular block from the layout handle.

Lets say if my layout handle has a child block with name "hello", I want to render that particular block. How to do this?

I tried the below code:

    $this->loadLayout();
    $layout = $this->getLayout();
    $block = $layout->getBlock("hello");
    $block->renderLayout(); /* Error: 503 Service Unavailable */

Best Answer

Try like this:

$this->loadLayout();
$layout = $this->getLayout();
$block = $layout->getBlock("hello");
echo $block->toHtml();

For ajax requests do this:

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