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
Best Answer
I find these things very difficult. I am going to try to answer in two parts:
PART ONE
You may want to try to render the layout in the controller so try this first then read the additional items below:
BUT you are making an Ajax module. So you might want to utilise controller methods such as:
And if you want to
setBody()
to a block's HTML then in the controller the code you have posted above is looking good (but see part two below):-And actually even just calling
exit();
at the end ofindexAction()
will flush the PHP output buffer to the browser.I note that you are making a product controller. Studying the code in the class you are extending may help:
which might give you some ideas about sending Magento-standard product-related HTML to the browser. You may find the
prepareAndRender()
helper function more interesting but ultimately it uses$this->renderLayout()
to create the output html (here$this
is the controller).PART TWO
I think you need to concentrate on the line in your code:
and either change it to
or, in your module's config.xml file add something like: (I think this is the answer you need)
so that Magento can understand that
createBlock('ajax/product')
meansMyModule_Ajax_Block_Product
and notMage_Ajax_Block_Product
.I also recommend you change
MyModule
toMymodule
throughout so that there is not a capital letter in the middle. Becausewell, I don't understand the fine details but generally Magento will make
mymodule
intoMymodule
notMyModule
Does that make sense?