将变量从控制器传递到opencart中的视图

时间:2014-03-05 13:01:33

标签: php opencart

如何将变量从控制器传递到OpenCart中的view(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 \网站\目录\视图\主题\炒作\模板\金\ success.tpl   第16行

有人可以告诉我如何将$ msg变量从控制器传递给tpl?

3 个答案:

答案 0 :(得分:2)

您可以使用开放式购物车1.4x和1.5x

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

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

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

答案 1 :(得分:1)

这应该有用。

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

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

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

$_POST['output']

这是错误的。

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

当if语句的评估结果为true时,$ this-> data ['msg']永远不会被设置。

答案 2 :(得分:0)

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

$this->data['msg'] = 'your value';
控制器中的

,它被解压缩到模板文件中的$msg

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

相关问题