3

New to Magento. Trying to write a custom block and just make it show up at the most basic level. Been going crazy for two days, comparing my work to those of the other modules we are using plus research.

I think the issue lies somewhere with Magento not recognizing the block as being part of the global config. I need the block just to appear when I place it in local.xml!

At app\code\local\Csos\Spuritem\etc\config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Csos_Spuritem>
            <version>0.1.0</version>
        </Csos_Spuritem>
    </modules>

    <global>
        <blocks>
            <spuritem>
                <class>Csos_Spuritem_Block</class>
            </spuritem>
        </blocks>
    </global>
</config>

The block definition app\code\local\Csos\Spuritem\Block\Item.php:

<?php

class Csos_Spuritem_Block_Item extends Mage_Core_Block_Abstract {

}

When I use print_r(Mage::getConfig()->getNode()->xpath('//global//blocks')); shows that my module is active:

[spuritem] => Mage_Core_Model_Config_Element Object
                (
                    [class] => Csos_Spuritem_Block
                )

In frontend\base\default\layout\local.xml,

<layout version="1.0">
    <default>
        <reference name="content">
            <block type="spuritem/item" name="spuritem.forever" template="csos/spuritem/spuritem.phtml"/>
            <block type="core/template" name="showit" template="csos/showme.phtml" />
        </reference>
    </default>
</layout>

and finally, app\design\frontend\base\default\template\csos\spuritem\spuritem.phtml

<h1 id="spuritem">This is where my stuff will go.</h1>

In the current state, this produces nothing on the homepage. However, if in the local.xml I change

I have allowed block permissions in the back end for "spuritem/item".

<block type="spuritem/item" name="spuritem.forever" template="csos/spuritem"/>

to

<block type="core/templatem" name="spuritem.forever" template="csos/spuritem"/>

The homepage will produce the text as expected.

What am I doing wrong?

1 Answer 1

4

Change your block to extend from Mage_Core_Block_Template and not from Mage_Core_Block_Abstract. In Mage_Core_Block_Abstract::_toHtml() looks like this

/**
 * Override this method in descendants to produce html
 *
 * @return string
 */
protected function _toHtml()
{
    return '';
}

So your block is rendered but the output is empty string. Mage_Core_Block_Template has the following code which renders the template file:

/**
 * Render block HTML
 *
 * @return string
 */
protected function _toHtml()
{
    if (!$this->getTemplate()) {
        return '';
    }
    $html = $this->renderView();
    return $html;
}

Alternatively you can create _toHtml() method in your block and prepare your own code for rendering content in case default one is for some reasons not suitable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.