Pass a variable to a .phtml Magento block.

Here’s a simple snippet to pass a PHP variable from a controller ( or whatever magento file ) to a block, using the createBlock() method.

When you call a block:

pass-variable-mageno-block-template-phtml

with the setData() method you’ll be able to access your variable with:

<?php $this->key; ?>

$this What is it?

When we work at front-end in magento (in templates), we often see $this or $this-> … If you don’t know that,  will you ask a question for it, sometimes? I think this answer is YES

This is problem which 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 (See Mage_Core_Block_Template::fetchView).

Blocks in Magento are PHP class that are designed as a place to put all of your business logic, instead of your templates. Blocks are normally reusable class. A great example of the reusability of certain class is the Mage_Core_Block_Template and Mage_Page_Block_Html_Pager class. 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 provide generic methods for paginating collection, such as isLastPage().

If you are 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.