2

It's my execute method from controller:

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

It's my layout inside the <page>:

<body>
    <referenceContainer name="content">
        <block
            class="Magento\Framework\View\Element\Template"
            name="printshipment_index_index"
            template="Module_PrintShipment::print_shipment.phtml"/>
    </referenceContainer>
</body>

In this case I have default Magento layout with header, footer etc. And I need fully empty page. I have tried to add

<update handle="empty"/>

but then my block doesn't appear. What can you advise me?

6 Answers 6

2

There is no content container in an empty layout by default. Try this

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="empty" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="root">
            <block class="Magento\Framework\View\Element\Template" name="printshipment_index_index" template="Module_PrintShipment::print_shipment.phtml" />
        </referenceContainer>
    </body>
</page>
1
  • As of Magento 2.4.2 an empty layout page can come with 150+ useless dependencies/extra network requests (if no merging/bundling is enabled). =/ Jul 30, 2021 at 4:40
2

In your Controller Class, you need to add "true" when creating your factory under the "execute" function.

To assist you further; I include my code below to help writing this Class:

namespace Vendor\Module\Controller\Index;

class View extends \Magento\Framework\App\Action\Action
{
    protected $PageFactory;

    public function __construct
    (
          \Magento\Framework\App\Action\Context $context
        , \Magento\Framework\Json\EncoderInterface $Encoder
        , \Magento\Framework\View\Result\PageFactory $PageFactory
    )
    {
        $this->PageFactory = $PageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
       $resultPage = $this->PageFactory->create(true);
       return $resultPage;
    }
}

Notice the (true) bit here to do this:

$resultPage = $this->PageFactory->create(true);

Works for me, I hope this helps.

0

If you want empty layout then you can pass this on <page> tag, for example

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="empty" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="printshipment_index_index" template="Module_PrintShipment::print_shipment.phtml" />
        </referenceContainer>
    </body>
</page>
0

Follow default vendor/magento/module-theme/view/frontend/layout/print.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceContainer name="header.container" remove="true"/>
        <referenceBlock name="catalog.topnav" remove="true"/>
        <referenceContainer name="footer-container" remove="true"/>
        <referenceContainer name="before.body.end">
            <block class="Magento\Framework\View\Element\Template"  name="printshipment_index_index"          template="Module_PrintShipment::print_shipment.phtml"/>
        </referenceContainer>
    </body>
</page> 

or you can use simply

<?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">
    <update handle="print" />
    <body>
          <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template"  name="printshipment_index_index"          template="Module_PrintShipment::print_shipment.phtml"/>
          </referenceContainer>
    </body>
</page>
4
  • Can we use layout="empty" ?
    – Keyur Shah
    Dec 27, 2017 at 18:54
  • @KeyurShah yes we can use layout="empty" too. Dec 27, 2017 at 19:01
  • Unfortunately, with your method, as well as with layout="empty", my .phpml file is not rendered(( It is OK when layout isn't empty.
    – Skorzhyk
    Dec 28, 2017 at 9:42
  • @Skorzhyk i checked this code it is working with custom luma and blank theme. Dec 28, 2017 at 10:00
0

To prevent the "default" layout handles being applied you may need to use this in your controller execute function:

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

return $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
0

Similar to other answers here but more thorough

I did it with layout XML

https://github.com/DominicWatts/MaintenancePage/blob/master/view/frontend/layout/maintenance_page_mode.xml

<?xml version="1.0" ?>
<page layout="empty" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <meta name="robots" content="NOINDEX,NOFOLLOW"/>
        <remove src="mage/calendar.css"/>
        <remove src="requirejs/require.js"/>
        <remove src="css/styles-l.css"/>
        <remove src="css/styles-m.css"/>
        <remove src="css/styles-s.css"/>
        <remove src="css/print.css"/>
        <remove src="requirejs/require.js"/>
        <remove src="mage/requirejs/mixins.js"/>
        <remove src="requirejs-config.js"/>
        <remove src="mage/polyfill.js"/>
    </head>
    <body>
        <referenceContainer name="page.wrapper" remove="true"/>
        <referenceBlock name="translate" remove="true"/>
        <referenceBlock name="requirejs-config" remove="true"/>
        <referenceContainer name="root">
            <block class="Xigen\MaintenancePage\Block\Page\Mode" name="page.mode" template="Xigen_MaintenancePage::page/mode.phtml"/>
        </referenceContainer>
    </body>
</page>

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.