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

1

6

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

Sorehead answered 27/7, 2011 at 14:53 Comment(0)
S
3

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')
);
Shull answered 27/7, 2011 at 15:9 Comment(6)
Still no luck i tried loading that code and it outputs nothing. Is it possible to load the original "rendered" sidecart.phtml file from within this controller (by rendered i mean have it actually display whats in the cart as opposed to it always saying its empty)? Thanks againSorehead
In the layout file that corresponds to your custom controller you should add cart_sidebar block definition as in app/design/frontend/base/default/layout/checkout.xml, for examle. You can see adding the renderers for different product types here.Shull
alright well i attempted that as suggested also but still nothing. I posted my full controllerindex function and the layout file over at link I don't know exactly where im going wrong. the custom template directory is basically just a copy of *theme*/template/checkout.Sorehead
Thanks for the help i figured this out using this tutorial link i misunderstood some of the fundamentals of how magento loads the phtml files having mainly to do with the layout files. Thanks again for your helpSorehead
if you have a chance or even still have the code it would be great to see it. I'm having a issue with the same thing. CheersEverson
how to call a phtml file called in xml file without calling blockTout

© 2022 - 2024 — McMap. All rights reserved.