Pass data from block to view

梦想的初衷 提交于 2019-12-04 14:17:11

问题


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 pass data from controller to block?


回答1:


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 :)



来源:https://stackoverflow.com/questions/16270655/pass-data-from-block-to-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!