magento-2.0

Magento 2.0: Adding a static block in a page template


I want to add static block in page (.phtml) template.

I followed these steps:-

Step1: Navigate to CONTENT >Blocks >Add New Block.

Step 2: Create a Static Block (My_block).

Now I want to add this Block in page template.

I want to do something like below: $this->getLayout()->createBlock('cms/block')->setBlockId('your_block_id')->toHtml();

please let me know how to add this static block in my page (.phtml) template.


Solution

  • Display Static Block in Phtml file & CMS page

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

    In Phtml 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"}}