8

I always have challenges identifying the proper handles for everything, so please bear with me here. I've scoured Google, SE, and tried myriad variations, with no luck. (And yes, I've used techniques here: Debugging Layout Loading, but am unsure what to do with the output.

The Problem: The controller loads. The layout does not.

When I visit the page at localhost/magento/mymodule/ajax/cart

It echos 'HERE!'. The var_dump of the getLayouts generates:

array(4) { 
    [0]=> string(17) "mymodule_ajax_cart" 
    [1]=> string(13) "STORE_default" 
    [2]=> string(24) "THEME_frontend_theme_theme" 
    [3]=> string(19) "customer_logged_out" 
}

The Code

Custom controller:
app/code/local/mycompany/mymodule/controllers/AjaxController.php

class Mycompany_Mymodule_AjaxController extends Mage_Core_Controller_Front_Action
{
    public function cartAction() {
        echo 'HERE!';
        $this->loadLayout('mymodule_ajax_cart');
        var_dump($this->getLayout()->getUpdate()->getHandles());
        $this->renderLayout();
    }
}

Config file:
app/code/local/mycompany/mymodule/etc/config.xml

<config>
    <modules>
        <mycompany_mymodule>
            <version>1.0.0</version>
        </mycompany_mymodule>
    </modules>
    <global>
        <helpers>
            <mymodule>  
                    <class>Mycompany_Mymodule_Helper</class>
                </mymodule>
        </helpers>
    </global>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Mycompany_Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
        <layout>
            <updates>
                <mymodule>
                    <file>mymodule.xml</file>
                </mymodule>
            </updates>
        </layout>
    </frontend>
</config>

Layout file at
app/design/frontent/base/default/layout/mymodule.xml

<?xml version="1.0"?>
    <layout>
        <mymodule_ajax>
            <block type="core/text_list" name="content" output="toHtml" as="content" />
        </mymodule_ajax>
        <mymodule_ajax_cart>
            <reference name="content">
                <block type="core/template" template="mymodule/ajaxcart.phtml" />
            </reference>
        </mymodule_ajax_cart>
    </layout>

And finally, the template file at
app/design/frontend/theme/theme/template/mymodule/ajaxcart.phtml

<div style="border: 2px solid red">
    Hello world
</div>

Please advise. I know I'm missing something obvious, but cannot for the life of me find it.

  • 1
    Shouldn't you use mycompany_mymodule prefix in every handle ? (mycompany_mymodule_ajax for instance). – Niloct Oct 23 '13 at 16:42
  • @Nicolt - thanks for the comment. This very well may be the issue - when you say "Every Handle" - where specifically are you seeing it not used that it should be used? Are you referring to the layout xml file? The tutorials / write-ups I've read don't make it clear how those need to be named, so hence the handles I've posted. – cale_b Oct 23 '13 at 17:01
  • @Niloct - just grabbing your attention, I misspelled your handle earlier. Please see my comment above. – cale_b Oct 23 '13 at 17:16
  • Well, you first declare the module with mycompany_mymodule (first handle). Every xml tag that has the prefix mymodule without the mycompany_ prefix is bad IMHO. You should change all tag names to have the prefix mycompany_ before mymodule. – Niloct Oct 23 '13 at 17:24
18

To create a simple Ajax layout return try updating your code as follows:

Update your controller to use loadLayout(false); this will stop the "default" layout handle being added.

$this->loadLayout(false);
$this->renderLayout();

Next you can update your layout xml to simple just add the template you want. You will need the following for magento to actually output anything name="root" output="toHtml"

<?xml version="1.0"?>
<layout>
    <mymodule_ajax_cart>
        <block type="core/template" name="root" output="toHtml" template="mymodule/ajaxcart.phtml" />
    </mymodule_ajax_cart>
</layout>

For a complete "simple response" that will be accessible under /ajaxtest/index/index you can do the following:

/app/etc/modules/Custom_Ajax.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Ajax>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_Ajax>
    </modules>
</config>

/app/code/local/Custom/Ajax/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Ajax>
            <version>0.0.1</version>
        </Custom_Ajax>
    </modules>
    <frontend>
        <layout>
            <updates>
                <custom_ajax>
                    <file>custom_ajax.xml</file>
                </custom_ajax>
            </updates>
        </layout>
        <routers>
            <ajaxtest>
                <use>standard</use>
                <args>
                    <module>Custom_Ajax</module>
                    <frontName>ajaxtest</frontName>
                </args>
            </ajaxtest>
        </routers>
    </frontend>
</config>

/app/design/frontend/base/default/layout/custom_ajax.xml

<?xml version="1.0"?>
<layout>
    <ajaxtest_index_index>
        <block type="core/template" name="root" output="toHtml" template="custom_ajax.phtml" />
    </ajaxtest_index_index>
</layout>

/app/code/local/Custom/Ajax/controllers/IndexController.php

<?php
class Custom_Ajax_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout(false);
        $this->renderLayout();
    }
}

/app/design/frontend/base/default/template/custom_ajax.phtml

Hello world!
  • 1
    Accepted. Beautiful - the key was the mods to the <block> tag in the layout file, but I learned a lot from your answer. Thank you. – cale_b Oct 24 '13 at 16:48
  • Also the frontName tag value is used in the layout handle tag, I didn't know about that. Very good. – Niloct Oct 24 '13 at 20:07

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.