1

I have a category list page which uses the 1column.phtml template. However, when the user navigates to a category where there are no subcategories - and only its products are displayed - I would like to call the left column. The problem is that it is that I am still using the 1column template for this situation.

How can I call the left column?

4
  • 2
    If you want a left column - then it isn't a 1column layout any more, its a 2col-left layout. Why not just change the root template of your categories that need this layout? You could do some block manipulation fairly easily with XML - but it goes a bit against the point of having a 2col-left layout if you are putting a left col in your 1column layout. Commented Feb 26, 2013 at 17:01
  • I use 1column for my category lists because in most instances (when categories have subcategories) I do not want a left column. However, in one instance (when there are no subcategories) I want the left column.
    – YWSW
    Commented Feb 26, 2013 at 17:16
  • Then you could rewrite the category controller - and set the layout handle and root template for the category conditionally - based on whether it had products or not. That would work. Commented Feb 26, 2013 at 17:17
  • ok. how would I do that?
    – YWSW
    Commented Feb 26, 2013 at 17:19

2 Answers 2

2

If I remember correctly, the CategoryController adds a layout handle for categories with no subcategories [link], meaning that this can be done via layout XML, e.g. in local.xml layout file:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <catalog_category_layered_nochildren>
        <action method="setTemplate" block="root">
            <tpl>page/2columns-left.phtml</tpl>
        </action>
    </catalog_category_layered_nochildren>
</layout>
5
  • 1
    <catalog_category_layered_nochildren translate="label"> <reference name="root"> <action method="setTemplate"><template>page/2columns-left.phtml</template></action> </reference> </catalog_category_layered_nochildren> This is the code I wrote but I am still getting 1column.phtml... why do you think this is happening?
    – YWSW
    Commented Feb 26, 2013 at 22:26
  • never mind - "isAnchor" issues were the problem... thank you very much!
    – YWSW
    Commented Feb 26, 2013 at 23:02
  • super benmarks :( all of my work was for nothing :) I like short answers! Thanks benmarks. TIL: there is catalog_category_layered_nochildren. Commented Feb 26, 2013 at 23:53
  • 1
    @FabianBlechschmidt - please do not think that your work was for nothing - it taught me how to make observers - and while I did not need it in this instance, I will surely need it elsewhere...
    – YWSW
    Commented Feb 27, 2013 at 16:53
  • Oh no, I don't think that :-) And I'm eager to learn more. So everything is alright. Commented Feb 28, 2013 at 17:28
2

For a single category:

You can change the layout for the single category. To do this, go to the categorie in the backend, open the tab Custom Layout and choose "2 columns with left bar" in Page Layout dropdown.

Implementing an observer

I think listening on core_block_abstract_to_html_before might be a good idea. Check wether the block is root. If it is a category, the category can be found in Mage::registry('current_category');. Then you check for hasChildren() and if it has, set the Template for the root block.

I'm not sure whether this is enough. Maybe you need to change the update handle too.

Implementation

Observer.php

public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $observer)
{
    /* @var $block Mage_Core_Block_Template */
    $block = $observer->getBlock();
    if($block->getNameInLayout() == 'root') {
        /* lvar $category Mage_Catalog_Model_Category */
        if(($category = Mage::registry('current_category')) && $category instanceof Mage_Catalog_Model_Category) {
            if(!$category->hasChildren()) {
                $block->setTemplate('page/2columns-left.phtml');
            }
        }
    }
}

config.xml

<config>
    <frontend>
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <namespace_mymodule>
                        <type>model</type>
                        <class>Namespace_MyModule_Model_Observer</class>
                        <method>coreBlockAbstractToHtmlBefore</method>
                    </namespace_mymodule>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
    </frontend>
</config>
8
  • thank you - but this would be too time consuming to do for every single category...
    – YWSW
    Commented Feb 26, 2013 at 17:45
  • ah, I misinterpreted your "in one instance" :) Then you can implement a observer :) I'll edit my answer Commented Feb 26, 2013 at 17:48
  • 1
    @FabianBlechschmidt That's exactly what I was getting at. A nice clean & simple solution. Commented Feb 26, 2013 at 17:55
  • Is it possible to add some more explanation (I am still new at anything in Magento's "code" folder...)
    – YWSW
    Commented Feb 26, 2013 at 17:57
  • Personally, I would prefer @FabianBlechschmidt 's first method, simply updating the layout in each category's custom layout section. The reason being is that it is more obvious why the layout is changing on a specific category, rather than being set in an observer which will confuse someone down the road. If you're concerned with having to do this for many categories (how many are we talking?) then a simple script executed once could programmatically set these for you.
    – pspahn
    Commented Feb 26, 2013 at 20:11

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.