Magento 1 – Load catalog/product/list.phtml for Custom Module Collection

moduleproduct-listtemplate

I am writing a new module to get a filtered Collection.
Currently I am using my own template file to show the results but I want to use directly my templates catalog/product/list.phtml file to keep evreything simple and centralized.

Currently what I have in my controllers index action is :

$block = $this->getLayout()->getBlock('brands');//get the block instance
$block->setData("collection",$collection)->setTemplate('cantek_brand/brand.phtml'); //set collection and the template

In my modules layout xml

 <layout>
  <brand_index_index>
     <reference name="content">
         <block type="core/template" name="brands" as="brands" />
     </reference>
  </brand_index_index>
 </layout>

So, with this way I have the collection set and the template that I created. But this way the list views are not identical on the website.

My Question is:
How can I use catalog/product/list.phtml for my custom module template?

Best Answer

In your layout xml

<brand_index_index>
  <reference name="content">
     <block type="brand/custom" name="product_list" template="catalog/product/list.phtml">
        <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
            <block type="page/html_pager" name="product_list_toolbar_pager"/>
        </block>
        <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
        <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
        <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
        <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
        <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
        <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
        <action method="setColumnCount"><columns>3</columns></action>
    </block>
   </reference>
</brand_index_index>

In My/Brand/Block/Custom.php

<?php
class My_Brand_Block_Custom extends Mage_Catalog_Block_Product_List{
    protected function _getProductCollection() {
        if(is_null($this->_productCollection)) {
            $this->_productCollection = parent::_getProductCollection();
            $this->_productCollection->addAttributeToFilter('custom_attribute', array('eq' => 'custom_value'));// or your custom filter
        }
        return $this->_productCollection;
    }
}
Related Topic