2
votes

I'm new to opencart. I want to create a custom theme and some custom controllers and models. I can't find any tutorials relative to this, but I tried to create a view along a controller. When I call that view from home or header view page, like $header (in home file) and $search (in header file), then it shows undefined variable.

My code looks like this. It's in controller (path is catalog\controller\common\test.php).

<?php
class ControllerCommonTest extends Controller{
    public function index() {
 if(file_exists(DIR_TEMPLATE.this->config->get('config_template').'/template/test/test.tpl')) {

        $this->response->setOutput($this->render());
    } else {
        return $this->load->view('default/template/common/header.tpl');
    }
    }
}
?>

And my view is in \view\theme\MyTheme\template\common\test.tpl

<?php
echo "Test file";
?>

And in my home file, I call my controller like below...

<?php
echo $header;
echo $test;
echo $footer;
?>

When I run this it shows the below error:

Notice: Undefined variable: test in C:\xampp\htdocs\opencart\catalog\view\theme\MyCustome\template\common\home.tpl on line 4

So, please provide any tutorial links and any examples for developing a custom module in opencart.

Thanks in advance.

1
Check how the $header variable is defined in the controller.. Similarly define your $test variable. Till opencart version 1.x the template variables were defined in controller like $this->data['variable_name'] and from 2.x onwards $data['variable_name'].Sankar V

1 Answers

2
votes

To display test module tpl i.e. test.tpl on home page, You have load test controller on home controller. Please add following code in catalog/controller/common/home.php

add this code

$data['test'] = $this->load->controller('common/test');

After

$data['header'] = $this->load->controller('common/header');