Question

I'm working on cleaning up some codes where the previous person wrote a whole bunch of PHP code in PHTML files. So, for example, I made the PHP code put in list.phtml as part of the overriden List.php.

Now, I'm looking at view.phtml for the product-view page, and it calls a lot of child HTMLs. In particular, I'm looking at media.phtml and configurable.phtml, where there is very similar set of PHP codes. How can I integrate these PHP codes into one place (similar to what I did with List.php), so that each of these PHTML can call the same function (for example, getProductUrls())?

Additionally, is it possible to have variables available in media.phtml also available in configurable.phtml without having to call getProductUrls() twice (once in media and once in configurable)?

Was it helpful?

Solution

You can put all your custom code in one helper and have it available everywhere by calling:

Mage::helper('alias')->doSomething($withSomeParameter);

Also you can use the registry to simulate "Global" variables.
Set a variable like this:

Mage::register('key', $value);

and read it anywhere later with:

Mage::registry('key');

Just make sure you don't set the same key twice. It won't work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top