1

I am creating an attribute filter. When you click on an attribute value I would like an ajax request to pull through just the part of the module that displays the product list. However, I am getting the entire site fired back to me through console!

In my index controller:

public function indexAction()
{ 

    if ($this->getRequest()->isXmlHttpRequest()) {
        $this->loadLayout();
        $this->getLayout()->getBlock('product_list');
        $this->renderLayout();
    } else {
        $this->loadLayout();
        $this->renderLayout();            

    }
}

How can I just push through the product_list block?

Any help is appreciated.

--- UPDATE ----

I did it like this... is this the best way?

    if ($this->getRequest()->isXmlHttpRequest()) {
        $this->loadLayout();
        echo $this->getLayout()->getBlock('toxicfox.giftfinder.view')->getChildHtml('product_list');


    } else {
        $this->loadLayout();
        $this->renderLayout();            
      }
1
  • You may want to write your update as an answer. This is how I usually do it and it worked so far without issues.
    – Marius
    Sep 19 '13 at 13:20
3

A great example for this kind of functionality is the onepage checkout, for instance the piece that loads just the sidebar content:

controller:

public function progressAction()
{
    if ($this->_expireAjax()) {
        return;
    }
    $this->loadLayout(false);
    $this->renderLayout();
}

checkout.xml

<checkout_onepage_progress>
    <!-- Mage_Checkout -->
    <remove name="right"/>
    <remove name="left"/>

    <block type="checkout/onepage_progress" name="root" output="toHtml" template="checkout/onepage/progress.phtml">
        <block type="checkout/onepage_payment_info" name="payment_info">
            <action method="setInfoTemplate"><method></method><template></template></action>
        </block>
    </block>
</checkout_onepage_progress>

The trick is $this->loadLayout(false) and name="root" output="toHtml".

You can create a controller and a layout xml tag for your ajax response and set the block that you want to display as name="root".

2
  • Nice job on the extra explanation. Makes it more comprehensible!
    – Sander Mangel
    Sep 20 '13 at 12:04
  • No problem. I think we cross-posted by the way. :) Sep 24 '13 at 13:30
2

You can set this block as the root element in your layout XML which will cause only this block to return

<your_extension_handle>
  <block type="toxicfox/giftfinder_view" name= "root" template="template/toxicfox/giftfinder/view.phtml"/>
</your_extension_handle>

This does however mean that you need a controller that loads this handle. If not you need to create one in, for example, the IndexController.

Your handle would be toxicfox_index_ajax and in the IndexController the method would be

[...]
public function ajaxAction()
{
  $this->loadLayout();
  $this->renderLayout();   
}
[...]

Then calling the ajax via http://domain.com/toxicfox/index/ajax/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.