Here is a small tip on how to call CMS static block on the homepage with a specific .phtml template in Magento 2.
Magento 2 allows easy creating of CMS static blocks. After you’ve created a CMS Block, you need to add this block into the layout.
In order to add the block on every page, you should use default.xml layout file from app/design/frontend/Vendor/Theme/Magento_Theme/layout directory, that will call block’s template on every page of your current theme.
If you need to place Static Block only on the homepage you should add directives into cms_index_index.xml layout file from the app/design/frontend/Vendor/Theme/Magento_Cms/layout directory. For those who just get started with Magento 2 let’s take a look at the detailed example.
For example, you have created CMS Block “Slider” which has the following ID “homepage-slider“. In order to use the slider block only on the Homepage, you should place your template (let’s call it slider.phtml) to the directory app/design/frontend/Vendor/Theme/Magento_Cms/templates of your current theme. Layout update file cms_index_index.xml should be placed into the app/design/frontend/Vendor/Theme/Magento_Cms/layout directory of your current theme.
Template file slider.phtml file will call your block:
1 |
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('homepage-slider')->toHtml();?> |
Layout file cms_index_index.xml will call your template:
1 2 3 |
<referenceContainer name="main"> <block class="Magento\Framework\View\Element\Template" name="homepage_slider" template="Magento_Cms::slider.phtml" /> </referenceContainer> |
Another way to add CMS Block to the page is:
1 2 3 4 5 6 7 |
<referenceContainer name="main"> <block class="Magento\Cms\Block\Block" name="homepage-slider"/> <arguments> <argument name="block_id" xsi:type="string">homepage-slider</argument> </arguments> </block> </referenceContainer> |
That’s it.
Check also how to create a custom Homepage Template in Magento 2.
PrestaShop 1.6 - 1.7 themes
Take your online store design to the new level with BelVG PrestaShop themes for 1.6 and 1.7 versions
Visit the storeNeed a ready-made and quality Magento solution? Visit BelVG store and find the one to suit your store the most.
Dear Mari,
it’s possible that I misunderstood the issue; could you please clarify the question?
Kindly,
Iryna
Hi, could you please tell me where should I place this code:
homepage-slider
Is it inevitable or can I use it instead of this one:
???
Eduardo, thank you for the addition!
Remember to take the last block slash to get the argument working
Thank you. Very helpful.