Diego
Diego

Reputation: 7562

Magento - Block is not rendered

I'm developing my second Magento module, which should retrieve some data and render a block containing it. Such block would then be used by other pages.

At the moment, I have almost the whole module in place, but, for some reason, the block is not rendered when I call the controller method. I put some debug information, and I could see that the block's __construct() method is called correctly, but then the template doesn't seem to be loaded and the returned page is blank.

Here's the code for the block, which I copied from another module and modified:

class Company_CustomerData_Block_CustomerSummary extends Mage_Core_Block_Template {
    const _TEMPLATE = 'customerdata/customersummary.phtml';

    public function __construct() {
    // This method is called correctly
        parent::_construct();
        $this->setTemplate(self::_TEMPLATE);
    }
}

The file customersummary.phtml is in app/design/frontend/base/default/template/customerdata, which should be the correct place (or, at least, I think it is). Its content is the following:

It works!

Just some plain text. No tags, no code, nothing. I don't mind that it's a static text, it will be populated with data once complete.

In case of need, here's the code for the Controller (I removed the parts where the data is retrieved, as they don't make a difference):

public function dashboardAction() {
    // Customer Data to render in the block
    $CustomerData = array(); // Data is retrieved elsewhere

    $this->getResponse()->setBody(
        $this->getLayout()->createBlock('customerdata/customersummary')
        ->toHtml()
    );
}

What could I be doing wrong? I'm afraid I made some stupid mistake again, but I really can't see it.

Finally two more questions:

Thanks in advance for the help.

Upvotes: 0

Views: 2812

Answers (2)

Diego
Diego

Reputation: 7562

Although Jim's Answer is correct, I'm adding a second part because I found out what else was wrong: symbolic links. As I usually do when I develop plugins for a framework, I use symlinks to avoid copying the files over and over again. However, Magento uses function RealPath(), which resolves the symlink to its full path. As a result, the Template file to be loaded resides in a path outside Magento installation directory, and it can't be loaded for security reasons. This makes perfectly sense, pity it's just not very visible.

To fix the issue, I enabled Allow Symbolic Links in configuration on my Development PC, and now the Template is loaded and rendered correctly.

I think that Magento could do with a Log Viewer in the Admin interface. If there isn't a module that does it already, perhaps I should create one myself.

Thanks everybody for the help.

Upvotes: 2

Jim OHalloran
Jim OHalloran

Reputation: 5908

Most classes on Magneto derive from Varien_Object. Varien_Object (and it's descendents) claim PHP's constructor (__construct) for themselves, and provide an _construct callback which you can use for whatever you like. What this means is that if you override the native PHP constructor (__construct) you need to remember to do a few things:

  1. Accept the same number of parameters as the base class, and...
  2. Call the parent constructor (parent::__construct) with the parameters your constructor was supplied.

Or, alternatively, use the _construct callback supplied by Varien_Object and you're done. There's no need to remember to call parent::_construct if you're using the Magento callback.

So to fix your code snippet above, you can either change...

public function __construct() {

... to ...

public function _construct() {

This will switch you over the using the Magento callback. Or you can change...

parent::_construct();

.. to ...

parent::__construct();

... to call the parent class' constructor. Remembering to add in the parameters that the parent class supplies.

Alan Storm write a great article about Magento's Block Lifecycle and call back methods recently, which might be of assistance.

To your other questions:

  • Passing information from the controller to a block is generally done via Magento's registry.
  • Rendering your template as part of a page requires that you create layout instructions (written in XML) which load your block and place it inside a parent block.

Upvotes: 3

Related Questions