Magento – How to pass a variable from Controller to Block

developmentmagento-1.8magento-1.9

Hi I am encountering an error passing variables from controller to Block. I am trying to do a search function.

Error Message:

Fatal error: Call to a member function setCustomerRegistries() on a
non-object in
E:\xampp\htdocs\magento2\app\code\local\Mdg\Giftregistry\controllers\SearchController.php
on line 27

controller

<?php
class Mdg_Giftregistry_SearchController extends Mage_Core_Controller_Front_Action{

    public function indexAction(){
        $this->loadLayout();
        $this->renderLayout();
        return $this;
    }
    public function resultsAction(){
        $this->loadLayout();

        if ($searchParams = $this->getRequest()->getParam('search_params')) {

            $results = Mage::getModel('mdg_giftregistry/entity')->getCollection();

            if($searchParams['type']){
                $results->addFieldToFilter('type_id', $searchParams['type']);
            }

            if($searchParams['date']){
                $results->addFieldToFilter('event_date', $searchParams['date']);
            }

            if($searchParams['location']){
                $results->addFieldToFilter('event_location', $searchParams['location']);
            }
            $this->getLayout()->getBlock('mdg_giftregistry.search.results')->setCustomerRegistries($results);
        }

        $this->renderLayout();
        return $this;
    }


}

Block

<?php
class Mdg_Giftregistry_Block_List extends Mage_Core_Block_Template
{
    public function getCustomerRegistries()
    {
        $collection = null;
        $currentCustomer = Mage::getSingleton('customer/session')->getCustomer();
        if($currentCustomer)
        {
            $collection = Mage::getModel('mdg_giftregistry/entity')->getCollection()
                ->addFieldToFilter('customer_id', $currentCustomer->getId());
        }
        return $collection;
    }
}

Template

<?php
$_collection = $this->getCustomerRegistries();
$helper = Mage::helper('mdg_giftregistry')
?>
<div class="customer-list">
    <?php if(!$_collection->count()): ?>
        <h2><?php echo $this->__('You have no registries.') ?></h2>
        <a href="<?php echo $this->getUrl('giftregistry/index/new') ?>">
            <?php echo $this->__('Click Here to create a new Gift Registry') ?>
        </a>
    <?php else: ?>
        <ul>
            <?php foreach($_collection as $registry): ?>
                <li>
                    <h3><?php echo $registry->getEventName(); ?></h3>
                    <p><strong><?php echo $this->__('Event Name:') ?> <?php echo $registry->getEventName(); ?></strong></p>
                    <p><strong><?php echo $this->__('Event Location:') ?> <?php echo $registry->getEventLocation(); ?></strong></p>
                    <a href="<?php echo $this->getUrl('giftregistry/view/view', array('_query' => array('registry_id' => $registry->getEntityId()))) ?>">
                        <?php echo $this->__('View Registry') ?>
                    </a>
                    <?php if($helper->isRegistryOwner($registry->getCustomerId())): ?>
                        <a href="<?php echo $this->getUrl('giftregistry/index/edit', array('_query' => array('registry_id' => $registry->getEntityId()))) ?>">
                            <?php echo $this->__('Edit Registry') ?>
                        </a>
                        <a href="<?php echo $this->getUrl('giftregistry/index/delete', array('_query' => array('registry_id' => $registry->getEntityId()))) ?>">
                            <?php echo $this->__('Delete Registry') ?>
                        </a>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>

Best Answer

You can use register function in magento.

Try this:

In controller

$data = 'your data that you want to pass to block';
Mage::register('any_name', $data);

Now in your block

Mage::registry('any_name')

PS any_name is just name given to hold that data and which should be same while calling it in block

Related Topic