I am having a situation where I need to instantiate a helper object from my custom module in list.phtml file. For this I have two options either I need to overrride productlist.php and pass the helper class in constructor or use object manager. And I am not satisfied from both of the options.. Can anyone suggest me how can I pass this argument in constructor and use it in my phtml file ? I have read a lot about virtual types and type arguments in di.xml Any help in doing this?
2 Answers
You can try using ViewModel
. The view model is injected into the block via layout XML.
You just add an additional XML argument to the Block class - either when creating the block using <block>
or when referring to the block using <referenceBlock>
.
You can go through following articles for more details :
-
1Perfect.. this is what I am looking for. magento do offer us a lot of options the only thing is we don't know where and how to use them :P– VivekJun 22, 2018 at 6:30
Try this:
$_helper = $this->helper('{your custom module Vendor}\{Custome module name}\Helper\{Class name which is in Helper folder}');
$_helper = $this->helper('{your custom module Vendor}\{Custome module name}\Helper\{Class name which is in Helper folder}');
di.xml
file not require.