In this article, you going to see a discussion regarding Magento 2 call Static Block in phtml in detail and step by step with a simple code snippet.
If you are looking for this topic then you are at right place.
Let’s start with the topic Magento 2 call Static Block in phtml
It is very easy to Call CMS Static Block in phtml File using Magento 2
Let’s have a look step by step in calling of CMS Static Block in phtml file.
First step is to Login to admin section.
After Login to Magento 2,move to Section called Content –> Blocks.
Then Click “Add New Block”.
Next step is to add below information and save configuration.
- Enable Block: It is used to Enable the block.
- Block Title: Add the Name of the block to identify the purpose of creating the block.
- Identifier: Add a unique block identifier.
- Store View: Select the static block store views to show where to display a static block.
- Block Content: Final step is to add the content of the block.
Click the save button by add data to it
Below is code snippet for calling static block into the template file.
<?php
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')
->setBlockId('block_identifier')
->toHtml();
?>
In above code “block_identifier” is the static block identifier.
Main important in adding static block is its unique identifier and it is required field.
Below is code snippet to Display CMS Static Block In CMS Content.
{{block class="Magento\Cms\Block\Block" block_id="block_identifier"}}
Below is code snippet to Display CMS Static Block 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>
After updating code in the file, our next step is execute the following command given below for clear cache.
php bin/magento cache:clean
php bin/magento cache:flush
Now next step is to check in the front-end CMS data get appeared.
Magento 2 Call Helper Function in phtml
Conclusion:
Done with all point for adding Static Block in phtml.
I hope you like this article and if you have any query, please comment below.