Can't access variable data from controller to view(template tpl file) I have very basic function
public function index() {
$message = "hello";
return $this->load->view('common/hello.tpl', $message );
}
In view template i try to get $message variable but not defined
<?php echo $message; ?>
public function index() {
$message = "hello";
return $this->load->view('common/hello.tpl', $message );
}
In view template hello.tpl i try to get $message variable but not defined
This is only example. I have model which must load but for now I need only to access from Controller to View.... Help..
In your given code, the template has no chance to know anything about a variable called $message
as you only transmit the value of that variable. According to another SO thread, you should add that variable an array like $data
like this:
$data['message'] = $message;
This makes the content of $message
available under the same name in your template. If you change the key (eg. to $data['otherKey']
), it will get available under $otherKey
Afterwards, transmit that value array to the view
method:
return $this->load->view('common/hello.tpl', $data );
First You need post what version of OC are using... So if you use OC version 1.x in controller file you should define your data like this: $this->data['message'] = 'hello';
and render tpl:
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/hello.tpl')) {
$this->template = $this->config->get('config_template') . '/template/payment/hello.tpl';
} else {
$this->template = 'default/template/payment/hello.tpl';
}
$this->render();
if you use version 2 or higher you should define: $data['message'] = 'hello';
and render in tpl lik:
return $this->load->view('extension/payment/hello', $data);
In tpl retrieve data: <?php echo $message: ?>
if OC version 3.x.x where is used twig templates... retrieve data {{ message }}
.
This is very simple if you just take a look how it works in OC corresponding version.
So for OC2.3 as you have defined, should be: controller hello.php file:
<?php
class ControllerCommonHello extends Controller {
public function index() {
$data['hello'] = 'Hello!!!';
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$this->response->setOutput($this->load->view('common/hello', $data));
}
}
hello.tpl
file something like this:
?php echo $header; ?>
<div class="container">
<?php echo $hello; ?>
</div>
<?php echo $footer; ?>