conditionally change layout template for catalogsearch
-
16-10-2019 - |
Pergunta
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.
Solução
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();
}
Outras dicas
Take a look at this article: http://sandersluis.com/2011/11/changing-the-page-template-in-a-magento-controller/. Hope this helps.