0

I'm in a product listing page list.phtml, i have overwritten template file adding another view. As this file looks mess(also more lines of codes), i want to break down this file into different phtml file.

Here is what i want to achieve.

<?php 
    if ($this->getMode() == 'newview'):
        //New added view
        //Get this part from another phtml file
   elseif($this->getMode() == 'grid'):
        //Grid view
        //Get this part from another phtml file
   else:
        //List view
        //Get this part from another phtml file
   endif;
?>

And obviously the product data $_productCollection,$_attributes and so on needs to be accessible form the separated file.

i have overwritten the product listing page(file) from layout file like this

<catalog_category_default>
    <reference name="product_list">
         <action method="setTemplate">
             <template>newview/catalog/product/customlist.phtml</template>
         </action>
    </reference>
    <reference name="product_list_toolbar">
        <action method="setTemplate">
            <template>newview/catalog/product/list/customtoolbar.phtml</template>
        </action>
    </reference>
</catalog_category_default>
<catalog_category_layered>
    //Same content as above.
</catalog_category_layered>

I am wondering as, i'm already inside a condition, is it possible to manage those parts from different file.

1

One of the ways you can do this is by passing the information you require in to the child blocks. For example

<?php

if ($this->getMode() == 'newview'):
    $this->getChild('newview')->setProductCollection($_productCollection);
    echo $this->getChildHtml();
endif;

You can call multiple sets one after the other.

To enable this you will need to declare your new blocks as children of the product_list. Something similar to following in your own theme layout file.

<reference name="product_list">
    <block type="core/template" name="newview" template="catalog/product/list/newview.phtml" />
</reference>

You can find examples of this inside magento. Specifically there is one in app/design/frontend/base/default/template/wishlist/view.phtml

on line 43/44 (checked in v1.9.2.1)

Edit Make sure you add your child blocks first otherwise you will get the fatal error as in the comments.

Regards, Andy

  • It says Fatal error: Call to a member function setProductCollection() on a non-object and i guess it's echo $this->getChildHtml('newview'); on above code. – Suman K.C Aug 20 '15 at 11:48
  • Make sure you add your child blocks first. I will edit it in. – Smartie Aug 20 '15 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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