0

I am trying to access block class method from controller and set some value to block class property. The method is being accessed but when I try to print the property in .phtml file it is NULL. Here is my code:

app/code/local/Amactech/Events/controllers/IndexController.php

<?php

    class Amactech_Events_IndexController extends Mage_Core_Controller_Front_Action{
        public function indexAction(){

            $block=$this->getLayout()->createBlock('events/events');
            $block->setFeedback("wrwqerqwer fasdfasdf vxzcvzxcv");

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

    }

?>

app/code/local/Amactech/Events/Block/Event.php

<?php

  class Amactech_Events_Block_Events extends Mage_Core_Block_Template{

    public $_feedback;

    public function setFeedback($feedback){
      echo $feedback;
      $this->_feedback = $feedback;
    }

    public function getFeedback(){
      echo "get feedback"; /*This line is being printing which means I am getting in this method*/
      return $this->_feedback;
    }


  }

?>

app/design/frontend/base/default/template/events/events.phtml

<?php
    echo $this->getFeedback();
?>

So I am accessing block setFeedback() method and setting $_feedback value. I print something string inside setFeedback() and getFeedback() methods to check whether I am getting into these methods. The printing string is shown which means I am getting into the methods but when I assign some value to property and try to print the property is shows NULL.

1 Answer 1

2

You can set data in your block using $this->getLayout()->getBlock('blockname')->setData('data'); methos

Here is example for set data from controller get in template

Controller

public function indexAction()
{
    $this->loadLayout();
    $this->getLayout()->getBlock('blockname')->setMyname('vaibhav');     
    $this->renderLayout();
}

xml

    <reference name="content">
        <block type="core/template" name="blockname" template="template.phtml" />
    </reference>

template

<?php
    echo $this->getMyname();
?>
1
  • If this solution works for you can accept or upvote this answer to help someone Commented Dec 28, 2016 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.