Magento 2: How to call CMS Static Block from template (phtml) file?

In Magento 1.x, you could call/print the CMS Static Block in template file with the following code:


echo $this->getLayout()
          ->createBlock('cms/block')
          ->setBlockId('your_block_identifier')
          ->toHTML();

In Magento 2.x, it’s quite similar.

Below are the sample codes to show/call template (.phtml) files in Magento 2.

Call CMS Static Block in any template (.phtml) file


echo $this->getLayout()
          ->createBlock('Magento\Cms\Block\Block')
          ->setBlockId('your_block_identifier')
          ->toHtml();

Call a CMS Block inside another CMS Block (from admin panel)


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

Call CMS Block from Layout XML file

your_block_identifier = Your CMS Block Identifier can be found in CMS > Blocks in Magento Admin.


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

Call template (.phtml) file from CMS Block (Magento Admin)

If you want to use/call a template .phtml file from the Magento Admin CMS > Block, you can do so using the following code in the CMS Block content.

{{block class="VendorName\ModuleName\Block\BlockName" template="VendorName_ModuleName::your_template_file.phtml"}}

Hope this helps. Thanks.