Magento 2 Call Helper Function in phtml

0
272
Magento 2 Call Helper Function in phtml

In this article, you going to see a discussion regarding Magento 2 Call Helper Function in phtml in detail and step by step with a simple code snippet.

If you are looking for this topic then you are at right place.

Let’s start with the topic Magento 2 Call Helper Function in phtml

It is very easy to Call Helper Function in phtml File

Let’s have a look step by step in calling of Helper Function in phtml file.

In case of Magento 2, Class Helper offers many features.

Very useful thing regarding Helper class is that you can call it in controllers, models, views and other helpers.

Use the Helper class always in case of change in Core Functionality of Magento.

It is advisable not to change core file of Magento 2.

Instead of changing core files always try to use helper class.

From the below code snippet you can understand to Call Helper Function in phtml.

Below is the code snippet for Calling directly helper class

 $helper = $this->helper([VendorName]\[ModuleName]\Helper\Data);
 $values = $helper->HelperMethod();

Code snippet for Using block class

<?php 
namespace [VendorName]\[ModuleName]\PathtoBlock;

class BlockName extends \Magento\Framework\View\Element\Template
{
     protected $helper;

     public function __construct(
     ....
    [VendorName]\[ModuleName]\Helper\Data $helperData,
    ....
	) {
	    ....
	    $this->helper = $helperData;
	    ....
	}

	public function yourMethod()
	{
	    return $this->helper->methodName();
	}
}

If you want to call block in template file like displaydata.phtml, write $block->yourMethod();

In above code, Block Name class is block that run the template displaydata.phtml file.

After updating code in the file, our next step is execute the following command given below for clear cache.

php bin/magento cache:clean
php bin/magento cache:flush

Now next step is to check in the front-end CMS data get appeared.

Magento 2 call Static Block in phtml

Conclusion:

In this way, Magento 2 Call Helper Function.

I hope you like this article and if you have any query, please comment below.