2

Newer versions of Magento are using _toHtml() instead getGroupedHtml() in the template files.

But Mage_Core_Block_Messages::_toHtml() always just returns $this->getGroupedHtml() - Also back in Magento CE 1.5.1

Can anyone point out why it got changed in newer versions? Thanks!

5
  1. _toHtml() method:

Every block that extends Mage_Core_Block_Template can override that method to produce html output. The explanation is in Mage_Core_Block_Abstract class (which is extended by the Mage_Core_Block_Template class).

final public function toHtml()
    {.....
        if ($html === false) {
            ....    
            $this->_beforeToHtml();
            $html = $this->_toHtml();
            $this->_saveCache($html);
            ....
         }
        $html = $this->_afterToHtml($html);
        ......
        return $html;
    }

and

protected function _toHtml()
    {
        return '';
    }

As you can see, toHtml() is a final method and you can't override it. Instead you can override _toHtml() and your html will be added to output by the $this->_toHtml() call.

  1. getGroupedHtml() method:

It is defined in Mage_Core_Block_Messages and it provides messages in HTML format grouped by type.

public function getGroupedHtml()
    {
        $types = array(
            Mage_Core_Model_Message::ERROR,
            Mage_Core_Model_Message::WARNING,
            Mage_Core_Model_Message::NOTICE,
            Mage_Core_Model_Message::SUCCESS
        );
        $html = '';
        ..........
        return $html;
    }

As you pointed out, the Mage_Core_Block_Messages::_toHtml() method calls the getGroupedHtml() to render the messages html output.

To wrap up, the methods _toHtml() and getGroupedHtml() are intended to serve different purposes.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.