In Magento 2, the Helper can be called in controllers, models, views and other helpers. The below code can be used when you want to have helper’s data in .phtml file in Magento 2.
There are two programmatic method to call helper function in phtml in Magento 2 as given below can be helpful to implement custom functionalities and enhance the store features!
- Call helper class directly in .phtml file
$helper = $this->helper(Mageclues\Custom\Helper\Data);
$values = $helper->HelperMethodName();
2. Using block class call helper function in .phtml file
<?php
namespace Mageclues\Custom\Block;
class BlockName extends \Magento\Framework\View\Element\Template
{
protected $helper;
public function __construct(
....
\Mageclues\Custom\Helper\Data $helperData,
....
) {
....
$this->helper = $helperData;
....
}
public function yourMethod()
{
return $this->helper->HelperMethodName();
}
}
Then, you need to call block in template file like view.phtml, write just $block->yourMethod();