doutou7286
2014-03-05 13:01
浏览 65
已采纳

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

how to i pass variables from a controller to a view (tpl) file in OpenCart?

i have coded a custom module so i need to pass the the returned status to the view.

below is a part of my controller where i load the tpl (its a huge function, i have copied only the required block)

$message = '';

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

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

    $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());
}

in my success.tpl when i echo $msg it says:

Notice: Undefined variable: msg in C:\wamp\www\site\catalog\view\theme\hype\template\payment\success.tpl on line 16

can someone tell how can i pass the $msg variable from the controller to the tpl?

图片转代码服务由CSDN问答提供 功能建议

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

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

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

  $ message =''; 
 
if(isset($ _ POST ['server_response'])){
  $ message。='Server Says ='。  $ _POST ['server_response']。  “
”; 
} 
 
if(isset($ _ POST ['output'])){
 $ message。='Output ='。  $ _POST ['输出']。  “
”; 
 
 $ this-> data ['msg'] = $ message; 
 
 $ this-> data ['continue'] = $ this-> url-> link  ('checkout / success'); 
 
 if(file_exists(DIR_TEMPLATE。$ this-> config-> get('config_template')。'/ templat / pay / support.tpl')){
 $  this-> template = $ this-> config-> get('config_template')。  '/template/payment/success.tpl'; 
}}其他{
 $ this-> template ='default / template / payment / success.tpl'; 
} 
 
 $ this->儿童 = array(
'common / column_left',
'common / column_right',
'common / content_top',
'common / content_bottom',
'common / footer',
'common / header'  
); 
 
 $ this-> response-> setOutput($ this-> render()); 
} 
   
 
 

in my success.tpl当我 echo $ msg 时说:

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongsuo9982 2014-03-05 13:08
    已采纳

    This should work.

    Try setting the variable outside of any of your if statements to a default value eg.

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

    To make sure that it's not any of the other logic such as the

    $_POST['output']
    

    that is faulty.

    At the moment you just set $message outside the if statement.

    When the if statement doesn't evaluate to true $this->data['msg'] will never get set.

    点赞 打赏 评论
  • dongnanbi4942 2014-03-05 13:34

    How to work with controller variables and various other details can be found in this answer here. You basically use

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

    in the controller, which gets extracted to $msg in the template file

    You should also note that using $_POST instead of the framework's proper method of $this->request->post is frowned upon and should be changed accordingly

    点赞 打赏 评论
  • dongxieyi9115 2015-04-14 11:35

    You can use for open cart 1.4x and 1.5x

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

    but for latest open cart version 2.0x things are changed. Now you can use

    $data['variableName'] = 'value';
    
    点赞 打赏 评论

相关推荐 更多相似问题