从模板访问类属性和方法 [英] Accessing class properties and methods from template

查看:76
本文介绍了从模板访问类属性和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆