30

How i include a static block in a page using xml. For example i created a static block with identifier promo. In magento 1 we include a static block using below code

     <block type="cms/block" name="Promo">
          <action method="setBlockId"><block_id>promo</block_id></action>
     </block> 

I want to do exactly same in magento 2

1

8 Answers 8

70
<block class="Magento\Cms\Block\Block" name="Promo">
    <arguments>
        <argument name="block_id" xsi:type="string">promo</argument>
    </arguments>
</block>

This is equivalent to M1.

4
  • @Ben-Space48, agree
    – KAndy
    May 3, 2016 at 16:27
  • Can i add after='-' or before='-' tag in this Feb 11, 2020 at 10:59
  • yes you can @VinitKumar
    – diazwatson
    Feb 29, 2020 at 2:10
  • how to make that block non-cacheable?
    – Manjunath
    Jan 19, 2022 at 7:12
18

Try below code.

Call from Phtml File:

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

Call from cms page or block:

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

Call from Xml File:

<referenceContainer name="content">
    <block class="Magento\Cms\Block\Block" name="block_identifier">
        <arguments>
            <argument name="block_id" xsi:type="string">block_identifier</argument>
        </arguments>
    </block>
</referenceContainer>

Hope this helps you!

2
  • how to make that block non-cacheable
    – Manjunath
    Jan 19, 2022 at 7:09
  • Call from xml file means are you talking about layout.xml file? if yes then where should we create the layout.xml file is it really layout.xml or layouts.xml or we should go with default.xml? Oct 19, 2022 at 11:12
5

Call from Xml File worked. Change the "block_identifier" from the CMS block created on magento dashboard and add it to layout.xml

<referenceContainer name="footer">
       <block class="Magento\Cms\Block\Block" name="block_identifier">
            <arguments>
                <argument name="block_id" xsi:type="string">block_identifier</argument>
            </arguments>
        </block>
    </referenceContainer>
4

Since Magento\Cms\Block\Block class is deprecated, you should use:

<block name="custom_block_name" class="Magento\Cms\Block\BlockByIdentifier">
    <arguments>
        <argument name="identifier" xsi:type="string">custom_block_identifier</argument>
    </arguments>
</block>
3

Try below code:

<block class="Magento\Cms\Block\Block" name="news">
    <arguments>
        <argument name="block_id" xsi:type="string">news</argument>
    </arguments>
</block>

It works for me!Hope this helps!

1
2

static Block Above image you can see my block identifier= "product_view_right_sidebar". add below code to your xml file

<referenceContainer name="content">
    <block class="Magento\Cms\Block\Block" name="product_view_right_sidebar">
        <arguments>
                <argument name="block_id" xsi:type="string">product_view_right_sidebar</argument>
        </arguments>
    </block>
</referenceContainer>
2
  • Can i add after='-' or before='-' tag in this Feb 11, 2020 at 10:54
  • @VinitKumar , yes you can use after=''-" or before="-" tag. <block class="Magento\Cms\Block\Block" name="product_view_right_sidebar" before='-'> Feb 12, 2020 at 6:12
1
 <referenceContainer name="content">
      <block class="Magento\Cms\Block\Block" name="daily_deals" template="bigbazaar.phtml">
        <arguments>
            <argument name="block_id" xsi:type="string">daily_deals</argument>
        </arguments>
    </block>
 </referenceContainer>

Here daily_deals is my CMS block identifier's name. I wish it should work..

1

You can call CMS block on Magento2 in following 6 ways:-

  • Call static block on category page in Magento2
  • Display CMS block with Magento 2 widgets
  • Insert CMS block into Magento 2 WYSIWYG editor
  • Call the CMS block using layout XML
  • Add static block from template phtml file in Magento 2
  • Show Static Block within another block in Magento2

For calling CMS block on layout, we can use following code - Considering your block identifier is - promo.

<block class="Magento\Cms\Block\Block" name="Promo">
    <arguments>
        <argument name="block_id" xsi:type="string">promo</argument>
    </arguments>
</block>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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