I am trying to display a block as part of a response to an ajax call. Everything is working except I cannot get the controller to echo the template HTML code.
My module class:
class MyModule_Ajax_ProductController extends Mage_Catalog_ProductController {
public function indexAction() {
if ($product = $this->_initProduct()) {
echo '<div>hello</div>'; //this works
echo $this->getLayout()->createBlock('ajax/product')->setTemplate('mymodule_ajax/product.phtml')->toHtml();
//I also tried:
//$layout = $this->getLayout();
//$update = $layout->getUpdate();
//$update->load('ajax_product_index');
//$layout->generateXml();
//$layout->generateBlocks();
//$output = $layout->getOutput();
//echo $output;
}
}
}
Inside my template file product.phtml - this HTML never gets shown. (saved to app/design/frontend/default/default/template/mymodule_ajax/product.phtml)
<div>HERE!</div>
My block class:
class MyModule_Ajax_Block_Product extends Mage_Catalog_Block_Product
{
private $product;
protected function _construct()
{
parent::_construct();
$this->setTemplate('mymodule_ajax/product.phtml');
}
protected function _toHtml() {
return parent::_toHtml();
}
public function setProduct($product) {
$this->product = $product;
return $this;
}
public function getProduct() {
return $this->product;
}
}
My layout/mymodule_ajax.xml
<?xml version="1.0"?>
<layout>
<ajax_product_index>
<reference name="root">
<block type="ajax/project" name="root" output="toHtml" template="mymodule_ajax/product.phtml"/>
</reference>
</ajax_product_index>
</layout>
I am assuming that because I am setting up the template programmatically in my block class I shouldn't need the module reference? Removing the reference makes no difference.
I get no PHP errors, the HTML displayed renders
<html>
<head></head>
<body></body>
</html>
I simply can't figure out what I am doing wrong? I'm using Magento CE 1.8.1