5

I got a template called dashboard.phtml and i want to add a function there. I thought it was a bad practice to write it directly in the template it self so where should i put it?

The class for this one is the Mage_Customer_Block_Account_Dashboard.

I thought about helpers but in a other Question they say, that helpers never should be used.

The function is called 5 times in the template.

MCSell
  • 83
  • 7

3 Answers3

5

Best solution would be to override this block in a custom module to add your method.

etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Vendor_Module>
            <version>0.0.1</version>
        </Vendor_Module>
    </modules>
    <global>
        <blocks>
            <customer>
                <!--
                Mage_Customer_Block_Account_Dashboard
                -->
                <rewrite>
                    <account_dashboard>Vendor_Module_Block_Customer_Account_Dashboard</account_dashboard>
                </rewrite>
            </customer>
        </blocks>
    </global>
</config>

Then you can create Block/Customer/Account/Dashboard.php and add your method

<?php
class Vendor_Module_Block_Customer_Account_Dashboard extends Mage_Customer_Block_Account_Dashboard {

    public function yourMethod() {
    }

}

Now, you can use your method in your template by calling: $this->yourMethod()

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
  • ok, so what does the xml do here exactly? is the origin class extended here? – MCSell Jun 06 '16 at 07:36
  • 1
    @MCSell yes the XML declares that your custom class is a rewrite of the native block class and thus will be used instead of the native class. It is indeed an extend – Raphael at Digital Pianism Jun 06 '16 at 07:38
  • 1
    @MCSell feel free to mark your question as answered so it will benefit for others ;) – Raphael at Digital Pianism Jun 06 '16 at 07:41
  • one question for my understanding and to go really sure :D that means, if i upgrade my magento it will take changes on the overwrited class if i dont overwrite original functions? – MCSell Jun 06 '16 at 07:42
  • 1
    @MCSell absolutely. In your case, as you are adding a function that does not exist in Magento, I suggest you use a very custom name for your method to ensure a method with the same name won't be added to Magento in the future. As you don't override any native method in your custom block, there's almost no change that your code will break after an upgrade – Raphael at Digital Pianism Jun 06 '16 at 07:44
5

The alternative to rewrite the block is to replace it with another block in layout.xml so you still have access to the original implementation. As Amit mentioned in comments, this can/should be done via local.xml

The problem is, if you replace the block, you have to replay all changes to the block, like adding other blocks, calling actions, etc.

The worst problem is, if you install a plugin, you have to manually do all the layout stuff on the dashboard block.

All in all the rewrite is the better option most of the time, but this is an option from time to time.

Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182
4

First, if one of the previous solutions from Raphael and Fabian works for you, go for that! Having functions you need in the template as methods of the block is the cleanest way to go.

But there might be cases where this is not easily applicable, like if there is already another extension rewriting this block.

In this case, there is another solution that I used a few times: Define your function as anonymous function.

Example

<?php
$uppercaseName = function($item)
{
    return strtoupper($this->htmlEscape($item->getName()));
}
?>

<ul>
<?php foreach ($this->getItems() as $item): ?>
    <li><?php echo $uppercaseName($item); ?></li>
<?php endforeach ?>
</ul>

How is that different from defining a plain old function in the template?

  1. you don't add anything to the global namespace => no conflicts or side effects
  2. you can use the same template twice without errors about redefining an existing function
Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421