CTO, Software Engineer, Indie Hacker
$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
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_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
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.