0

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

1 Answer 1

3

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.

3
  • As an experiment - registering closures with Mage::registry(): gist.github.com/philwinkle/6526768
    – philwinkle
    Commented Sep 11, 2013 at 17:14
  • I'm reading Alan Storm's article on it, but any common pitfalls to watch out for with Mage::registry?
    – laketuna
    Commented Sep 17, 2013 at 21:00
  • It's somehow similar to using a global variable. If you can avoid it, do it, if not...don't worry to much.
    – Marius
    Commented Sep 17, 2013 at 21:04

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.