1

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 */

1 Answer 1

7

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());
3
  • I am trying to do this on ajax request. If I do as you mentioned above, the responseText is coming blank..
    – Mr_Green
    Dec 4, 2013 at 9:04
  • Hey wait I forgot adding echo. working fine now.. thanks :)
    – Mr_Green
    Dec 4, 2013 at 9:06
  • 2
    @Mr_Green I've updated the answer with the best practice for ajax calls.
    – Marius
    Dec 4, 2013 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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