Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Last active May 23, 2021 20:05
Show Gist options
  • Save mttjohnson/99e3b60e291676f9f991 to your computer and use it in GitHub Desktop.
Save mttjohnson/99e3b60e291676f9f991 to your computer and use it in GitHub Desktop.
Magento 2 - Include Static Blocks (template/cms/layout)

In .phtml tempalte file:

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

In CMS content:

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

In layout .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>
@neocamel
Copy link

neocamel commented Oct 3, 2018

What do I need to change in this code to specify which static block I want to add?

@gil--
Copy link

gil-- commented Oct 8, 2018

@neocamel replace block_identifier with the block name in the admin.

@leonmekic
Copy link

how to specify where the block is added?

@MurKit
Copy link

MurKit commented May 24, 2019

I have a problem with the xml method. The block is added to the layout, but its' _toHtml() is never called, so this block does not appear on the page.

EDIT:
My bad, didn't notice name="block_id" was crucial here and didn't need to be changed, as it is an id of the argument!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment