---
title: "How to override template file using helper function in Magento2"
url: "https://dolphinwebsolution.com/how-to-override-template-file-using-helper-function-in-magento2"
date: "2023-07-22T12:43:37+05:30"
modified: "2023-08-29T06:48:51+05:30"
author:
  name: "Jigar Patel"
categories:
  - "Magento"
  - "Magento2"
word_count: 436
reading_time: "3 min read"
summary: "In Magento 2, you can override a template file using a helper function by dynamically specifying the custom template path in your custom helper class. This approach allows you to change the templat..."
description: "In Magento 2, you can override a template file using a helper function by dynamically specifying the custom template path in your custom helper class. This a..."
keywords: "template file,helper function,xsi:type=helper, Magento, Magento2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How To Add Custom Button In System Configurations Magento 2"
    url: "https://dolphinwebsolution.com/how-to-add-custom-button-in-system-configurations-magento-2"
  - title: "Magento vs. Shopware  vs. Shopify vs. WooCommerce: Core Differences"
    url: "https://dolphinwebsolution.com/blog/magento-vs-shopware-vs-shopify-wocommerce/"
  - title: "How To Add Custom Attribute  Data To Recently Viewed Products Widget"
    url: "https://dolphinwebsolution.com/how-to-add-custom-attribute-data-to-recently-viewed-products-widget"
---

# How to override template file using helper function in Magento2

_Published: July 22, 2023_  
_Author: Jigar Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/07/override-template.png)

In Magento 2, you can override a template file using a helper function by dynamically specifying the custom template path in your custom helper class. This approach allows you to change the template file for a specific block without modifying the core block class.

### Step 1: Create a layout xml file
Create **dolphin\_index\_blog.xml** in **app/code/Dolphin/Customize/view/frontend/layout** directory with the following code.

```
<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceContainer name="content">
            <block name="index.blog" class="Dolphin\Customize\Block\Index\Index">
                <action method="setTemplate">
                    <argument name="template" xsi:type="helper" helper="Dolphin\Customize\Helper\Data::getTemplate"></argument>
                </action>
            </block>
        </referenceContainer>
    </body>
</page>
```

The *helper* can use only public methods. In this example the **`someMethod()`** method should be public. The argument with *helper* type can contain `param` items which can be passed as a helper method parameters.

we use the **`<action>`** node to call the **`setTemplate`** method of the block with the alias **“index.blog”** We pass the path to the custom template file obtained from the helper function as an argument using **`xsi:type="helper"`**. This tells Magento to dynamically fetch the template path using the specified helper method.

### Step 2: Create helper class
Create **Data.php** in **app/code/Dolphin/Customize/Helper** directory with the following code.

```
<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Dolphin\Customize\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context
    ) {
        parent::__construct($context);
    }

    public function getTemplate()
    {

        $template =  'Dolphin_Customize::index/blog.phtml';

        return $template;
    }
}
```

### Step 3: Create a templates file
Create **blog.phtml** in **app/code/Dolphin/Customize/view/frontend/templates/index** directory with the following code.

```
<?php
/**
 * dolphin index template
 *
 * @var $block \Dolphin\Customize\Block\Index\Index
 */
?>

<h2>blog templates</h2>
```

Now execute the below command and go to the system configuration page:

**php bin/magento s:up**
**php bin/magento s:d:c**
**php bin/magento s:s:d -f**
**php bin/magento c:c**
#### Result:-

![Best AI Agent Development Companies in India (2026)](https://dolphinwebsolution.com/wp-content/uploads/2023/07/magento246-com-dolphin-index-blog.png)

I Hope, This instruction will be helpful for you.

If you have any difficulties regarding this blog, do consider posting them in the Comments section below!

I’m here to help.

**Thank you!**

</body>


---

_View the original post at: [https://dolphinwebsolution.com/how-to-override-template-file-using-helper-function-in-magento2](https://dolphinwebsolution.com/how-to-override-template-file-using-helper-function-in-magento2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:14:15 UTC_  
