Accessing class properties and methods from template

拈花ヽ惹草 提交于 2019-12-23 00:51:17

问题


Prior to Opencart 2.0 class properties could be accessed directly from the template. For example, $this->config->get('config_language') or $this->request->get['route'] were both accessible from an admin template file.

With the new method $this->load->view(), neither of these work. Is there a simple way to pass on class methods and properties which are available in the current controller to the tpl without explicitly adding them to the $data array?


回答1:


In Opencart version 2, if you want these variables then you can easily access them. There is slight change is code, now you can use

$this->registry 

which holds everything. So you have to get these things form

$this->registry

like this

$this->registry->get('config')

it will work like

$this->config  

so your

$this->config->get('config_language')

becomes

$this->registry->get('config')->get('config_language')

like this

$this->request->get['route'] == $this->registry->get('request')->get['route'];

$this->request->post['route'] == $this->registry->get('request')->post['route'];

$this->request->files['file'] == $this->registry->get('request')->files['file'];

for more just simply print $this->registry in any template.



来源:https://stackoverflow.com/questions/31929036/accessing-class-properties-and-methods-from-template

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