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.

3 Answers 3


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


<?xml version="1.0" encoding="UTF-8"?>

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

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()

  • ok, so what does the xml do here exactly? is the origin class extended here?
    – MCSell
    Jun 6, 2016 at 7: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 Jun 6, 2016 at 7:38
  • 1
    @MCSell feel free to mark your question as answered so it will benefit for others ;) Jun 6, 2016 at 7: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 6, 2016 at 7: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 Jun 6, 2016 at 7:44

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.

  • 1
    i am like to change via local.xml.
    – Amit Bera
    Jun 6, 2016 at 8:21

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.


$uppercaseName = function($item)
    return strtoupper($this->htmlEscape($item->getName()));

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

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.