Magento – How to Pass an Array from Block File to Template File

arrayblockscollection;template

I have a collection of Dealer Stores. I have implemented a function that calculates the distance of all stores from the location of the customer and i have an array of respective distances. I have added the logic in Block.php file.

Now I want this array to be accessed from a template file. How do i implement this?

All answers i get from web are exact opposite of what is want – Passing data from template file to block file

Any help would be appreciated..

Best Answer

The template is renderer inside of the block class context, this means whatever information you put in $block->setMyPersonalVar($value) can be read with $this->getMyPersonalValue() inside the template. So as long as you have a reference to the block class in $block, you can set any value on it.

This can be used in the block class itself, e.g. in toHtml or in the controller class $this->getLayout()->getBlock('nameInLayout')->setMyPersonalValue() or wherever you want, e.g. in observers with Mage::app()->getLayout()->getBlock('nameInLayout')->setMyPersonalValue()

The alternative is to use Mage::register('name', $value) and Mage::registry('name')

If this doesn't work my first guess is: The code is executed in the wrong order

Related Topic