Magento – Use template/block in a helper

blockshelpermagento-enterprisetemplate

I have a helper with this method to get the items in a customer's cart

public function getCartData()
{
    //Get cart DATA
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $cartItems = $quote->getAllVisibleItems();
    $items = '';
    foreach ($cartItems as $item) {
        $items .= $item->getId() . " ";
    }
    return $items;
}

But what I want to do is replace this line

$items .= $item->getId() . " ";

With an instance of template/checkout/cart/sidebar/default.phtml

How would I go about this? The method is being called in an ajax controller. I want to update the user's cart without a page refresh, but it needs to be formatted.

Best Answer

You can create blocks on the fly and fetch the HTML

Simple example:

    $tempBlock = Mage::app()->getLayout()->createBlock('core/template')
        ->setTemplate('template/checkout/cart/sidebar/default.phtml');

    $items .= $tempBlock->toHtml();

This is not complete as it misses the item information and I just used the core/template which is not the usual on for this template.

But I hope you get the idea?