1

I got asnwer of how to call it in Magento 1, but in M2 i am getting an error.

if($product->getTest()){
        $productid = $product->getId();
        return Mage::app()->getLayout()->createBlock('core/template')->setTemplate('demo/test/test.phtml');
    }

Please Help.

2 Answers 2

2

try this code

protected $layoutFactory;

public function __construct(
    ...................................
    \Magento\Framework\View\LayoutFactory $layoutFactory,
    ...................................
) {
    ...................................
    $this->layoutFactory = $layoutFactory;
    ...................................
}

public function getPHtmlFile()
{

    $layout = $this->layoutFactory->create();
    $blockOption = $layout->createBlock("Your Block path")->setTemplate('Vendorename_Modulename::Yourphtmlfile.phtml')
    return $blockOption->toHtml();
}
0
0
...
protected $_resultPageFactory;
...
public function __construct(
   ...
   \Magento\Framework\View\Result\PageFactory $resultPageFactory,
   ...
) {
   ...
   $this->_resultPageFactory = $resultPageFactory;
   ...
}

//method
public function getStaticBlock()
   {
       $resultPage = $this->_resultPageFactory->create();
       return $resultPage->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Vendor_Module::example.phtml")->toHtml();
   }

Happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.