Pass data from block to view

后端 未结 1 737
不知归路
不知归路 2021-02-11 04:04

I found that you can set data to the view from block by setting it to your xml file. Is there\'s a way to set it without accessing your xml file? And another question, how can I

相关标签:
1条回答
  • 2021-02-11 04:37

    For passing data from block to view

    In block use

    $this->setVariableName($value) or $this->assign(‘variableName’, $value)
    

    In template use

    $this->getVariableName() or $variableName respectively
    

    For passing data from controller to block

    It's not the responsibility of the controller to set variables for the view.

    Controller set values from Models and block retrieves values from that model.

    You can do this by:

    • Creating a model class that inherits from Varien_object
    • In the controller, instantiate that object using this code:

      $object = Mage::getSingleton('model')
      $object->setVar($value) or  $object->setData('var', $value)
      
    • Later access the variable by

      $var = $object->getVar()
      

    Hope you got what was needed :)

    0 讨论(0)
提交回复
热议问题