8

How can we call a static block in Magento 2 in a page. In Magento 1 we have this with the following code:

<reference name="left">
<block type="cms/block" name="Klantenservice menu" before="-">
    <action method="setBlockId"><block_id>cmsmenu</block_id></action>
</block>    
</reference>
45

If you want to call static block in page

Try below code :

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

If you want to call in phtml file :

Try below code :

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

Your xml file code should be :

<referenceContainer name="sidebar.additional">
   <block class="Magento\Cms\Block\Block" name="yourblockid">
       <arguments>
            <argument name="block_id" xsi:type="string">yourblockid</argument>
       </arguments>
   </block>
</referenceContainer>

At Last if you want to call phtml with your block in cms page :

Try below code :

{{block class="Magento\Modulename\Block\Blockname" template="Magento_Modulename::templatefilename.phtml"}} 

Refer this link for more details - https://chetansanghani.wordpress.com/2015/11/20/magento2-display-static-block-in-phtml-file-cms-page/

  • Thanks for the reply, whatever I try in a page I get the following error: "Please correct the XML data and try again. Element 'layout': Character content other than whitespace is not allowed because the content type is 'element-only'. Line: 1" Any idea? – Coen Jan 25 '17 at 13:42
  • @Coen check is there any extra space in your xml file ? or at the starting of xml file ? – Manthan Dave Jan 25 '17 at 13:43
  • yes there was a comma, I now found somewhere else the answer. See my question above – Coen Jan 25 '17 at 13:47
  • @Coen updated the answer please check – Manthan Dave Jan 25 '17 at 13:49
  • Thanks, uses the one in the xml file and that one by a cms page – Coen Jan 25 '17 at 15:16
7

This should work:

<referenceContainer name="sidebar.additional">
   <block class="Magento\Cms\Block\Block" name="yourblockid">
       <arguments>
            <argument name="block_id" xsi:type="string">yourblockid</argument>
       </arguments>
   </block>
</referenceContainer>
  • Where should I put this xml? – Witalo Benicio Dec 19 '17 at 18:02

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.