How to load a magento view (.phtml) file from a controller

user398314 picture user398314 · Jul 27, 2011 · Viewed 8.2k times · Source

Im trying to load a rendered version of the cart sidebar, which i intend to load via ajax... I've been searching around alot and it seems like the best approach is to create a custom module which will handle all my ajax request. I have created a custom module and everything seems to be working however when I display the sidebar.phtml it is not rendered properly. It displays as if there is nothing in the cart. (im assuming its just trying to reading the file without using any session info). I've search a bunch but nothing seems relevant to what I'm trying to do.

This is the code im using in my custom controller to load the phtml file which is essentially a copy and past of the checkout/cart/sidebar.phtml file.

$layout = $this->loadLayout();


$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'PPWD_Custom',
array('template' => 'custom/custom.phtml')
);
echo $block->toHtml(); 

Thanks

Answer

vsushkov picture vsushkov · Jul 27, 2011

The problem is in incorrect block type. Instead of Mage_Core_Block_Template you should use Mage_Checkout_Block_Cart_Sidebar. Like this:

$this->getLayout()->createBlock(
    'checkout/cart_sidebar',
    'PPWD_Custom',
    array('template' => 'custom/custom.phtml')
);