How To

Magento 2: How to Call phtml File in Helper

Hello Magento Friends,

Hope all are doing well. Today I am going to explain Magento 2: How to Call phtml File in Helper. In case you missed out our previous blog, check it out here. Magento 2: How to Create/Add Custom Cache Type in Cache List.

Magento 2 E-commerce platform allows adding functionalities to your website. But changing the core files of Magento 2 is not a good practice. For this, the Magento helper class is used to override the core files. To add a phtml file from helper then the below code is useful. Let’s look at the steps to Call the phtml File in Helper in Magento 2.

Steps to Call phtml File in Helper in Magento 2:

Step 1: First you need to add Data.php in the following path:

app\code\Vendor\Extension\Helper

Then, add the below code

<?php

namespace Vendor\Extension\Helper;


class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $layoutFactory;

    public function __construct(\Magento\Framework\View\LayoutFactory $layoutFactory)
    {
        $this->layoutFactory = $layoutFactory;
    }

    public function getPhtmlFile()
    {

        $layout = $this->layoutFactory->create();
        $blockOption = $layout->createBlock("Vendor\Extension\Block\Extension")->setTemplate("Vendor_Extension::extension.phtml");
        
       return $blockOption->toHtml();
    }
}

Step 2: Now you need to add the Extension.php file in the following path:

app\code\Vendor\Extension\Block

And add the below code

<?php
namespace Vendor\Extension\Block;

class Extension extends \Magento\Framework\View\Element\Template
{
    public function getDisplayText()
    {
        return "This is display text";
    }   
}

Step 3: After this now you need to add the extension.phtml in the following path:

app\code\Vendor\Extension\view\frontend\templates

And finally, add the below code:

<?php 
 echo $block->getDisplayText();
?>

That’s it!

Conclusion:

Hence, this way you can call phtml file in helper in Magento 2. While implementing the above code, if you come across any hardship, let me know in the comment section. Do share the article further and stay connected for more updates.

Happy Coding!

Click to rate this post!
[Total: 10 Average: 4.8]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Thanks for tutorial. However i try to call the getPhtmlFile from model and got error critical Invalid template file

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

8 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago