Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 484
user398314 Avatar asked Jul 27 '11 14:07

user398314


1 Answers

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')
);
like image 191
vsushkov Avatar answered Oct 16 '22 22:10

vsushkov