Reading list Switch to dark mode

    How to override template using helper function in Magento2

    Updated 19 April 2023

    In this guide, we will learn how can we call our custom template file in layout referenceBlock from a helper function.

    1. Add the below code in your layout file:
    <referenceBlock name="test_block">
                <action method="setTemplate">
                    <argument name="template" xsi:type="helper" helper="Webkul\Test\Helper\Data::getTemplate"></argument>
    
                </action>
              
     </referenceBlock>

    2. Create getTemplate function in your helper function.

    public function getTemplate()
    {
            
         $template =  'Webkul_Test::test.phtml';
            
         return $template;
    }

    We hope it will help you. Thank you!!

    If any issue or doubt please feel free to mention in comment section.

    We would be happy to help. Happy Coding!!

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home