Magento Version: 2.3.0
Hi,
As per my client requirement, I want to add the block's class name dynamically into the layout.xml file.
Is it possible in Magento 2?
Thanks in advance.
Solved! Go to Solution.
Hello @yagnik_solanki1
According to my knowledge, you can not set a dynamic block class in layout.xml file. However, you can set it at block class which is defined in layout.xml file.
Place the below code in block class in layout.xml file:
protected function _toHtml(){ $html = $this->getLayout() ->createBlock('[Namespace]\[Module]\Block\[Class]') // Here you can set dynamic class ->setTemplate('[Namespace]_[Module]::custom.phtml') // if require ->toHtml(); }
Hope it helps.
Do let me know if you need further assistance.
Hello @yagnik_solanki1
According to my knowledge, you can not set a dynamic block class in layout.xml file. However, you can set it at block class which is defined in layout.xml file.
Place the below code in block class in layout.xml file:
protected function _toHtml(){ $html = $this->getLayout() ->createBlock('[Namespace]\[Module]\Block\[Class]') // Here you can set dynamic class ->setTemplate('[Namespace]_[Module]::custom.phtml') // if require ->toHtml(); }
Hope it helps.
Do let me know if you need further assistance.