Magento 2: Create a Help class and use it in template

In Magento 2, there is a similar help class as magento 1 and can be used in template

Posted on April 10, 2018 in Magento2

Trying to figure out how to create a helper class as Magento 1? You’ll need to create a Data class which extends Magento\Framework\App\Helper\AbstractHelper class , and use the functions defined in it.

Here’s a practical example:

namespace Jeff\MyAttribute\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper {
    public function func() {
        return "this is my helper";

Then in your template you’ll be able to use it like so:

    $myhelper = $this->helper('Jeff\MyAttribute\Helper\Data');
     echo $myhelper->func(); 

comments powered by Disqus