---
title: "How to call phtml file in Magento 2"
url: "https://dolphinwebsolution.com/how-to-call-phtml-file-in-magento-2"
date: "2023-03-15T13:05:39+05:30"
modified: "2023-08-28T06:58:07+05:30"
author:
  name: "Prashant Dhaduk"
categories:
  - "Magento2"
word_count: 397
reading_time: "2 min read"
summary: "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..."
description: "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 i..."
keywords: "Magento2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Reasons to Switch Hyvä Theme for Magento 2"
    url: "https://dolphinwebsolution.com/blog/reasons-to-switch-hyva-theme-for-magento-2/"
  - title: "Key Benefits Of Hyva Theme"
    url: "https://dolphinwebsolution.com/blog/benefits-of-hyva-theme/"
  - title: "What to expect from the new release of Magento 2.4.6"
    url: "https://dolphinwebsolution.com/what-to-expect-from-the-new-release-of-magento-2-4-6"
---

# How to call phtml file in Magento 2

_Published: March 15, 2023_  
_Author: Prashant Dhaduk_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/03/phtml-magento-1024x536.png)

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](https://dolphinwebsolution.com/magento-2-development) 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.

</body>


---

_View the original post at: [https://dolphinwebsolution.com/how-to-call-phtml-file-in-magento-2](https://dolphinwebsolution.com/how-to-call-phtml-file-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 07:13:14 UTC_  
