Reputation: 3253
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'] . "\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());
}
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?
Upvotes: 2
Views: 7743
Reputation: 653
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';
Upvotes: 2
Reputation: 15151
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
Upvotes: 0
Reputation: 1670
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.
Upvotes: 1