Magento Expert Forum - Improve your Magento experience
Using $this in magento programming
The infamous $this found in Magento templates is a concept that I’ve found baffles beginner Magento developers. Here’s to hoping I can provide a half-decent explanation as to what it is, and where it comes from.
The most important point you should take away from this is: all Magento templates have a corresponding block instance. The $this exposed to us is the block instance, as if we were inside a method of the object (We actually are! See Mage_Core_Block_Template::fetchView).
Blocks are PHP classes that are designed as a place to put all of your business logic, instead of your templates. Blocks are normally reusable classes. A great example of the reusability of certain classes is the Mage_Core_Block_Template and Mage_Page_Block_Html_Pager classes. The core/template block is the foundation of the template system, allowing us the ability to load .phtml files from our themes. The page/html_pager block provides generic methods for paginating collections, such as isLastPage().
If you’re ever in any doubt as to which block type the template is using, just call echo get_class($this). Remember, it’s an object. This gives you a great place to start when hunting down methods available to the template.
View more threads in the same category:
Magento is an Ecommerce Platform Created On Open Source Technology, which provides online merchants with an exceptional flexibility and control over the content, look and functionality of their e-commerce store.
Last edited by PeterMaskolin; 10-03-2016 at 11:57 AM.
Tags for this Thread