views:

417

answers:

1

I am currently working on a Magento extension, and I have overridden a core controller, which works fine.

I have now added a new action to my controller. Problem is that whenever I call the action a blank page is produced. If I echo something it is displayed correctly.

Therefore I dug into the core of the Customer module and controllers. I saw there that methods like indexAction() implement the layout this way:

<?php
public function indexAction()
{
  $this->loadLayout();
  $this->_initLayoutMessages('customer/session');
  $this->_initLayoutMessages('catalog/session');

  $this->getLayout()->getBlock('content')->append(
      $this->getLayout()->createBlock('customer/account_dashboard')
  );
  $this->getLayout()->getBlock('head')->setTitle($this->__('My Account'));
  $this->renderLayout();
}

I transferred this to my own action and the layout is now rendered correctly. Now for the question:

No matter what I enter into the ->createBlock('...') call, nothing is rendered into the content area.

How do I specify the location of my own block to be rendered as the content of the page while still decorating it with the layout?

I tried fiddling with the xml files in /design/frontend/base/default/layout/myaddon.xml but couldn't really make it work.

+1  A: 

Covering the entirety of the Magento layout system in a single StackOverflow post is a little much, but you should be able to achieve what you want with the following.

    $block = $this->getLayout()->createBlock('Mage_Core_Block_Text');
    $block->setText('<h1>This is a Test</h1>');
    $this->getLayout()->getBlock('content')->append($block);

Starting from the above, you should be able to build up what you need. The idea is you're creating your own blocks, and then append them to existing blocks in the layout. Ideally, you're creating your own block classes to instantiate (rather than Mage_Core_Block_Text), and using their internal template mechanism to load in phtml files (separating HTML generation from code generation).

If you're interested in learning the internals of how the layout system works, you could do a lot worse than to start with an article I wrote on the subject.

Alan Storm