如何在OpenCart 2.3.0.2中设置全局变量? [英] How to setting global variables in OpenCart 2.3.0.2?
问题描述
当我想在模板(.tpl
文件)中设置全局变量时,我只在opencart 2.2或更早版本中使用global
.
When I want to set a global variable in template (.tpl
files), I just use global
in opencart 2.2 or older version.
例如:
<?php global $config; if($config->get('my_options')== 1) { ?>....<?php } ?>
但是,它在最新的OpenCart 2.3.0.2中不起作用
因为在控制器中预定义的global $config
相当不方便(我们在许多模板文件中使用了config值).
希望有更多经验的人可以解决这个问题.
Because predefined global $config
in controller rather inconvenient (we have use config value in many template file).
Hopefully someone with more experience will have an answer to this problem.
推荐答案
在config.php或admin/config.php中,根据所需的位置,将它们定义为常量的最少代码.这将使这些常量在整个目录或管理上下文中立即可用.
The least code you will get defining them as constants in config.php or admin/config.php, depending on where you will need them. This will make these constants immediately available throughout the catalog or admin context.
另一种方法是通过在控制器中使用$ this-> config将键/值从设置表中获取到模型或控制器中.
Another way is by using $this->config in controllers to get key/values out of the setting table into your models or controllers.
这篇关于如何在OpenCart 2.3.0.2中设置全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!