{"id":605,"date":"2019-09-16T05:43:10","date_gmt":"2019-09-16T05:43:10","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/09\/16\/add-blocks-class-name-dynamically-into-layout-xml-file-in-magento-2\/"},"modified":"2025-05-22T15:59:37","modified_gmt":"2025-05-22T10:29:37","slug":"add-blocks-class-name-dynamically-into-layout-xml-file-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-blocks-class-name-dynamically-into-layout-xml-file-in-magento-2\/","title":{"rendered":"How to Add Block&#8217;s Class Name Dynamically into Layout.xml File in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 <i><strong>add block&#8217;s class name dynamically into layout.xml file in Magento 2<\/strong><\/i>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Block&#8217;s Class Name Dynamically into Layout.xml File in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create <em><strong>registration.php<\/strong><\/em> file in <strong><strong>app\\code\\[Vendor]\\[Namespace]\\<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    &lt;?php\n        \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n            \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n            '[Vendor]_[Namespace]',\n            __DIR__\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <em><strong>module.xml<\/strong><\/em> file in <strong><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    &lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n        &lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create <em><strong>defualt.xml<\/strong><\/em> file in <strong><strong>app\\code\\[Vendor]\\[Namespace]\\view\\frontend\\layout<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    &lt;?xml version=\"1.0\"?>\n\n    &lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n        &lt;container name=\"root\">\n            &lt;block class=\"[Vendor]\\[Namespace]\\Block\\Dynamic\" name=\"block.banner\" as=\"block.dynamic.block\" \/>\n        &lt;\/container>\n    &lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create <em><strong>Dynamic.php<\/strong><\/em> file in <strong><strong>app\\code\\[Vendor]\\[Namespace]\\Block<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    &lt;?php\n\n\n    namespace [Vendor]\\[Namespace]\\Block;\n\n    use Magento\\Framework\\View\\Element\\Template;\n    use Magento\\Framework\\App\\RequestInterface;\n    \n    class Dynamic extends Template\n    {\n        protected $helper;\n        public function __construct(\n            Template\\Context $context, \n            array $data = []\n        ){\n\n            parent::__construct($context, $data);\n        }\n        protected function _toHtml(){\n        \n            if(\/* condition true *\/){\n                $html = $this->getLayout()\n                      ->createBlock('[Vendor]\\[Namespace]\\Block\\[ClassOne]') \/\/ Here you can set dynamic class\n                      ->setTemplate('[Vendor]_[Namespace]::custom.phtml') \/\/ if require\n                      ->toHtml();\n            }else{\n                $html = $this->getLayout()\n                      ->createBlock('[Vendor]\\[Namespace]\\Block\\[ClassTwo]') \/\/ Here you can set dynamic class\n                      ->setTemplate('[Vendor]_[Namespace]::custom.phtml') \/\/ if require\n                      ->toHtml();\n            }\n            \n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Hope it helps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=605"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":15270,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/605\/revisions\/15270"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}