In this article, we will explain how to call a phtml file in Magento 2. In this, we cover how to create a phtml file and different ways to call phtml files in Magento 2.
Before calling the phtml file, you must create a phtml file in Magento 2.
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/> </referenceContainer> </body> </page>
{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}
<?= $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::html/test.phtml")->toHtml(); ?>
That’s it! I hope this technical blog will help you find what you were looking for.
If you have any further questions about the phtml file in Magento 2 or if you need any help from our Magento 2 expert, contact us now for a free consultation.
Bookmark it for your future reference. Do comment below if you have any other questions.
P.S. Do share this note with your team.