Meet Us at Dubai - Gitex Global 2023 For The Exponential Growth Of Your Business!! Schedule a Meeting.

How to override template file using helper function in Magento2

Written by Jigar Patel

Jul 22, 2023

How to override template file using helper function in Magento2

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="" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block name="" class="Dolphin\Customize\Block\Index\Index">
                <action method="setTemplate">
                    <argument name="template" xsi:type="helper" helper="Dolphin\Customize\Helper\Data::getTemplate"></argument>

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 “” 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.

 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.

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
    ) {

    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.

 * 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



magento246 com dolphin index blog


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!

Jigar Patel


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