title: How to pass variables from Controller to Block
body: <p>Hi I am encountering an error passing variables from controller to Block. I am trying to do a search function.</p>
  
  <p><strong>Error Message:</strong></p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p><strong>controller</strong></p>
  
  <pre><code>&lt;?php
  class Mdg_Giftregistry_SearchController extends Mage_Core_Controller_Front_Action{
  
      public function indexAction(){
          $this-&gt;loadLayout();
          $this-&gt;renderLayout();
          return $this;
      }
      public function resultsAction(){
          $this-&gt;loadLayout();
  
          if ($searchParams = $this-&gt;getRequest()-&gt;getParam('search_params')) {
  
              $results = Mage::getModel('mdg_giftregistry/entity')-&gt;getCollection();
  
              if($searchParams['type']){
                  $results-&gt;addFieldToFilter('type_id', $searchParams['type']);
              }
  
              if($searchParams['date']){
                  $results-&gt;addFieldToFilter('event_date', $searchParams['date']);
              }
  
              if($searchParams['location']){
                  $results-&gt;addFieldToFilter('event_location', $searchParams['location']);
              }
              $this-&gt;getLayout()-&gt;getBlock('mdg_giftregistry.search.results')-&gt;setCustomerRegistries($results);
          }
  
          $this-&gt;renderLayout();
          return $this;
      }
  
  
  }
  </code></pre>
  
  <p><strong>Block</strong></p>
  
  <pre><code>&lt;?php
  class Mdg_Giftregistry_Block_List extends Mage_Core_Block_Template
  {
      public function getCustomerRegistries()
      {
          $collection = null;
          $currentCustomer = Mage::getSingleton('customer/session')-&gt;getCustomer();
          if($currentCustomer)
          {
              $collection = Mage::getModel('mdg_giftregistry/entity')-&gt;getCollection()
                  -&gt;addFieldToFilter('customer_id', $currentCustomer-&gt;getId());
          }
          return $collection;
      }
  }
  </code></pre>
  
  <p><strong>Template</strong></p>
  
  <pre><code>&lt;?php
  $_collection = $this-&gt;getCustomerRegistries();
  $helper = Mage::helper('mdg_giftregistry')
  ?&gt;
  &lt;div class="customer-list"&gt;
      &lt;?php if(!$_collection-&gt;count()): ?&gt;
          &lt;h2&gt;&lt;?php echo $this-&gt;__('You have no registries.') ?&gt;&lt;/h2&gt;
          &lt;a href="&lt;?php echo $this-&gt;getUrl('giftregistry/index/new') ?&gt;"&gt;
              &lt;?php echo $this-&gt;__('Click Here to create a new Gift Registry') ?&gt;
          &lt;/a&gt;
      &lt;?php else: ?&gt;
          &lt;ul&gt;
              &lt;?php foreach($_collection as $registry): ?&gt;
                  &lt;li&gt;
                      &lt;h3&gt;&lt;?php echo $registry-&gt;getEventName(); ?&gt;&lt;/h3&gt;
                      &lt;p&gt;&lt;strong&gt;&lt;?php echo $this-&gt;__('Event Name:') ?&gt; &lt;?php echo $registry-&gt;getEventName(); ?&gt;&lt;/strong&gt;&lt;/p&gt;
                      &lt;p&gt;&lt;strong&gt;&lt;?php echo $this-&gt;__('Event Location:') ?&gt; &lt;?php echo $registry-&gt;getEventLocation(); ?&gt;&lt;/strong&gt;&lt;/p&gt;
                      &lt;a href="&lt;?php echo $this-&gt;getUrl('giftregistry/view/view', array('_query' =&gt; array('registry_id' =&gt; $registry-&gt;getEntityId()))) ?&gt;"&gt;
                          &lt;?php echo $this-&gt;__('View Registry') ?&gt;
                      &lt;/a&gt;
                      &lt;?php if($helper-&gt;isRegistryOwner($registry-&gt;getCustomerId())): ?&gt;
                          &lt;a href="&lt;?php echo $this-&gt;getUrl('giftregistry/index/edit', array('_query' =&gt; array('registry_id' =&gt; $registry-&gt;getEntityId()))) ?&gt;"&gt;
                              &lt;?php echo $this-&gt;__('Edit Registry') ?&gt;
                          &lt;/a&gt;
                          &lt;a href="&lt;?php echo $this-&gt;getUrl('giftregistry/index/delete', array('_query' =&gt; array('registry_id' =&gt; $registry-&gt;getEntityId()))) ?&gt;"&gt;
                              &lt;?php echo $this-&gt;__('Delete Registry') ?&gt;
                          &lt;/a&gt;
                      &lt;?php endif; ?&gt;
                  &lt;/li&gt;
              &lt;?php endforeach; ?&gt;
          &lt;/ul&gt;
      &lt;?php endif; ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Layout XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;layout version="0.1.0"&gt;
      &lt;mdg_giftregistry_index_index&gt;
          &lt;reference name="root"&gt;
              &lt;action method="setTemplate"&gt;&lt;template&gt;page/1column.phtml&lt;/template&gt;&lt;/action&gt;
          &lt;/reference&gt;
          &lt;reference name="content"&gt;
              &lt;block name="giftregistry.list" type="mdg_giftregistry/list" template="mdg/list.phtml" as="giftregistry_list"/&gt;
          &lt;/reference&gt;
      &lt;/mdg_giftregistry_index_index&gt;
  
      &lt;mdg_giftregistry_index_new&gt;
          &lt;reference name="content"&gt;
              &lt;block name="giftregistry.new" type="core/template" template="mdg/new.phtml" as="giftregistry_new"/&gt;
          &lt;/reference&gt;
      &lt;/mdg_giftregistry_index_new&gt;
  
      &lt;mdg_giftregistry_index_edit&gt;
          &lt;reference name="content"&gt;
              &lt;block name="giftregistry.edit" type="core/template" template="mdg/edit.phtml" as="giftregistry_edit"/&gt;
          &lt;/reference&gt;
      &lt;/mdg_giftregistry_index_edit&gt;
  
      &lt;mdg_giftregistry_view_view&gt;
          &lt;reference name="content"&gt;
              &lt;block name="giftregistry.view" type="core/template" template="mdg/view.phtml" as="giftregistry_view"/&gt;
          &lt;/reference&gt;
      &lt;/mdg_giftregistry_view_view&gt;
  
      &lt;mdg_giftregistry_search_index&gt;
          &lt;reference name="content"&gt;
              &lt;block name="giftregistry.search" type="core/template" template="mdg/search.phtml" as="giftregistry_search"/&gt;
          &lt;/reference&gt;
      &lt;/mdg_giftregistry_search_index&gt;
  
      &lt;mdg_giftregistry_search_results&gt;
          &lt;reference name="content"&gt;
              &lt;block name="giftregistry.results" type="mdg_giftregistry/list" template="mdg/list.phtml"/&gt;
          &lt;/reference&gt;
      &lt;/mdg_giftregistry_search_results&gt;
  
      &lt;catalog_product_view translate="label"&gt;
          &lt;reference name="content"&gt;
              &lt;reference name="product.info"&gt;
                  &lt;block type="mdg_giftregistry/add" name="registry.add" as="registry_add" template="mdg/add_to_registry.phtml" /&gt;
              &lt;/reference&gt;
          &lt;/reference&gt;
      &lt;/catalog_product_view&gt;
  &lt;/layout&gt;
  </code></pre>