cancel
Showing results for 
Search instead for 
Did you mean: 

How to render block layout from controller in magento 2?

How to render block layout from controller in magento 2?

Hello Friends,

 

I am tring to call block from the controller action but its not working and throwing me an error as

Exception #0 (OutOfBoundsException): No element found with ID 'left'.

 

$this->_addContent($this->_view->getLayout()->createBlock('\Demo\Test\Block\Adminhtml\Method\Edit'));

$this->_addLeft($this->_view->getLayout()->createBlock('\Demo\Test\Block\Adminhtml\Method\Edit\Tabs'));

Can you please tell me how can i achieve above things?

 

Thanks in advance

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.
1 REPLY 1

Re: How to render block layout from controller in magento 2?

Hello SanjayJethva,

 

We are not sure what you mean, but maybe this can help you:

1) For render all layouts you can use:

$this->_view->loadLayout();
$this->_view->renderLayout();

2) Try to run code without "addContent"and "addLeft" methods . Maybe they cause the problem.

 

3) If your methods need html, you can get it using the following:

$this->_view->getLayout()->createBlock('\Demo\Test\Block\Adminhtml\Method\Edit\Tabs')->toHtml();

4) Perhaps you need to use layouts for adding tabs:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
...
</head>
<body>
<referenceContainer name="content">
<block class="Demo\Test\Block\Adminhtml\Method\Edit" name="demotest_method_edit" />
</referenceContainer>
<referenceContainer name="left">
<block class="Demo\Test\Block\Adminhtml\Method\Edit\Tabs" name="demotest_method_edit_tabs">
<block class="Demo\Test\Block\Adminhtml\Method\Edit\Tabs\General" name="demotest_general" />
<action method="addTab">
<argument name="name" xsi:type="string">general_section</argument>
<argument name="block" xsi:type="string">demotest_general</argument>
</action>
</block>
</referenceContainer>
</body>
</page>

Hope it will work for you.

Regards,

Plumrocket Team