dongshan2004 2013-03-06 16:42
浏览 92
已采纳

OpenCart:如何制作全局变量?

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; ?>

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?

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?>

Updated

/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'); 

And now in /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?>

And the errors:

 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 
  • 写回答

2条回答 默认 最新

  • doucong7963 2013-03-06 17:18
    关注

    you can use $GLOBALS super global array

    for example declare it first in controller/common/header.php

    $GLOBALS["1"] = "test";
    

    then use it in any tpl file like

    <?php echo $GLOBALS["1"]; ?>
    

    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'     
            );
    

    Answer to updated question

    /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');
    

    And now in /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?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择