15

How to show a block content on homepage?

After install magento2 without sample data, I chose 'Magento Luma' theme and add 'Home Page Block' block and copy the code from the sample store.

but nothing show up on the home page, how to show my cms block on home page like sample store?

my home enter image description here

sample store

enter image description here

4
  • You mean showing custom cms block text/content or admin panel created cms block text/content?
    – Bojjaiah
    Commented Dec 31, 2015 at 4:45
  • @magentotwo I create a custom cms block I want to know how to show this blcok on home page
    – Yohan
    Commented Dec 31, 2015 at 5:03
  • have you tried below solution?
    – Bojjaiah
    Commented Dec 31, 2015 at 5:39
  • The home page content in sample data is coming from widgets.
    – user36793
    Commented Feb 26, 2016 at 9:20

3 Answers 3

51
+50

in admin panel go to Content->Pages and edit home page and if you created your static block from admin panel add following to the Content tab

{{block class="Magento\Cms\Block\Block" block_id="your_block_identifier"}}

and if you created a custom block using a module add following to home page content tab

{{block class="Vendor\ModuleName\Block\BlockName" template="Vendor_ModuleName::yourcustomblock.phtml"}} 

then save the changes and flush magento cache and deploy/delete static contents now refresh home page .

If this was not worked let me know

5
  • It works like a charm! Thanks a lot Chamal!
    – Yohan
    Commented Dec 31, 2015 at 6:15
  • Is there any link I can learn more about this? magento block class and block id code for front-end?
    – Yohan
    Commented Dec 31, 2015 at 6:16
  • 1
    I found devdocs.magento.com/guides/v2.0/howdoi/frontend/… this is a good guide for understanding magento 2 block architecture Commented Dec 31, 2015 at 6:24
  • From my module's cms_index_index.xml, I added this in Home Page content - <block class="Vendor\Module\Block\Index" name="modulename" template="module.phtml"></block> and It says Error filtering template: Invalid template file: 'Vendor_Module::module.phtml' in module: 'Vendor_Module' block's name: 'index_0'`` Commented Oct 18, 2017 at 10:32
  • ifconfig is not working with this approach. Any idea ?
    – 26vivek
    Commented Aug 20, 2020 at 9:59
10

try this or else post your code then we will suggest you where you went wrong.

Block file

<?php
namespace Learning\SmartSearch\Block;

class SmartSearch extends \Magento\Framework\View\Element\Template
{
    public function getTitle()
    {
        return "Smart Search";
    }
}

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="Learning_SmartSearch" setup_version="1.0.0">
    </module>
</config>

view/layout/cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Learning\SmartSearch\Block\SmartSearch" template="Learning_SmartSearch::html/cms.phtml"/>
        </referenceContainer>
    </body>
</page>

view/templates/html/cms.phtml

<h1><?php echo $block->getTitle(); ?></h1>

after creating flush cache.

I hope this will help you. If you are not getting results let me know.

1
  • Thank you for your answer! I will try later and let you know the result
    – Yohan
    Commented Dec 31, 2015 at 5:47
3

In the home page you can call the static block like the below mentioned way

{{block class="Magento\Cms\Block\Block" block_id="your_block_identifier"}}

In the html page you can call the static block like the below mentioned way.

<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>

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