0
$this->loadLayout();
    $sidebar_blocks = $this->getLayout()->getBlock('catalog.compare.list');
    $sidebar_blocks->setTemplate('catalog/product/compare/list.phtml');
    $sidebar = $sidebar_blocks->toHtml();
    $sidebar = $this->renderLayout();

setTemplate on a nonObject error.

I am trying to load this block in the controller function.

10
  • 1
    are you sure the block with the name catalog.compare.list exists in your layout handles that you load?
    – Marius
    Sep 9, 2014 at 7:05
  • catalog.compare.list exists in the catalog.xml under <catalog_product_compare_index> handle Sep 9, 2014 at 7:07
  • in order to work this code snippet, you need to make sure block with name catalog.compare.list should be defined in the layout for this controller Sep 9, 2014 at 7:07
  • then the question is which controller that is using to do this ? Sep 9, 2014 at 7:08
  • i am using this block in the mycustom controller Sep 9, 2014 at 7:08

1 Answer 1

2

Suppose you have the custom controller like this.

class Namespace_Moduleneame_CustomController extends Mage_Core_Controller_Front_Action
{
    public function someAction()
    {
         $this->loadLayout();
         $sidebar_blocks = $this->getLayout()->getBlock('catalog.compare.list');
         $sidebar_blocks->setTemplate('catalog/product/compare/list.phtml');
         $sidebar = $sidebar_blocks->toHtml();
         $sidebar = $this->renderLayout();
    }
}

This means in someAction(), it is trying to set a template for a block that is already defined in your layout.

Suppose your route alias name is custom in config.xml file. Then suppose you are requested your custom method like this www.mysite.com/custom/custom/some. Magento will eventually comes to your controller's someAction() to process it. First it will loads the layout. This means it will load all the blocks that comes under custom_custom_some. ie

File : `app\design\frontend\<package>\<theme>\layout\your_layout.xml`

<custom_custom_some>
     <reference name="content">
         <block type="catalog/product_compare_list" name="catalog.compare.list" template="catalog/product/compare/list.phtml"/>
     </reference>
</custom_custom_some>

If the unique layout handle holds this block, then loadLayout() will automatically load this block and renderLayout() then render that block. If this block do not defined there, then magento don't know which block that you are referencing in your controller and hence you cannot see output. ie in effect your someAction() should look like this.

public function
{
     $this->loadLayout();
     $this->renderLayout();
 }
1
  • thanks i am trying this solution and soon i will mark this as a solution Sep 9, 2014 at 7:38

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.