Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento - create a global function

I want to create a function that can be accessed by all *.phtml files. Where should i place this function in the magento framework?

like image 591
John Avatar asked Dec 09 '22 11:12

John


2 Answers

For down and dirty things, you can always define it in index.php. for example, I always put this function there:

function dumpit($obj)
{
  print '<pre>';
  print_r($obj);
  print '</pre>';
}

Then you can quickly call this routine from anywhere, without having to remember all the other overhead function names to get at the helper.

like image 123
Billy Avatar answered Dec 11 '22 23:12

Billy


You should create a module and a helper class in that module (Usually MyCompany_Mymodule_Helper_Data by default). Then, add your function to that helper class. You can get to that function in your PHTML like this:

Mage::helper("mymodule")->someFunction();

Hope that helps!

Thanks, Joe

like image 36
Joe Mastey Avatar answered Dec 11 '22 23:12

Joe Mastey