25

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

61
<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.

  • @Ben-Space48, agree – KAndy May 3 '16 at 16:27
  • Can i add after='-' or before='-' tag in this – Vinit Kumar Feb 11 '20 at 10:59
  • yes you can @VinitKumar – diazwatson Feb 29 '20 at 2:10
15

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!

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>
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!

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>
  • Can i add after='-' or before='-' tag in this – Vinit Kumar Feb 11 '20 at 10:54
  • @VinitKumar , yes you can use after=''-" or before="-" tag. <block class="Magento\Cms\Block\Block" name="product_view_right_sidebar" before='-'> – Nikunj Panchal Feb 12 '20 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..

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.