I’m currently working on a custom module in which I have a controller extending Mage_Core_Controller_Front_Action to handle requests. This controller loads and renders the blocks which I have defined in the xml layout file.
Each one of these blocks can have their own block controller assigned in the type field.
What I would like to know is, what’s best practice when it comes to assign variables to template files? Assuming I have a list of products that I would like to display on a template. Do I create a function in the block controller and call this function in the template file, or should I assign the list of products to the block in the core controller?
I’m aware this question is quite theoretical, was just wondering what the benefits of each option are and what’s best practice in Magento.
I’m currently working on a custom module in which I have a controller extending
Share
Good practice, in general, is to keep blocks “standalone” as much as possible. This would mean, in this case, to assign the template/view variables in the block controller. This would make it possible to re-use your block on other pages without changes and keeps everything in one place. So if something changes in the specified block, you only have to change the block controller, while otherwise you might have needed to change the front controller as well.
Offcourse in some situations the block uses data from the front-controller. But this is a specific situation and should be avoided as much as possible.