Coding standards

  • Removed the PHP ending tag ?> from php files

Load the Language File

In 1.5.x, you would use the following snippet to load the corresponding module language file.


On the other hand, you need to use a slightly different version in the case of OpenCart 2.x.x, as shown in the following snippet.



#### 1. Assigning template variables

most of the time you’ll end up assigning your variables so that you can access them in the view template files

OpenCart 1.5:

$this->data['foo'] = $foo;

$this->data['heading_title'] = $this->language->get('heading_title');

OpenCart 2.0:

$data['foo'] = $foo;

$data['heading_title'] = $this->language->get('heading_title');

2. Assigning a template

OpenCart 1.5:

$this->template = 'module/module.tpl';

$this->response->setOutput($this->render()); // OC 1.5.6.x

OpenCart 2.0:

$this->response->setOutput($this->load->view('module/module', $data));

$this->response->setOutput($this->load->view('default/template/account/account.tpl', $data)); // OC

$this->response->setOutput($this->load->view('extension/total/total', $data)); // OC (no .tpl at the end)

In the earlier version of OpenCart, you would have used the following code to assign the template file and render it.

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

In the recent version, you’ll need to use a slightly different and shorter way to handle it.

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/account.tpl')) {
  $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/account.tpl', $data));
} else {
  $this->response->setOutput($this->load->view('default/template/account/account.tpl', $data));

As you can see, in the recent version we’re using the view method of the Loader class.

3. Children Template Assignment

In 1.5.x, you needed to provide an array to assign all the children templates like header, footer, column_left, etc.

$this->children = array(

In the case of the 2.x.x version, you’ll use the controller method of the Loader class. (You can load controllers now)

$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');

4. URL Redirection

OpenCart 1.5:
In the older version, the redirect method was part of the abstract Controller class, so you could call that directly using the $this object.

$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));

OpenCart 2.0:
In the recent version, the redirect method belongs to the Response class and is thus accessed via that object.

$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));

In the recent version, you don’t need to provide a “separator” as an array key as opposed to the earlier version.

Here’s the snippet from the 1.5.x version:

$this->data['breadcrumbs'][] = array(
  'text'      => $this->language->get('text_home'),
  'href'      => $this->url->link('common/home'),
  'separator' => false

A similar result can be achieved by using the following code:

$data['breadcrumbs'][] = array(
  'text' => $this->language->get('text_home'),
  'href' => $this->url->link('common/home')

6. Status field is mandatory for the module now

if (isset($this->request->post['moduleName_status'])) {
	$data['moduleName_status'] = $this->request->post['moduleName_status'];
} else {
	$data['moduleName_status'] = $this->config->get('moduleName_status')