How to call the helper method in phtml template file?

A Helper is a class that provides functionalities for various features throughout the Magento website. Available for use anywhere on the website, Magento 2 Helper can be called in controllers, views, models and even in other helpers too!

Here, I will show you how to call a helper class methods in the template files. There’s a small tip for you, we suggest you use the second method.

Method 1: Using $this->helper directly in the template files

In Magento 2 templates there is an object: $this. It is an object of the class. Check below how to call the helper method using $this.

$this->helper('[Vendor]\[Module]\Helper\[Helper Name]')

Use this method as a last resort because as far as we know, calling a helper in your template is equal to using the object manager directly. Thus, we should inject the helper in the constructor of the block. See method 2 given below.

Method 2: By injecting helper in the  constructor of the block class

This is the best practice for using helper in Magento 2. Helper method can be called anywhere in Magento 2 using Dependency Injection. You should not call the helper directly in the template.

To use this helper method, check the below code snippet where you can see the helper instance is dependent on the block,  that renders the template and creates a method in your block, which then calls the helper and calls that method in the template.

Define your block as shown below:

protected $helper;
public function __construct(
    \{Vendor}\{Module}\Helper\Data $helper,
) {
    $this->helper = $helper;

public function callHelperMethod()
    return $this->helper->getDataFromHelper();

Then you can call the helper in your template with : $block->callHelperMethod()


In this article, I’ve shown you how to call the helper method in .phtml template files. If you still need any help regarding the helper class or Magento, we are happy to help you. Feel free to get in touch!

  • Share :