Magento – Handling AJAX requests to display template partials

ajaxoverridestemplate

Pretty new to magento and I'm setting up a custom module to override the core newsletter. All I really want to do is add ajax submit to the form, which is straightforward so far, and also allow the form to be both displayed in a lightbox and on the page, depending on the context of the request. i.e within my custom module controller, would I use something like the below when the request is via AJAX, or is there a more recommended way to do this kind of thing? Thanks for any help

$html = Mage::getSingleton('core/layout')->createBlock('core/template')
            ->setTemplate('my_module/form.phtml')->toHtml();
$this->getResponse()->setBody($html);

EDIT: update after advice from beeplogic. I now get a blank page with the following code. Is there anything obvious I'm doing wrong here?

What I have is a MyCompany_Newsletter_SubscriberController which has a newAction(), overriding core Newsletter controller newAction. My controller action is called fine. I'm trying to target the default newsletter/subscriber/new In my module config.xml, I have:

<frontend>
    <routers>
        <newsletter>
            <args>
                <modules>
                    <MyCompany_Newsletter before="Mage_Newsletter">MyCompany_Newsletter</MyCompany_Newsletter>
                </modules>
            </args>
        </newsletter>
    </routers>
    <layout>
        <updates>
            <newsletter>
                <file>newsletter.xml</file>
            </newsletter>
        </updates>
    </layout>
</frontend>

Then in default/default/layout/newsletter.xml, I have:

<layout>
    <newsletter_subscriber>
        <block type="core/text_list" name="content" output="toHtml" as="content"/>
    </newsletter_subscriber>
    <newsletter_subscriber_new> <!--NOTE: this will be the generated action layout handle -->
        <reference name="content">
            <block type="core/template" template="newsletter/subscribe.phtml" />
        </reference>
    </newsletter_subscriber_new>
</layout>

Best Answer

You can still use Magento's XML layout configuration to send snippets of HTML. In your controller you will need to specify the handle's to render:

public function formAction()
{
    $this->loadLayout('newsletter_ajax_form');
    $this->renderLayout();
}

Define the layout update for your module:

<frontend>
    <layout>
        <updates>
            <newsletter_ajax>
                <file>mycompany/newsletter.xml</file>
            </newsletter_ajax>
        </updates>
    </layout>
</frontend>

In the layout file: app/design/frontend/package/theme/layout/mycompany/newsletter.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <newsletter_ajax>
        <block type="core/text_list" name="content" output="toHtml" as="content"/>
    </newsletter_ajax>
    <newsletter_ajax_form> <!--NOTE: this will be the generated action layout handle -->
        <reference name="content">
            <block type="core/template" template="my_module/form.phtml" />
        </reference>
    </newsletter_ajax_form>
</layout>
Related Topic