2

What I need to do is, if a person search for a product then if products found the layout need to be page/2columns-left.phtml with layered navigation but if there were no results then its layout should be page/1column.phtml and it will display new products, popular products.

One of the solution I came around is to override the CatalogSearch/ResultController's indexAction() where I can get whether $query->getNumResults() is 0 or not. On that basis how could I load layout differently?

If there's another better approach to this, that would be helpful too.

2 Answers 2

2

1.Add layout handle in catalogsearch.xml:

<NO_SEARCH_RESULTS>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
    <reference name="content">
        <!--  add your blocks here-->
    </reference>
</NO_SEARCH_RESULTS>

2.Load this handle in controller:

public function indexAction()
{
    /**
     * some codes here
     */

    $this->loadLayout();
    if ($someValueIsTrue) {
        $this->getLayout()->getUpdate()->addHandle('NO_SEARCH_RESULTS');
        $this->loadLayoutUpdates();
        $this->generateLayoutXml()->generateLayoutBlocks();
    }

    $this->renderLayout();
}
5
  • perfect! exactly the same thing what I wanted. I was missing $this->loadLayoutUpdates();. Can you tell me what is the order of calling these functions? Is there any blog or tutorial on it? Commented Feb 7, 2014 at 5:00
  • @MohammadFaisal see public function loadLayout() of Mage_Core_Controller_Varien_Action class. Our logick is same as logic of this function.
    – mageUz
    Commented Feb 7, 2014 at 6:15
  • I'd previously tried $this->loadLayout('NO_SEARCH_RESULTS'); but it doesn't worked that time? Commented Feb 7, 2014 at 6:20
  • 1
    In this case default handle will not added to layout updates. See public function loadLayout() for understanding the logic.
    – mageUz
    Commented Feb 7, 2014 at 6:23
  • 1
    You could also do $this->loadLayout(array('default','NO_SEARCH_RESULTS').
    – benmarks
    Commented Mar 31, 2014 at 12:33
0

Take a look at this article: http://sandersluis.com/2011/11/changing-the-page-template-in-a-magento-controller/. Hope this helps.

1
  • not the answer I'm looking for. it just tell about changing the template. and i'm looking for rendering the layout xml Commented Feb 7, 2014 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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