【问题标题】:pass variables from controller to view in opencart从控制器传递变量以在 opencart 中查看
【发布时间】:2014-03-05 13:01:33
【问题描述】:

如何将变量从控制器传递到 OpenCart 中的视图 (tpl) 文件?

我编写了一个自定义模块,所以我需要将返回的状态传递给视图。

下面是我加载 tpl 的控制器的一部分(它是一个巨大的功能,我只复制了所需的块)

$message = '';

if (isset($_POST['server_response'])) {
$message .= 'Server Says= ' . $_POST['server_response'] . "\n";
}

if (isset($_POST['output'])) {
$message .= 'Output= ' . $_POST['output'] . "\n";

    $this->data['msg'] = $message;

$this->data['continue'] = $this->url->link('checkout/success');

                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/success.tpl')) {
                    $this->template = $this->config->get('config_template') . '/template/payment/success.tpl';
                } else {
                    $this->template = 'default/template/payment/success.tpl';
                }

                $this->children = array(  
                    'common/column_left',
                    'common/column_right',
                    'common/content_top',
                    'common/content_bottom',
                    'common/footer',
                    'common/header'
                );

                $this->response->setOutput($this->render());
}

在我的 success.tpl 中,当我 echo $msg 时它说:

注意:未定义的变量:msg in C:\wamp\www\site\catalog\view\theme\hype\template\payment\success.tpl 第 16 行

谁能告诉我如何将 $msg 变量从控制器传递到 tpl?

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    您可以用于打开购物车 1.4x 和 1.5x

    $this->data['variableName'] = 'value';
    

    但对于最新的开放式购物车版本 2.0x,情况发生了变化。现在你可以使用

    $data['variableName'] = 'value';
    

    【讨论】:

      【解决方案2】:

      这应该可行。

      尝试将任何 if 语句之外的变量设置为默认值,例如。

      $this->data['msg'] = 'test';
      

      确保它不是任何其他逻辑,例如

      $_POST['output']
      

      这是错误的。

      目前,您只需在 if 语句之外设置 $message。

      当 if 语句的计算结果不为真时,$this->data['msg'] 将永远不会被设置。

      【讨论】:

        【解决方案3】:

        如何使用控制器变量和其他各种细节可以在this answer 中找到。你基本上使用

        $this->data['msg'] = 'your value';
        

        在控制器中,提取到模板文件中的$msg

        您还应该注意,使用 $_POST 而不是框架的正确方法 $this->request->post 是不受欢迎的,应该相应地进行更改

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-06
          • 2017-03-15
          • 1970-01-01
          • 1970-01-01
          • 2015-12-13
          • 1970-01-01
          • 1970-01-01
          • 2017-09-16
          相关资源
          最近更新 更多