12

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

  • You mean showing custom cms block text/content or admin panel created cms block text/content? – Bojjaiah Dec 31 '15 at 4:45
  • @magentotwo I create a custom cms block I want to know how to show this blcok on home page – Moon Dec 31 '15 at 5:03
  • have you tried below solution? – Bojjaiah Dec 31 '15 at 5:39
  • The home page content in sample data is coming from widgets. – user36793 Feb 26 '16 at 9:20
41
+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

  • It works like a charm! Thanks a lot Chamal! – Moon Dec 31 '15 at 6:15
  • Is there any link I can learn more about this? magento block class and block id code for front-end? – Moon Dec 31 '15 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 – Chamal Chamikara Dec 31 '15 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'`` – Slimshadddyyy Oct 18 '17 at 10:32
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.

  • Thank you for your answer! I will try later and let you know the result – Moon Dec 31 '15 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();?>

protected by Community Jun 18 '16 at 10:27

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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