1

I have created a new custom page layout in Magento 2.

Here is the code:

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
    <update handle="empty"/>
    <referenceContainer name="page.wrapper">
        <container name="header.container" as="header_container" label="Page Header Container"  htmlTag="header" htmlClass="page-header" before="main.content"/>
        <container name="page.top" as="page_top" label="After Page Header" after="header.container"/>
        <container name="footer-container" as="footer" before="before.body.end" label="Page Footer Container" htmlTag="footer" htmlClass="page-footer" />
    </referenceContainer>
</layout>

I have also created a Block in the backend.

My question is...How can I call the block content into my layout, in other words I want to reference the Block I made in my Layout so that the content of my block will be should in this layout.

Have can I do that?

Tried this - Suggested by krishna:

<referenceContainer name="content">
        <block class="Magento\Cms\Block\Block" name="topmostMessage">
            <arguments>
                <argument name="topmostMessage" xsi:type="string">quickblock</argument>
            </arguments>
        </block>
</referenceContainer>
<referenceContainer name="page.wrapper">
    <container name="header.container" as="header_container" label="Page Header Container"  htmlTag="header" htmlClass="page-header" before="main.content"/>
    <container name="page.top" as="page_top" label="After Page Header" after="header.container"/>
    <container name="footer-container" as="footer" before="before.body.end" label="Page Footer Container" htmlTag="footer" htmlClass="page-footer" />
</referenceContainer>

6

You can call the static block in layout as described.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceContainer name="content">
                <block class="Magento\Cms\Block\Block" name="quick-block">
                    <arguments>
                        <argument name="block_id" xsi:type="string">quickblock</argument>
                    </arguments>
                </block>
    </referenceContainer>
    </body>
</page>

Change quickblock with your static-block identifier.

I have created a small working example

mahento/app/code/Vendor/Module/registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

magento/app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>

magento/app/code/Vendor/Module/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="module" frontName="module">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

/var/www/m5agento207/app/code/Vendor/Module/Controller/Index/Index.php

<?php
/**
 *
 * Copyright © 2015 Vendorcommerce. All rights reserved.
 */
namespace Vendor\Module\Controller\Index;

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

    /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Cache\StateInterface $cacheState,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        $this->resultPage = $this->resultPageFactory->create();  
        return $this->resultPage;

    }
}

magento/app/code/Vendor/Module/view/frontend/layout/module_index_index.xml

In my case i have created CMS block with test3 identifier and placed in front end layout. Make sure you should replace by your block name.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
     <body>     
    <!--        <block class="Vendor\Module\Block\Index\Index" name="index_index" template="index/index.phtml">
            </block>  -->

    <referenceContainer name="content">
                <block class="Magento\Cms\Block\Block" name="quick-block">
                    <arguments>
                        <argument name="block_id" xsi:type="string">test3</argument>
                    </arguments>
                </block>
    </referenceContainer>
    </body>
</page>

Run

php magento setup:upgrade

php magento cache:clean

Run url in Browser

http://127.0.0.1/magento/module

| improve this answer | |
  • OK, I've tried it and changed the ID but it's not showing...I'd added the code to the question as it's too long to paste here – user159500 Sep 4 '16 at 13:51
  • use <page tag with layout="2columns-left" and paste the reference container inside the body, try it one more time by removing cache..) – Krishna ijjada Sep 4 '16 at 14:29
  • Check the updated answer.and make sure your module working fine. – Krishna ijjada Sep 4 '16 at 14:31
  • OK, I've changed it but now it's only showing container.phtml file but not the Block content ... I'm working localhost ... is there anything else I need to do? – user159500 Sep 4 '16 at 14:46
  • Please check the working example added to answer. – Krishna ijjada Sep 4 '16 at 16:21

Your Answer

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

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