Magento2

How to call phtml file in Magento 2

Written by Prashant Dhaduk

Mar 15

How to call phtml file in Magento 2

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.

  • Now you should create a test.phtml file at app/design/frontend/{Package}/{theme}/Magento_Theme/templates/html/ and add your code to this file those you want.
Now we can call the newly created phtml file from a layout XML, blocks, and CMS pages and another phtml file.

Calling phtml file in XML layout

  • You can create a layout file to call the phtml file into the home page. create a layout file called “cms_index_index.xml” in your theme. Insert the code below into the layout file.
<?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>

Calling phtml file in blocks and cms pages

  • If you’re creating a static block or static page and want to call a phtml file into the CMS block or CMS page, copy the code below and add it to the block or page.
{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}

Calling phtml file in another phtml file

  • If you need to call a custom PHTML file into another PHTML file, copy the below code, give a path to your newly created PHTML file into the setTemplate argument, and paste the code into another PHTML file that you want.
<?= $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.

Written by Prashant Dhaduk

Magento Store

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

TO TOP