7

I have gone through various Magento tutorials as well as a Magento book on layout, but I'm stuck with the following problem.

I have created a custom module, located in app/code/local/Company/Module/ . I have created a block Company_Module_Block_Ablock located in Company/Module/Block/Ablock.php. I have defined <config> ... <frontend><layout><updates><module><file>module.xml in Company/Module/etc/config.xml

config.xml also has

<global>
   <blocks>
      <module>
         <class>Company_Module_Block</class>
      </module>
   </blocks>
</global>

Inside this module.xml I have:

<layout>
    <company_module_index_index>
        <reference name="content">
            <block type="module/ablock" name="myablock" template="module/ablock.phtml" />
        </reference>
    </company_module_index_index>
</layout>

I have created a Company/Module/controllers/IndexController.php and have a indexAction defined there which does

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

But whatever I try I'm unable to get my ablock.phtml to display. ablock.phtml is in app/design/frontend/company/default/template/module/ablock.phtml. The theme has been enabled and is generally working on the site.

I have even tried to change module.xml inside layout so its not even using a template and even this is not displaying anything. Like this -

<reference name="content">
<block type="core/text">
    <action method="setText">
        <text>Testing</text>
    </action>
</block>
</reference>

By the way the custom theme works fine otherwise, other pages have content displayed in them in the correct place.

I have SetEnv MAGE IS DEVELOPER MODE 1 in .htaccess which is supposed to help display even warnings and things. But my systems.log and exceptions.log have no errors in them. (Yes logging is turned on)

Anyone have any advise on how to troubleshoot this or can spot a mistake in the configuration or code?

My next option seems to be hacking the core module code and logging where my module.xml is being loaded and parsed to see what is going on there.

Thanks.

arunkumar
  • 32,803
  • 4
  • 32
  • 47

4 Answers4

33

Steps to Debug Layout Update XML issues:

  1. Is your XML file (local.xml or module.xml) being loaded into the system

  2. Does the handle tag you used in your layout file match a handle being generated for your request?

The quickest way to debug step 1 is, while in developer mode with errors showing, deliberately introduce a non-well-formed error to your Layout Update XML file.

<layout <!-- notice missing closing taglayout -->
    <company_module_index_index>
        <reference name="content">
            <block type="module/ablock" name="myablock" template="module/ablock.phtml" />
        </reference>
    </company_module_index_index>
</layout>

If developer mode is on and you've cleared your cache, loading any page with the above in place will cause an error. This lets you know that Magento is trying to load your XML file. If the page loads without problem, that means you have your XML file in the wrong location, or you've misconfigured your XML in config.xml.

Next up is your checking your layout handle. You'll want to make sure you're using the right one. You can view the layout handles that were used for a particular request by calling the following after your loadLayout and renderLayout request

//from a controller action
$this->loadLayout();
$this->renderLayout();
var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
exit("bailing early at ".__LINE__." in ".__FILE__);

I've found the above items usually take care of 90% of layout problems. Be sure to go through the process a few times, as it's easy to miss one step and assume something's all right when it's not. Taking my usual risk of being a shill, part of why I created Commerce Bug (commercial debugging extension) was to provide this information quickly, and at a glance, to help with debugging problems.

Based on your comments below, the problem appears to be the layout handle you're using. The handle that's generated by the system is

module_module_test

However, the handle you're defining in your layout.xml is

company_module_index_index

This is the "full action name" handle. The typical syntax for this is

frontname_controllername_actionname

Change the handle to module_module_test and you should be set.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Thanks Alan, its your book in fact on layout that I have open as reference. Using a malformed layout XML causes a parsing error as expected, so you are right its obviously loading it. Trying the var_dump gave me this `array(5) { [0]=> string(7) "default" [1]=> string(13) "STORE_default" [2]=> string(25) "THEME_frontend_company_default" [3]=> string(22) "module_module_test" [4]=> string(19) "customer_logged_out" } ` I have a ModuleController.php which defines a testAction(). If I do an echo "test" in testAction, the text shows up on the page, albeit not within the default template for the site. – arunkumar Aug 13 '11 at 19:22
  • The last comment ran too long to add this - I have tried to install your LayoutViewer and ConfigViewer but it doesn't appear to work on Magento 1.6. – arunkumar Aug 13 '11 at 19:22
  • accepted and +1 Thanks! I could have sworn I tried that at first. But perhaps something else wasn't working then. How come I can refer to it as just module and not have to say company_module, does it pick up module from the config.xml - frontend / routers/ module section ? – arunkumar Aug 13 '11 at 20:24
  • 1
    The handle name is taken from the routers/[foo]/* section for config.xml. I can't remember if it uses the value in the frontName tag, or if it uses the name of the tag itself ("[foo]") – Alana Storm Aug 13 '11 at 20:44
  • Thanks Alan. I ran a test, you are right, its taking it from the module name in config.xml – arunkumar Aug 13 '11 at 21:29
  • It should be noted that the layout XML will only be loaded if the page uses layout. So don't test this with something like a simple text output controller action like I did. – Fabian Schmengler Aug 18 '11 at 08:58
  • @AlanStorm Thank you for your answer. Saved me some hair! +1 – Tash Pemhiwa Jan 31 '13 at 12:24
  • @AlanStorm What if I conditionally wish to change layout handler from controller action? I am asking in comment not in a separate question.. For knowledge gain purpose.. Hope to get comment from you.. Lets say I want to change based on one condition to load module_module_test1.. then what can you say? – Kamal Joshi Apr 05 '13 at 16:11
  • @Kamal I don't understand the question – Alana Storm Apr 05 '13 at 17:16
  • Lets say that I am passing parameters from url and based on that I want to change layout.. then from controller I can change handler.. – Kamal Joshi Apr 05 '13 at 21:46
  • Better to use: Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles()); Instead of: var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles()); a clearer format! – netusco Feb 18 '16 at 15:23
1
  • did you disable or update Magento cache?
  • is your module enabled? Is it listed in System, Configuration, Advanced, Advanced?
  • which class does your Block extend?

Regards, Alessandro

  • Cache is disabled. The module is enabled. It extends Mage_Checkout_Block_Onepage_Abstract . But I think the problem is in the layout.xml or simply in some configuration setup. Since even the simple built-in 'core/text' block type does not work. – arunkumar Aug 13 '11 at 06:44
0

In magento 2.3.2, you have to enter this code for getting layout handle

echo $this->getRequest()->getFullActionName();

So the controller execute function like this

    public function execute()
    {

        echo $this->getRequest()->getFullActionName();exit;
    }

And check the value printed on screen if it is same with layout handle name

Clarenceli
  • 31
  • 5
0

I cannot tell without seeing all of your config.xml, you might be missing a section like this:

<frontend>
    <routers>
        <company_module>
            <args>
                <frontName>module</frontName>
                <module>Company_Module</module>
            </args>
            <use>standard</use>
        </company_module>
    </routers>
</frontend>

Then, to reach the indexAction or your indexController you would need the address www.example.com/module. That will mean the layout handle company_module_index_index is used when you load the layout. BTW, the posted layout XML is broken, the closing tag does not match the opening tag.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • 1
    Wouldn't the layout handle be `module_index_index`, not `company_module_index_index`? – Nick Aug 13 '11 at 11:49
  • @Nick. That is a good question. The `frontName` is `module` but the XML node is called `company_module`. That node is used when building a layout handle. Just look at the admin pages; all URLs start with `admin` but the layout handles start with `adminhtml` because it matches the router. – clockworkgeek Aug 13 '11 at 13:11
  • Sorry I should have mentioned that. Yes the routers section is there. Viewing www.example.com/module shows my basic site with no content in the middle. You can see the IndexController.php that I have defined above. That was a typo here. Have edited the question. Thanks – arunkumar Aug 13 '11 at 14:09
  • To clarify the typo was in the closing block of XML in layout here. The actual XML doesn't have the typo. – arunkumar Aug 13 '11 at 14:49
  • @Nick +1 yes that was the problem. – arunkumar Aug 13 '11 at 20:19