Magento 2 – How to Call Custom Block in PHTML File

custom blockmagento2

I have created a custom block page. I want to call custom block to another phtml file.

When I called custom block in xml file is working.

But when I called custom block in phtml file is not working.

<?php echo $block->getLayout()
    ->createBlock('Codism\Csr\Block\Index\TopMenu')
    ->setBlockId('Codism_Csr::menu.phtml')
    ->toHtml();
?>

Best Answer

Call phtml like this below way :

<?php echo $block->getLayout()
    ->createBlock('Codism\Csr\Block\Index\TopMenu')
    ->setTemplate('Codism_Csr::menu.phtml')
    ->toHtml();
?>

Or

<?php echo $this->getLayout()
    ->createBlock('Codism\Csr\Block\Index\TopMenu')
    ->setTemplate('Codism_Csr::menu.phtml')
    ->toHtml();
?>