call-phtml-file-in-magento-2

How To Call Helper Function In Phtml File In Magento 2

If you are working with Magento 2, you may have needed a helper class in your PHTML file. Helpers are a great way to organise your code and keep your logic separate from your presentation. This article will show you how to call a helper in a PHTML file in Magento 2.

Understanding Helpers in Magento 2

Before we dive into how to call a helper in a PHTML file, let’s first understand what helpers are in Magento 2. A helper is a PHP class that contains a set of reusable methods that can be used across the entire application. Helper classes are typically used for tasks that are not directly related to the business logic of the application, but rather for tasks such as formatting data, manipulating URLs, or generating HTML.

Steps to call helper in phtml file in Magento 2

  • Create a helper class in Magento 2
  • Call helper in phtml file in Magento 2

Create a helper class in Magento 2

Before we can call a helper in a PHTML file, we need to create a helper class. To create a helper class in Magento 2, you need to create a PHP file in the Helper directory of your module. In the previous article, we discussed in detail to create a helper in Magento 2.

Code of Helper

<?php

namespace GDBlogger\DemoModule\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function myHelperMethod()
    {
        // Your helper logic here
    }
}

Call helper in phtml file in Magento 2

Now that we have created our helper class, we can call it in a PHTML file. Create a phtml file and paste the following code. In the previous article, we discussed in detail to create a template and phtml file in Magento 2.

Code of phtml file

<?php
$helper = $this->helper(GDBlogger\DemoModule\Helper\Data');
echo $helper->myHelperMethod();

In this example, we are calling the myHelperMethod() method of our Data helper class.

Additionally

Using a Helper in a Block or Controller In Magento 2

In addition to using helpers in PHTML files, you can also use them in blocks or controllers. To use a helper in a block or controller, you can inject the helper class into the constructor of the block or controller using dependency injection. Here is an example of injecting a helper into a block:

Code of Block file

<?php

namespace GDBlogger\DemoModule\Block;

use GDBlogger\DemoModule\Helper\Data;

class MyBlock extends \Magento\Framework\View\Element\Template
{
    protected $_helper;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        Data $helper,
        array $data = []
    ) {
        $this->_helper = $helper;
        parent::__construct($context, $data);
    }

    public function myBlockMethod()
    {
        return $this->_helper->myHelperMethod();
    }
}

Conclusion

Helpers are a powerful tool in Magento 2 that can help you keep your code organized and maintainable. In this article, we have shown you how to call a helper in a PHTML file, block, or controller. By using helpers effectively in your Magento 2 application, you can simplify your code and make it easier to maintain over time.

FAQs

What is a helper class in Magento 2?

A helper class is a PHP class that contains a set of reusable methods that can be used across the entire application in Magento 2.

Why should I use a helper class in Magento 2?

Helper classes are used in Magento 2 to organize your code and keep your logic separate from your presentation. They can be particularly helpful for tasks that are not directly related to the business logic of your application, such as formatting data, manipulating URLs, or generating HTML. By using helper classes, you can simplify your code and make it easier to maintain over time. Additionally, helper classes can be reused across different parts of your application, which can save you time and effort in the long run.

You may also know,

Create Controller in Magento 2

CRUD Model In Magento 2

How To Create A Unit Test File In Magento 2

How to Create UI Component Grid and Form in Magento 2

Leave a comment

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