How To

Magento 2: Append Block to Specific Container Dynamically

Hello Magento Friends,

In today’s article, I will be explaining Magento 2: Append Block to Specific Container Dynamically. Let’s check out our previous blog, Magento 2: How to use Default Curl Class For API Call. 

Introduction:

Blocks are used to display information in the form of text, images, videos, or dynamic information in Magento 2. We can add or remove blocks as per the requirement. Sometimes you come across the need to add a block to a specific container dynamically in Magento 2. Let’s dive into the steps to append block to specific container dynamically in Magento 2. 

Steps to Append Block to Specific Container Dynamically in Magento 2:

Step 1: First you need to create events.xml in the following path

app\code\Vendor\Extension\etc\frontend

Now, add the below code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="core_layout_render_element">
    <observer name="load_custom_handler" instance="Vendor\Extension\Observer\Appendblock" />
</event>

Step 2: Now you need to add the Appendblock.php file in the following path

app\code\Vendor\Extension\Observer

And finally, add the below code

<?php
namespace app\code\Vendor\Extension\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class Appendblock implements ObserverInterface
{
  private static $_layoutSet = false;

  public function execute(Observer $observer)
  {
     // Run this only once
     if(ModifyBlock::$_layoutSet) { return; }
     Appendblock::$_layoutSet = true;
     $layout = $observer->getLayout();
     // Add a new block
     $layout->createBlock('Vendor\Extension\Block\Index\Index', 'custom-index-index');
     $layout->setChild('content', 'custom-index-index', 'custom-index-index');
     return;
  }
}

That’s it!

Conclusion:

Hopefully, everyone is able to append blocks to specific containers dynamically in Magento 2. In case you address the difficulty while performing the above steps, feel free to ask me in the comment section. Spread the article amongst your Magento friends to help them out with the solution. See you back soon, till then stay connected with us.

Happy Coding!

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVĂ„ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago