1

As I understand it, the \Magento\Framework\View\Result\PageFactory class is responsible for keeping an array of layout handles. According to this tutorial there are 3 types of handles. You can add handles programatically by calling $page->addPageLayoutHandles() or $page->addHandle() (which is looks like addPageLayoutHandles just adds them through addHandle anyway).

Here is what I have

app/code/module/Social/Controller/Index/Index.php

<?php

namespace module\Social\Controller\Index;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action 
{

    protected $_resultPageFactory;

    /**
     * Constructor
     * @param Context                                    $context           [description]
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory [description]
     */
    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory) 
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute() 
    {
        $page = $this->_resultPageFactory->create();

        $page->addPageLayoutHandles(['one' => 'one', 'two' => 'two']);

        return $page;
    }
}

app/code/module/Social/view/frontend/layout/social_index_index.xml

<?xml version="1.0"?>

<page layout="4columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="mycontainer">
            <block class="module\Social\Block\Social" name="social" template="socialmedia.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/module/Social/view/frontend/layout/social_index_index_one_one.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="mycontainer">
            <block class="module\Social\Block\Social" name="social" template="socialmediaone.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/module/Social/view/frontend/layout/social_index_index_two_two.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="mycontainer">
            <block class="module\Social\Block\Social" name="social" template="socialmediatwo.phtml" />
        </referenceContainer>
    </body>
</page>

Output

Now, if I hit the page at http://mylocal/social/index/index, Magento calls my controller because I have it in the path for the action. When it hits that controller I am adding social_index_index_one_one and social_index_index_two_two to the array. Here are all of my questions.

  • How does this array of handles work?
  • Why is it that the last handle in the array is the template that gets output?
  • What is the point in adding other template handles then?
  • If I was to add sku or id handles what is the point of that?

The documentation on handles is pretty bad, and I can not find any explanation on the point of these handles in any detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.