Add Top Links on condition basis in magento

2019-07-27 04:00发布

问题:

I am little bit stuck with adding top links in my custom phtml.I removed the links block in my xml <remove name="top.links"/> , now after some condtion become true i want to add this block again.When i use this code for top menu it works but not for links

$block = Mage::getSingleton('core/layout');     
        echo $block->createBlock('catalog/navigation')->setTemplate('catalog/navigation/top.phtml')->toHtml();

This works and disply top menu.But the below code doesn't show anything.

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

Any help ???

回答1:

After using observer i solve my problem for time being.I think this is alternate way.In my config i define oberser like :

 <frontend>
        <events>
            <controller_action_layout_generate_xml_before>
                <observers>
                    <Mymodule>
                        <class>Mymodule_Model_Observer</class>
                        <method>addmyblock</method>
                    </Mymodule>
                </observers>
            </controller_action_layout_generate_xml_before>
        </events> 
</frontend>

while in oberser simply check and remove block :

<?php
class Mymodule_Model_Observer
{
    public function addmyblock(Varien_Event_Observer $observer)
    {
         if(Mage::getStoreConfig("mymodule/general/enable")==1) 
    {
        $layout = $observer->getLayout();
        $layout->getUpdate()->addUpdate('<remove name="top.search"/>
        <remove name="top.links"/>');              
         $layout->generateXml();
     }
    }
}.

Also i removed the code from xml <remove name="top.links"/>.So the code only remove block when the condition become true.