How to call phtml file in Magento 2

Written by Prashant Dhaduk

Mar 15, 2023

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="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/>

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.

Prashant Dhaduk


We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP