0
 $block = $this->getLayout()
        ->createBlock('Magento\Framework\View\Element\Template')
        ->setTemplate('MyModule_ProductTile::widget\test.phtml');


 $this->getLayout()->getBlock('content')->append($block);

$this->getLayout()->getBlock('content') returns false, I found a suggestion to use $this->loadLayout() but I get an invalid error function

  • have you put that code in controller? – kunj Aug 21 '18 at 13:15
  • It is in the constructor of a widget public function __construct( Context $context, Json $serializer, array $data = [] ) { parent::__construct($context, $data); $block = $this->getLayout() ->createBlock('Magento\Framework\View\Element\Template') ->setTemplate('MyModule_ProductTile::widget\test.phtml'); // var_dump( $this->getLayout()); $this->getLayout()->getBlock('content')->append($block); } – Borche Glafche Aug 21 '18 at 13:16
0

You can set something like below code:

protected $_resultPageFactory;

public function __construct(
    Context $context,
    Magento\Framework\View\Result\PageFactory $resultPageFactory

) {
    $this->_resultPageFactory = $resultPageFactory;
    parent::__construct($context);

}

public function execute()
{

    $resultPage = $this->_resultPageFactory->create();
    $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

    $block = $resultPage->getLayout()
            ->createBlock('Companyname\Module\Block\blockname')
            ->setTemplate('MyModule_ProductTile::widget\test.phtml')
            ->toHtml();
    $this->getResponse()->setBody($block);
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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