Magento 2 Render Block in phtml file directly

If you want to display or render the block in .phtml file directly, you can use the following code snippet:
Example: If your block class name is "Magemeta\Test\Block\Test" and you want to render the custom.phtml template file in any other template, you can use the following:

echo $this->getLayout()
          ->createBlock('Magemeta\Test\Block\Test')
          ->setTemplate('Magemeta_Test::custom.phtml')
          ->toHtml();

Hope this helps you!

 

Leave a Reply