Evrig

Magento 2 offers great flexibility for developers when it comes to displaying content dynamically. One powerful feature is the ability to call static block in Magento 2 PHTML files, which allows you to manage and display predefined content efficiently across your stores.
In this guide, we’ll walk you through the process of calling a static block in .phtml file in Magento 2

Step-by-Step Guide:

Step 1: Get the Block Identifier

The first step is to identify the static block you want to display. You can find the block identifier by navigating to Content > Blocks in the Magento Admin panel.

Static Block In Magento 2

 

Step 2: Define the Static Block in the XML Layout

To effectively call static block in PHTML Magento 2, you need to reference the Magento 2 static block in the layout XML file. This is essential depending on where you want to display the block (product page, category page, etc.)

Example: Call Static Block in customer/account/login/ page

customer_account_login.xml

Static Block in the XML Layout

 

Create or edit the XML file in your custom theme following the path:-

customer_account_login.xml

app/design/frontend/Vendor_Name/Theme_Name/Magento_Customer/layout/

Here’s the XML code you need to add:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_form_login">
<block class="Magento\Cms\Block\Block" name="discount-coupon-code">
<arguments>
<argument name="block_id" xsi:type="string">discount-coupon-code</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>

Replace discount-coupon-code with the identifier of your static block.

Step 3: Render the Static Block in a .phtml File

To render the block in a .phtml file, you can use getChildHtml() to call the block defined in the XML layout. This is another crucial step when you want to call a static block in PHTML Magento 2.

Create or edit the PHTML file in your custom theme following path:-

app/design/frontend/Vendor_Name/Theme_Name/Magento_Customer/templates/form/


Add the following code to render the block:

<?php echo $block->getChildHtml('discount-coupon-code'); ?>

 Render the Static Block in a .phtml File

Step 4: Clear the Magento cache

Afetr making there changes, you need to refresh the cache for them to take effect. Run this command in your terminal

php bin/magento cache:flush

Conclusion:

By implementing the steps outlined in this guide, you can seamlessly integrate static blocks into your PHTML files within Magento 2. This approach not only streamlines your content management process but also enhances your site’s overall performance. By utilizing this technique, you empower your e-commerce platform to deliver a more engaging and user-friendly experience, ultimately driving higher customer satisfaction and retention. Embrace this powerful capability to take your Magento store to the next level!