Magento 2 CMS is popular among the E-commerce store owners owing to its flexibility. It allows fulfilling the business requirements that are based on customer expectations or to improve the onsite user experience.
As a part of such requirement fulfilment and enriching the store frontend, the following solution to pass data from CMS block to PHTML file in Magento 2 can be helpful.
Using the below code, one can update the category on the home page to display the subcategories, display product blocks, offers launched, etc.
Method to pass data from CMS block to PHTML file in Magento 2:
Add the following code in any static block:
{{block class="Magento\Framework\View\Element\Template" template="Magento_Catalog::display-product.phtml" product-sku="VVXL10"}} \\ Here you can put your custom block class
Create display-product.phtml in app/design/[Namespace]/[Theme]/Magento_Catalog/templates
<?php $productSku = $block->getData('product-sku'); echo "Product SKU ".$productSku;
That’s it.
Feel free to share the solution with Magento 2 community via social media.
Thank you.