How To Call phtml File In Another phtml File In Magento 2

Earlier I posted the solution to call helper function in pHTML in Magento 2 and call CMS static block in pHTML in Magento 2.

However, one of the forum users came up with the requirement to call pHTML file in another pHTML file in Magento 2!

The developers at Meetanshi tried and tested the below code for the solution to call custom pHTML file in another pHTML file. And here I’m sharing it with my blog readers:

Method to Call phtml file in another phtml file in Magento 2:

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login.phtml")->toHtml() ?>

That’s it. You can create a custom tab and call custom Phtml in product UI component form in Magento 2 for developing extensions with custom features.

I’d be grateful if you help me share the solution with the Magento community via social media.

Thank you.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...