Magento – Block->toHtml() dosn’t have template

magento-1.9template

I have a function like that:

public function aAction()
{
    $productId = $this->getRequest()->getParam('id');
    $block = Mage::getBlockSingleton('catalog/product_view_options');
    $product = Mage::getModel('catalog/product')->load($productId);
    $block->setProduct($product);
    echo $block->toHtml();
}

the method toHtml doesn't have a template set and returns nothing.
How can I fix it?

Best Answer

public function aAction()
{
    $productId = $this->getRequest()->getParam('id');
    $block = Mage::getBlockSingleton('catalog/product_view_options');
    //add this line.
    $block->setTemplate('catalog/product/view/options.phtml');
    $product = Mage::getModel('catalog/product')->load($productId);
    $block->setProduct($product);
    echo $block->toHtml();
}

But this might now work properly because the options block makes use of options renderes to render different types of options.

So you can try this:

public function aAction()
{
    $productId = $this->getRequest()->getParam('id');
    $optionRenderers = array(
          'text' => array (
               'block' => ' catalog/product_view_options_type_text',
               'template' => 'catalog/product/view/options/type/text.phtml',
           ),
           'file' => array (
               'block' => ' catalog/product_view_options_type_file',
               'template' => 'catalog/product/view/options/type/file.phtml',
           ),
           'select' => array (
               'block' => ' catalog/product_view_options_type_select',
               'template' => 'catalog/product/view/options/type/select.phtml',
           ),
           'date' => array (
               'block' => ' catalog/product_view_options_type_date',
               'template' => 'catalog/product/view/options/type/date.phtml',
           ),
    )
    $block = Mage::getBlockSingleton('catalog/product_view_options');
    $block->setTemplate('catalog/product/view/options.phtml');
    foreach ($optionRenderers as $type => $settings) {
        $block->addOptionRenderer($type, $settings['block'], $settings['template']);
    }
    $product = Mage::getModel('catalog/product')->load($productId);
    $block->setProduct($product);
    echo $block->toHtml();
}
Related Topic