Magento 2 – Render Layout Not Working in Custom Module

blockscontrollerslayoutmagento2module

My module was working perfectly in Magento2 Beta version. In stable version, module is getting listed and custom module backend is working fine. However frontend pages of my module are not working. Here is my code.

Index Controller

namespace Companyname\Modulename\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action {
    public function execute() { 
        $this->_view->loadLayout();
        $this->_view->renderLayout();   
    }

}

Layout – /app/code/Companyname/Modulename/view/frontend/layout/modulename_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceContainer name="content">
        <block class="Companyname\Modulename\Block\Modulename" name="modulename" template="Companyname_Modulename::modulename.phtml" />
    </referenceContainer>        
    </body>
</page>

Block File

namespace Companyname\Modulename\Block;
class Modulename extends \Magento\Framework\View\Element\Template {
   public function __construct(
       \Magento\Framework\View\Element\Template\Context $context, 
        array $data = array()
    ) {     
        parent::__construct($context, $data);
   }    
}

When i debug, it does not come in construct of the block. I am assuming that it is not able to find the block. The phtml file simply prints echo "hello world"; But nothing appears on frontend. Any ideas as to what i am doing wrong ?

Best Answer

I think issue in your modulename_index_index.xml file, You have to mention page layout like layout="1column".

You can change your page layout

  • 1column
  • 2columns-left
  • 2columns-right
  • 3columns
Related Topic