Magento: How to call block directly from phtml file without defining in layout?

You can call your block directly from a phtml file with the following code. You can keep this code and call the block from any phtml file.

I have assumed my module name as Newmodule and my block name as Newblock.php. The phtml file for the block is assumed to be newmodule/newblock.phtml


echo $this->getLayout()
          ->createBlock('newmodule/newblock')
          ->setTemplate('newmodule/newblock.phtml')
          ->toHtml();

If you want to call the block from another module or another phtml template file then you can use the following code:


echo $this->getLayout()
          ->getBlockSingleton('newmodule/newblock');

Thanks.