Skip to main content

Display PHTML file or Static Block in CMS page Magento2

Yo display phtml file in CMS page put this line


{{block class="Magento\Framework\View\Element\Template" name="test_block" template="{Vendor}_{Module}::test_block.phtml"}}

the same way in XML layout file you can use 

<block class="Magento\Framework\View\Element\Template" name="test_block" template="{Vendor}_{Module}::test_block.phtml"/>

if you want to put it in phtml file

<?php include ($block->getTemplateFile('{Vendor}_{Module}::test_block.phtml')) ?>

Finally  you can do it as before like this 

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("{Vendor}_{Module}::test_block.phtml")->toHtml();?>

Add static block in CMS page

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

in phtml file

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

in XML

 

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