🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Add Block’s Class Name Dynamically into Layout.xml File in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 1 min read

Dynamic content is one of the ways to enhance the shopping experience in Magento 2 store. It not only attracts the visitors but boosts the conversion too.

For example, you can dynamically display offers based on customer type, location, etc. And one of the ways to do so is the below programmatic method to add block’s class name dynamically into layout.xml file in Magento 2.

Method to Add Block’s Class Name Dynamically into Layout.xml File in Magento 2:

1. Create registration.php file in app\code\[Vendor]\[Namespace]\

    <?php
        \Magento\Framework\Component\ComponentRegistrar::register(
            \Magento\Framework\Component\ComponentRegistrar::MODULE,
            '[Vendor]_[Namespace]',
            __DIR__
        );

2. Create module.xml file in app\code\[Vendor]\[Namespace]\etc

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="[Vendor]_[Namespace]" setup_version="1.0.0"/>
    </config>

3. Create defualt.xml file in app\code\[Vendor]\[Namespace]\view\frontend\layout

    <?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">
        <container name="root">
            <block class="[Vendor]\[Namespace]\Block\Dynamic" name="block.banner" as="block.dynamic.block" />
        </container>
    </page>

4. Create Dynamic.php file in app\code\[Vendor]\[Namespace]\Block

    <?php


    namespace [Vendor]\[Namespace]\Block;

    use Magento\Framework\View\Element\Template;
    use Magento\Framework\App\RequestInterface;
    
    class Dynamic extends Template
    {
        protected $helper;
        public function __construct(
            Template\Context $context, 
            array $data = []
        ){

            parent::__construct($context, $data);
        }
        protected function _toHtml(){
        
            if(/* condition true */){
                $html = $this->getLayout()
                      ->createBlock('[Vendor]\[Namespace]\Block\[ClassOne]') // Here you can set dynamic class
                      ->setTemplate('[Vendor]_[Namespace]::custom.phtml') // if require
                      ->toHtml();
            }else{
                $html = $this->getLayout()
                      ->createBlock('[Vendor]\[Namespace]\Block\[ClassTwo]') // Here you can set dynamic class
                      ->setTemplate('[Vendor]_[Namespace]::custom.phtml') // if require
                      ->toHtml();
            }
            
        }
    }

Hope it helps.

Feel free to share the solution with fellow developers via social media.

Thanks.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.