The template system that ships with Magento may seem a little complex at first, but the price we pay in complexity affords us a great deal of flexibility and power, as we’ll discover.
There are three components to the template system:
PHP template files
PHP block classes
XML layout configuration
The template files contain what you’d expect a template system to handle, such as the HTML, JavaScript, and some PHP.
The block classes allow us to move the reusable functionality from the PHP template files into PHP classes, which can be used again on different template files in the future. As a rule, all template files will have an associated block class. Block classes are just normal PHP classes, and can be accessed in the template through the $this variable.
Various helper methods, such as getSkinUrl($path) or getUrl($path), are contained in all block classes. These methods are then used in a template file, by calling: $this->getSkinurl(‘images/logo.png’), for example.
Along with providing useful methods to the template files, blocks are also used to decide how to render the template file to the user. Each block has a toHtml() method for this purpose. Usually, the toHtml() method will simply parse the template file and output it to the user, but we could override that functionality and return anything we like, such as XML or JSON.
Finally, the XML layout configuration files are the “glue” that pulls together the entire set of template files to be rendered in the browser. In the XML, we can specify what PHP template/block combinations we’d like to load, and the order that we’d like to display them on the page.