OpenCart:如何创建全局变量? [英] OpenCart: How to make a global variable?
问题描述
我正在尝试在OpenCart中创建两个全局变量.我基本上希望能够在我的任何.tpl
文件中声明它们
I am trying to make two global variables within OpenCart. I basically want to be able to declare them in any of my .tpl
files
<?php echo $global1; ?>
我尝试通过在文件内添加$global1="test"
来编辑library/system.php
以及config.php
.但是在.tpl
文件中调用它不起作用吗?
I have tried editing, library/system.php
and also config.php
by adding $global1="test"
inside my files. However calling that in .tpl
files is not working?
例如,看下面的文件,我希望能够随时调用这些变量.我必须编辑config.php还是什么?该示例显示了在每个.tpl文件中使用的$header
调用.
Example, look at the file below, I want to be able to call these variables anytime.. do I have to edit config.php or what?? The example shows the $header
call which is used on every .tpl file.
not_found.tpl
<?=$header?>
<div class="breadcrumb">
<? foreach ($breadcrumbs as $breadcrumb) { ?>
<? $breadcrumb['separator']; ?><a href="<?=$breadcrumb['href']?>"><?=$breadcrumb['text']?></a>
<? } ?>
</div>
<div id="content">
<?=$global1?>
<img src="/catalog/view/theme/default/image/error.png"/>
</div>
<?=$footer?>
已更新
/catalog/controller/common/header.php
<?php
class ControllerCommonHeader extends Controller {
protected function index() {
// NEW GLOBAL VARS
$cdnDefault="//www.gorgeouscouturedev.com/catalog/view/theme/";
$currentUseLang = $this->language->get('code');
现在位于/catalog/view/theme/default/template/common/home.tpl
<?=$header?>
<?=$column_left?>
<?=$column_right?>
<div id="content">
<? echo $cdnDefault ?>
<? echo $currentUseLang ?>
<?=$content_top?>
<div class="flexslider">
<ul class="slides">
<li><img src="catalog/view/theme/default/image/desktop.png"/></li>
<li><img src="catalog/view/theme/default/image/blogger.png"/></li>
</ul>
</div>
<?=$content_bottom?>
</div>
<?=$footer?>
错误:
Notice: Undefined variable: cdnDefault in /catalog/view/theme/default/template/common/home.tpl on line 6
Notice: Undefined variable: currentUseLang in /catalog/view/theme/default/template/common/home.tpl on line 7
推荐答案
您可以使用$GLOBALS
超级全局数组
you can use $GLOBALS
super global array
例如,首先在controller/common/header.php
$GLOBALS["1"] = "test";
然后在任何tpl文件中使用它
then use it in any tpl file like
<?php echo $GLOBALS["1"]; ?>
关于header
的事情,实际上在这样的每个控制器文件(对应于每个tpl文件)中声明了header
和five other files
regarding that header
thing, that header
and five other files
are actually declared in every controller file (corresponding to every tpl file ) like this
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
回答更新的问题
/catalog/controller/common/header.php
<?php
class ControllerCommonHeader extends Controller {
protected function index() {
// NEW GLOBAL VARS
$GLOBALS["cdnDefault"]="//www.gorgeouscouturedev.com/catalog/view/theme/";
$GLOBALS["currentUseLang"] = $this->language->get('code');
现在位于/catalog/view/theme/default/template/common/home.tpl
<?=$header?>
<?=$column_left?>
<?=$column_right?>
<div id="content">
<? echo $GLOBALS["cdnDefault"]; ?>
<? echo $GLOBALS["currentUseLang"]; ?>
<?=$content_top?>
<div class="flexslider">
<ul class="slides">
<li><img src="catalog/view/theme/default/image/desktop.png"/></li>
<li><img src="catalog/view/theme/default/image/blogger.png"/></li>
</ul>
</div>
<?=$content_bottom?>
</div>
<?=$footer?>
这篇关于OpenCart:如何创建全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!