Magento 2 - Call phtml in helper
I have figured out the solution. Copied from gift-card module (Enterprise)
For variables in constructor
protected $_layout;
public function __construct(
...
\Magento\Framework\View\LayoutInterface $layout
...
) {
$this->_layout = $layout;
...
}
Now in any of the function where you want to call phtml, use this:
$block = $this->_layout->createBlock(\Vendor\Module\Block\Index::class);
$block->setTemplate("Vendor_Module::index.phtml");
return $block;
You can use this:
$block = $this->_layout->createBlock(\Vendor\Module\Block\Index::class);
$block->setTemplate("Vendor_Module::index.phtml");
return $block;
Where $this->_layout
is the instance of \Magento\Framework\View\LayoutInterface