cancel
Showing results for 
Search instead for 
Did you mean: 

call block in observers Magento2

call block in observers Magento2

How to call block in observers Magento2?

3 REPLIES

Re: call block in observers Magento2

Do you want to access a block thats in the layout? Or can you specify your question.

Re: call block in observers Magento2

Yes i want to access a block through observer that is in the layout.

Re: call block in observers Magento2

@Pooja_8

public function __construct(\Magento\Framework\View\LayoutInterface $layout)
{
  $this->layout = $layout;
}

public function execute()
{
	$block = $this->layout->createBlock('NameSpace\ModuleName\Block\ListProduct')->setTemplate('NameSpace_ModuleName::category/view.phtml')->toHtml();
	echo "<prE>";
	print_R($block);
	exit;
}