1

I want to send block html in JSON response.

To be specific - I want to add functionality where customers are able to edit their addresses via Ajax requests. So the block class will be \Magento\Customer\Block\Address\Edit instance.

In Magento 1.x it will be just something like that: $this->getLayout()->createBlock('xxx')->toHtml()

How to achieve similar things in Magento 2 ?

Am I doing it in proper way? Or maybe Magento 2 implements other ways of doing such things?

Khoa Truong
32.5k11 gold badges91 silver badges159 bronze badges
asked Oct 26, 2016 at 10:17

2 Answers 2

2

-- \Magento\Framework\View\LayoutFactory to create the block.
-- \Magento\Framework\Controller\Result\RawFactory to return Json format.

/**
 * @var \Magento\Framework\View\LayoutFactory
 */
protected $layoutFactory;
/**
 * @var \Magento\Framework\Controller\Result\RawFactory
 */
protected $resultRawFactory;
public function __construct(
 \Magento\Framework\App\Action\Context;
 \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
 \Magento\Framework\View\LayoutFactory $layoutFactory
 .....
) {
 $this->resultRawFactory = $resultRawFactory; 
 $this->layoutFactory = $layoutFactory;
 parent::__construct($context);
}
public function execute() {
$layout = $this->layoutFactory->create();
$block = $layout->getLayout()
 ->createBlock('\Magento\Customer\Block\Address\Edit')
 ->setTemplate('Magento_Customer::address/edit.phtml')
 ->toHtml();
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
$resultRaw->setContents($block);
//$this->getResponse()->setBody($block); <= We also can use setBody.
return $resultRaw;
}
answered Oct 26, 2016 at 16:05
1
  • 1
    You need to remove ->getLayout(), since you already have a layout. Commented Feb 15, 2018 at 10:32
0

I was getting Interceptor error with Khoa solution , here is what worked for me:

 protected $resultPageFactory;
/**
 * @var \Magento\Framework\Controller\Result\RawFactory
 */
protected $resultRawFactory;
public function __construct(
 Context $context,
 \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
 \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
 $this->resultRawFactory = $resultRawFactory; 
 $this->resultPageFactory = $resultPageFactory;
 parent::__construct($context);
}
public function execute() {
 $layout = $this->resultPageFactory->create();
 $block = $layout->getLayout()->createBlock('Custom\Module\Block\Active')->setTemplate('Custom_Module::socialsignup/block.phtml')->toHtml();
 /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
 $resultRaw = $this->resultRawFactory->create();
 $resultRaw->setContents($block);
 //$this->getResponse()->setBody($block); <= We also can use setBody.
 return $resultRaw;
}
answered Oct 31, 2017 at 7:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.