从模板访问类属性和方法 [英] Accessing class properties and methods from template
问题描述
Opencart 2.0类属性之前的属性可以直接从模板访问.例如,都可以从管理模板文件访问$this->config->get('config_language')
或$this->request->get['route']
.
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.
使用新方法$this->load->view()
,这两种方法均无效.是否有一种简单的方法可以将当前控制器中可用的类方法和属性传递给tpl,而无需将它们显式添加到$data
数组中?
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?
推荐答案
在Opencart版本2中,如果需要这些变量,则可以轻松访问它们.代码稍有变化,现在您可以使用
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
像这样
$this->registry->get('config')
它将像
$this->config
所以您的
$this->config->get('config_language')
成为
$this->registry->get('config')->get('config_language')
像这样
$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'];
要获取更多信息,只需在任何模板中打印$this->registry
.
for more just simply print $this->registry
in any template.
这篇关于从模板访问类属性和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!