Magento – Dynamically create block from controller

blocksmagento-1.7

I'm creating a popup that loads via ajax default Magento login box.

The default login block template is located in template/persistent/customer/form/login.phtml.

I already have successfully implemented a controller the outputs data for ajax request:

<?php

class My_Module_LoginController extends Mage_Core_Controller_Front_Action {  

    public function loginwindowAction() {

        echo 'Hello world'; // I can see this text in ajax response

        // Now, how to print here login block?
    }

}

What I'm trying to do is just to create block and echo its HTML. Just the block, without the rest of page layout.

Is there any way of doing this without creating page layout xml?

Best Answer

You should be able to do this (block type may not be right so change as needed):

$block = $this->getLayout()->createBlock('customer/form_login')->setTemplate('persistent/customer/form/login.phtml');

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