OpenCart 2.0 – Template System Explained

OpenCart is considered to be an MVCL architecture which stands for Model-View-Control-Language. This article will go over the basics of how the VIEW MVCL, which are the template files with the extension (.tpl) are used within OpenCart 2.0

How .tpl's work

We're basically dealing with a templating system / parser. Opencart uses its own Model-View-Control design. The templates are part of the View portion, and you'd find them as the /catalog/view/…/*.tpl files on an installed Opencart system. The *.tpl files are all part of a composite view that uses simple PHP-variables as placeholders for the dynamic bits of the HTML templates. These variables are set in the corresponding controller PHP files, found in the catalog/controller/…/*.php files. In short, the template language is just PHP. Don't be fooled into thinking that this is SMARTY template language because it is not even though smarty uses .tpl as well.

Here is an example of the actual loader for the templates found in Opencart 2.0. Please open up the following file: opencart-\upload\system\engine\loader.php and locate this function:

public function view($template, $data = array()) {
		$file = DIR_TEMPLATE . $template;

		if (file_exists($file)) {



			$output = ob_get_contents();


			return $output;
		} else {
			trigger_error('Error: Could not load template ' . $file . '!');

This is very simple and straight forward. It first checks to see if the template file (.tpl) even exists and if so, run the extract() function. After extracting the contents then it will return the template information. FYI, the php extract function imports variables into the current symbol table from an array.

Here is an example of how the actual view() function is called:

Go to opencart-\upload\admin\controller\catalog\attribute.php and find the following line of code:

$this->response->setOutput($this->load->view('catalog/attribute_list.tpl', $data));

FYI, these calls to view() are all over the place. This is just one example. setOutput() needs explaining since this is the function that holds the view() function. The setOutput function can be found here: opencart-\upload\system\library\response.php. Open up that file and find the following function:

public function setOutput($output) {
   $this->output = $output;

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *