Opencart: Load multiple view files within a view template file

Posted: October 6, 2014 by Sankar Vijayakumar in OpenCart, Php
Tags: , , , , ,
Sometimes it’ll be better to split the template file into separate files for various things that need to be shown in that page. The most standard way is to use opencart ‘module’ concept and assign it to necessary pages via the admin panel. However, there can be situation where we cannot assign and use opencart modules. In that case, add the code in a separate function in controller and create a new template file for it.

Suppose we want to show similar products in product page without using opencart modules. In that case, create a new file named ‘similar_products.tpl’ and use it in the following way:

  • Add the following code in catalog/controller/product/product.php above the main template is loaded like:

    /*Code to render similar products template begins*/
    if (file_exists(DIR_TEMPLATE . $this->config->get(‘config_template’) . ‘/template/product/similar_products.tpl’)) {
    $this->template = $this->config->get(‘config_template’) . ‘/template/product/similar_products.tpl’;
    } else {
    $this->template = ‘default/template/product/similar_products.tpl’;
    }
    $this->data[‘similar_products’] = $this->render();
    /*Code to render similar products template ends*/

    /*Code that loads the main template.*/
    if (file_exists(DIR_TEMPLATE . $this->config->get(‘config_template’) . ‘/template/product/product.tpl’)) {
    $this->template = $this->config->get(‘config_template’) . ‘/template/product/product.tpl’;
    } else {
    $this->template = ‘default/template/product/product.tpl’;
    }

  • In product/product.tpl add the below code where we need to show similar products:
    echo $similar_products;

To do the same without modifying the opencart core files, use vqmod : Vqmod tutorial.
Happy Coding 😀 !!

Advertisements
Comments
  1. VARAPRASAD says:

    how to upload images in database when i click cart button and how to see user details

    Like

  2. Shaheer says:

    Good writeups Sankar! 🙂
    Just a little something I wanted to add…. I don’t know which version the above guidelines are for, but I’m using opencart 2.2.6.2. You can simply call other views using:
    $this->data[‘similar_products’] = $this->load->view(‘product/similar_products’);

    Liked by 2 people

How's it? Your comments and suggestions...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s