Search code examples
htmlmagentomagento-1.7

Programmatically get html of top.links in magento


I am trying to get the html of top.links using the following ways:

  • $blockHtml = Mage::getModel('cms/block')->getBlockHtml('top.links')

  • $blockHtml = Mage::app()->getLayout()->getBlock('top.links').toHtml()

  • $blockHtml = Mage::getSingleton('core/layout')->getBlock('top.links')->toHtml()

None of above is working for me, how I can do this?

Thanks.


UPDATE

I used

$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('page/html')->setTemplate('page/html/top.links.phtml')->toHtml();

With the help of this question Add Top Links on condition basis in magento but still no luck. During this try I found that the use of top.links.phtml is deprecated, any idea which template should I use for the links?

I think there is some dependency for top.links.phtml file, that's why it isn't working, when I tried to get footer.phtml it worked perfectly with above method.


Solution

  • Wow! I was able to find a correct answer finally :) Load block outside Magento, and apply current template

    So by following the the above question's answer, I did this to get generated top.links

        $layout = Mage::app()->getLayout();
        $layout->getUpdate()
                ->addHandle('default')
                ->load();
    
        $layout->generateXml()
                ->generateBlocks();
    
        echo $layout->getBlock('top.links')->toHtml();