1

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();
    }
} 

3 Answers 3

3

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');
1
2

If you are using blocks that inherit Mage_Core_Block_Template (i.e. that use a template to display) you can assign data using the assign() method, once the blocks have been instanciated by loadLayout()

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

$this->getLayout()->getBlock('your.block.name.in.the.layout')->assign('data', $data);

Then, in the .phtml template, you can simply use

<?php echo $data ?>

This is not used very often in magento, but since it's implemented as public methods and thus declared stable, I believe it's fine do so. Thats also the reason for the convention to start variables declared in a template with an underscore (e.g. $_product = $this->getProduct()), so they can be distinguished from assigned variables.

0

And for Magento2 :

inside controller's execute method:

$layout = $this->_view->loadLayout();
$layout->getlayout()->getBlock('your_block_name_here')->setData('order', $order);
$this->_view->renderLayout();

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.