Magento – pass data from module controller to layout

blockscontrollerslayoutmodelmodule

i make a simple module and i want to get all products of category and pass these products to my layout file. as well as i want to get products of a specific category and display these to my layout or view. for this i have to make seperate model or i can use build in functionality.
here is some code it write i my controller.

  class Saqi_Builder_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction(){
     $this->loadLayout(array('default'));
     $this->renderLayout();
    }
} 

Best Answer

Try this:

class Saqi_Builder_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction(){
     $productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);

     $this->loadLayout(array('default'),'');

     $this->getLayout()->getBlock('your_main_block_name_here')->setData('products', $productCollection);

     $this->renderLayout();
    }
} 

Then you will be able to access the product collection in your block or template like this:

$this->getData('products');
Related Topic