1

I have a simple module Alpine_AlpineCustom but it seems that my layout.xml file is not being applied properly and I just can't see why.

I can see via Commercebug that the layout file alpinecustom_index_index.xml is seen, but when I call my page it I see a "1column" page with "Create New Customer Account" and not my content. So either my layout is either not working, working wrong, or is ignored for this path block.

My router, controller, template seem to work properly because if I put $block->setTemplate('Alpine_AlpineCustom::alpine.phtml'); in my execute function things show as expected without error.

It must be something simple and obvious between the block and the layout, a path or name mismatch but I just cannot see it. It is the right block name and template reference since they work elsewhere.

What am I not seeing?

Bonus question: why is it showing "Create New Customer Account"?

Alpine\AlpineCustom\view\frontend\layout\alpinecustom_index_index.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <title>Layout Alive</title>
</head>
<body>
    <referenceContainer name="content">
        <block class="Alpine\AlpineCustom\Block\Index" name="alpinecatalog" template="Alpine_AlpineCustom::alpine.phtml" />
    </referenceContainer>
</body>
</page>

Alpine\AlpineCustom\Block\Index.php

<?php 

namespace Alpine\AlpineCustom\Block;
class Index extends \Magento\Framework\View\Element\Template 
{

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,   
        array $data = []
    )
    {
        parent::__construct($context, $data);
    }


    protected function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getAlive()
    {
        return 'Module Index Block ALIVE';
    }

}
?>

Alpine\AlpineCustom\Controller\Index\Index.php

<?php
namespace Alpine\AlpineCustom\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    /**
    * @var \Magento\Framework\View\Result\PageFactory
    */
    protected $resultPageFactory;
    /**
    * @param \Magento\Framework\App\Action\Context $context
    * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
    */
    public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {

        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        //$block = $this->_view->getLayout()->createBlock('Alpine\AlpineCustom\Block\Index');
        //$block->setTemplate('Alpine_AlpineCustom::alpine.phtml');
        //$this->getResponse()->appendBody($block->toHtml());
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();

        return $resultPage;        
    }
}
?>

Alpine\AlpineCustom\etc\frontend\routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
    <route id="alpinecatalog" frontName="alpinecatalog">
        <module name="Alpine_AlpineCustom" />
    </route>
</router>
</config>

1 Answer 1

1

app/code/Alpine/AlpineCustom/view/frontend/layout/alpinecatalog_index_index.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Layout Alive</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Alpine\AlpineCustom\Block\Index" name="alpinecatalog" template="Alpine_AlpineCustom::alpine.phtml" />
        </referenceContainer>
    </body>
</page>

Note :

Layout Router = {front-name}/{controller-name}/{action-name}

4
  • I can't spot the difference, what am I missing? aha, the name.
    – iphigenie
    Commented Oct 7, 2019 at 14:15
  • so the layout blocks are per route not module - lets see if I can make it work
    – iphigenie
    Commented Oct 7, 2019 at 14:24
  • It still now showing my content in template as expecte. BUT it is still additionally showing me the header "Create New Customer Account" which I have no idea where it is coming from, any idea? Surely that cannot be the default title?
    – iphigenie
    Commented Oct 7, 2019 at 23:28
  • I think it's inherited from the theme
    – PЯINCƎ
    Commented Oct 8, 2019 at 7:51

Your Answer

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

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