cancel
Showing results for 
Search instead for 
Did you mean: 

how to create a new custom page layout magento 2

how to create a new custom page layout magento 2

Can anyone tell me how can I create a new custom page layout magento 2

2 REPLIES 2

Re: how to create a new custom page layout magento 2

Hi @johdanlusefa34 

 

Please follow the steps below given URL :

 

https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/layout-create.html

Hope this helps you!
Problem Solved! Click Kudos & Accept as Solution!

Re: how to create a new custom page layout magento 2

Hi @johdanlusefa34 

 

So at first, we will create a layout file which is to be loaded from other controller under app/code/Namespace/Module/view/frontend/layout/ , let’s say module_custom_customlayout.xml Now write the following code in this file.

<?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="Namespace\Module\Block\Custom" name="custom_layout_load" template="Namespace_Module::custom.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

Create custom.phtml under below path:
app/code/Namespace/Module/view/frontend/templates/

Create controller :
app/code/Namespace/Module/Controller/ Customer.php

<?php
namespace Namespace\Module\Controller\Custom;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Custom extends Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * [__construct]
     * @param Context                          $context
     * @param PageFactory                      $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
    ) {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct(
            $context
        );
    }

    /**
     * loads custom layout
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
       $resultPage = $this->_resultPageFactory->create();
       $resultPage->addHandle('module_custom_customlayout'); //loads the layout of module_custom_customlayout.xml file with its name
       return $resultPage;
    }
}


Above code will load the file app/code/Namespace/Module/view/frontend/templates/custom.phtml  and then prints the string written in this file at frontend.

 

It may help you!
Problem Solved? Please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!