Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Controller Control
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Hi all, I am trying to call a block directly from the controller. Has anyone done this before?

Basically this is what I’m trying to do:

class Mage_Test_TestController extends Mage_Core_Controller_Front_Action
{
  
public function indexAction()
  
{
    $this
->loadLayout();

    
// call a block and template directly?

    
$this->renderLayout();
  
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Basically, I want to call a block only, not the entire page. Is there a way to call a specific block only?

For instance, can I call the template/checkout/cart/sidebar.phtml with its corresponding block, without getting the entire page to render?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Does anybody know of a quick way to do this through the controller, or do I have to create a custom url router in the config.xml and then a custom layout for each request?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

The layout kicks out an entire page because the “root” tag in the layout/page.xml is set to be an “output” block.  So, all you have to do is switch which block you want to be the main block as the only “output” block and the layout will start there and go down to any child blocks as normal.

$this->getLayout->removeOutputBlock('root');
$this->getLayout()->addOutputBlock('my_sub_block');

Makes sense?  Too bad it doesn’t work. confused You’ll have to add the method “removeOutputBlock” yourself.  It should be in the next release after 0.8 as a standard method.

// in Mage/Core/Model/Layout.php

    /**
     * Remove a block from output
     *
     * @param string $blockName
     */
    public function removeOutputBlock($blockName)
    
{
        
unset($this->_output[$blockName]);
    
}

I really wish people wouldn’t use private/protect/public with out a WHOLE LOT of thought put into the API first.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top