Question

What would be the downside of using Mage::getModel (+ load, getCollection) in a template?

I've seen quite a few templates with this logic put directly in the template instead of using the block?

Was it helpful?

Solution

Templates should not contain any non-presentational logic which would have to be duplicated based on the sole requirement of presentational changes. A good way to think about each line of template is, "If I need to change the markup, what will I have to duplicate in the new template?"

In Magento there are two architectural drivers which indicate template content decisions. Whereas a template in Magento is only rendered via a block class instance, it makes sense to put any non-presentational code (such as model and collection instantiation) into the block instance. This is even more important given that layout XML makes it possible for a block to be initialized with one template and updated to use another. A frontend developer should be able to copy a template to a custom theme or specify an entirely new template solely via layout XML without having to worry about ensuring that the proper objects are present and correctly configured.

OTHER TIPS

We use classes, templates, configurationfiles, etc. to sort things. The one who want to maintain the code after you will not search there, if he is "only" middle class magento dev :-)

In the beginning you look everywhere and implement things everywhere, then you learn how to do it better and only look in the place where things belong and in the end you learn, that there are beginners which implement things where they don't belong and you look again everywhere.

No offense ;-) I'm sure you understand what I mean :-)

I get some models in the template too, but loading a collection should be somewhere else, it feels wrong.

A template is for showing informations, the logic belongs into a class definition.

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