Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Custom module not working with custom template? 
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Hi all
I have a custom (simple) module that works. However, I can’t seem to use it with a template (phtml file) !!

Get down and dirty:

app/etc/modules/Fido_All.xml (this is Fido_Example not Fido_Catalog)

<?xml version="1.0"?>
<config>
    <
modules>
        <
Fido_Catalog>
            <
active>false</active>
            <
codePool>local</codePool>
        </
Fido_Catalog>
        <
Fido_Example>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Fido_Example>
    </
modules>
</
config>

app\code\local\Fido\Example\etc\config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Fido_Example>
            <
version>0.1.0</version>
        </
Fido_Example>
    </
modules>
    <global>
        <
blocks>
            <
fido_example>
                <class>
Fido_Example_Block</class>
            </
fido_example>
        </
blocks>
    </global>
</
config>

app\code\local\Fido\Example\Block\View.php

class Fido_Example_Block_View extends Mage_Core_Block_Template
{
    
private $message;
    
    public function 
__construct() {
        parent
::__construct();
        
        
$this->setMessage('Hello World');
    
}
    
    
    
protected function setMessage($msg{
        $this
->message $msg;
    
}
    
    
public function getMessage() {
        
if($this->getMessage != ''{
            
return $this->message;
        
}
    }
    
    
protected function _toHtml() {
        
if($att $this->getMyAttribute && $this->getMyAttribute != ''{
            
return '<br />'.$att;
        
else {
            
return '<br />No Custom Attribute Found';
        
}
    }
}

Template file: app\design\frontend\default\fido\template\example\view.phtml

<?php

/**
 * Fido view template
 *
 * @see Fido_Example_Block_View
 * 
 */
?>
<div>
    <
span><strong>This is the output of the fido example:</strong></span><br />
    <
span style="color:#FF9933;">
<?php
echo $this->getMessage();
?>
    
</span>
</
div>

Usage (in CMS):  {{block type="fido_example/view" MyAttribute="Test" template="example/view.phtml" }}

So, the template doesn’t get used! The module (block/view.php) works - the _toHtml() function is getting fired and outputting as it should (well...it doesn’t pick up the custom attribute, so technically it’s not working fully...)

My CMS page is using the custom fido theme. I have the settings to use Fido as well (system->design)

I’m not sure at all why it isn’t picking up my template file.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Well, the reason is more than likely that you override _toHtml() and never call parent::_toHtml()

EDIT:
Mage_Core_Block_Template::_toHtml() is where the template is actually loaded and rendered.  By implementing your own version of _toHtml() and NOT calling parent::_toHtml(), you lose all that functionality.

try

class Fido_Example_Block_View extends Mage_Core_Block_Template
{
    
private $message;
    
    public function 
__construct() {
        parent
::__construct();
        
        
$this->setMessage('Hello World');
    
}
    
    
    
protected function setMessage($msg{
        $this
->message $msg;
    
}
    
    
public function getMessage() {
        
if($this->getMessage != ''{
            
return $this->message;
        
}
    }
    
    
protected function _toHtml() {
        $html 
parent::_toHtml();

        if(
$att $this->getMyAttribute && $this->getMyAttribute != ''{
            $html 
.= '<br />'.$att;
        
else {
            $html 
.= '<br />No Custom Attribute Found';
        
}

        
return $html;
    
}
}

EDIT:

Ok, this is the last edit, I promise

Your getMessage() and setMessage() are really pointless as well.

Your object inherits form Varien_Object ultimatly, which means that you can get/set ANY variable name you want on it, assuming it does not collide with an existing get/set method.

For instance,

$block->setGaGaGa(’baby’);
$block->getGaGaGa() == ‘baby’;

would work just fine WITHOUT making getter/setter methods.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Oh, and your custom attribute problem might be because you use:

{{block type="fido_example/view" MyAttribute="Test" template="example/view.phtml" }}

try:

{{block type="fido_example/view" myAttribute="Test" template="example/view.phtml" }}

or:

{{block type="fido_example/view" my_attribute="Test" template="example/view.phtml" }}

And see if that works for you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

no to both :( (Although I agree after reading other items using custom attributes that your second comment should have worked)

EDIT:

I’m reading over your edit.

Yes, the getter and setters are pointless as you point out - I just want to get something working rasberry

Thanks for your help, I can see my template now!

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

If you take out everything, does your phtml template get loaded?

class Fido_Example_Block_View extends Mage_Core_Block_Template
{
}

or

class Fido_Example_Block_View extends Mage_Core_Block_Template
{
    
public function __construct() {
        parent
::__construct();
        
$this->setMessage('Hello World');
    
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

It all works now -
For other’s benefit (no real functionality here, just a working module with template):

view.php:

<?php
/**
 * Example View block
 *
 * @codepool   Local
 * @category   Fido
 * @package    Fido_Example
 * @module     Example
 */
class Fido_Example_Block_View extends Mage_Core_Block_Template
{
    
private $message;
    private 
$att;
    
    protected function 
createMessage($msg{
        $this
->message $msg;
    
}
    
    
public function receiveMessage() {
        
if($this->createMessage != ''{
            
return $this->message;
        
else {
            $this
->createMessage('Hello World');
            return 
$this->message;
        
}
    }
    
    
protected function _toHtml() {        
        $html 
parent::_toHtml();
        
        if(
$this->att $this->getMyCustom && $this->getMyCustom != ''{
            $html 
.= '<br />'.$this->att;
        
else {
            $html 
.= '<br />No Custom Attribute Found';
        
}
        
        
return $html;
    
}
}

view.phtml:

<?php

/**
 * Fido view template
 *
 * @see Fido_Example_Block_View
 * 
 */
?>
<div>
    <
span><strong>This is the output of the fido example:</strong></span><br />
    <
span style="color:#FF9933;">
<?php
echo $this->receiveMessage();
?>
    
</span>
</
div>

My custom attribute is still amiss, even when fixing the format (my_custom="test" to become $this->getMyCustom)
(p.s. TY again for your help - altho we still had a little mis-comm between our edits causing some extra posts rasberry)

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Good deal. smile

So, adding in the parent::_toHtml() did the trick then?

BTW: Your code has a bug.

you treat $this->createMessage like a variable when it is actually a method.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

this is true - that should be “$this->message”

 
Magento Community Magento Community
Magento Community
Magento Community
 
QuadraVis
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-05-30
D - Erbach
 
nikefido - 11 September 2008 12:24 PM

this is true - that should be “$this->message”

Hi nikefido,

does your file now work correctly?
We are not able to let it work.

The question is, that we want to call a function or class from view.phtml but this does not work.
Do you have any idea or how did you solve this problem.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top