cancel
Showing results for 
Search instead for 
Did you mean: 

is it possible to add block's class name dynamic into layout.xml file ?

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

is it possible to add block's class name dynamic into layout.xml file ?

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.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: is it possible to add block's class name dynamic into layout.xml file ?

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.

 

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

View solution in original post

1 REPLY 1

Re: is it possible to add block's class name dynamic into layout.xml file ?

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.

 

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"