open cart attaching the controller to the template - templates

I have created header_top.tpl in the catalouge folder (down in the theme "common" folder) and then a controller in the controller folder. But I do not know how should I render a template with a controller (and send data from controller to the template to be further parsed by the template).
I know $this->render() does render the template but what happens before to it is vague to me.

There should be a condition similar to the below the code in your controller file. Replace with your template file in that section as given below:
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header_top.tpl')) {
$this->template = $this->config->get('config_template') . '/template/common/header_top.tpl';
} else {
$this->template = 'default/template/common/header_top.tpl';
}
Values set as $this->data['sitename'] = 'stackoverflow'; in controller file can be used in template file like echo $sitename;
Have a nice day !!

Related

OpenCart 2.3 - Controller default and custom template

I am following the book OpenCart Theme and Module development
In chapter 3 it states:
For example, you can refer to catalog/controller/account/account.php. You
will find the following code in 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';
}
However, it is not possible to find this code excerpt while checking catalog/controller/account/account.php.
Where is this check in OpenCart 2.3?
Loading view in OpeCart 2.3:
$this->response->setOutput($this->load->view('account/account', $data));
You may want to ckeck these files:
system\library\response.php
system\engine\loader.php
system\library\template.php

Error displaying page in OpenCart

Hello I am new in OpenCart and I am studying it now for my next project. And My first step in learning this framework? is to display my own page using a link from my navigation menu in admin area. But I am getting this error.
Notice: Error: Could not load template C:\wamp\www\myshop/admin/view/template/locations/countries_list.tpl! in C:\wamp\www\myshop\system\engine\controller.php on line 90
And I can't spot my error. Here's what I did.
Here's my controller /controller/locations/countries.php
<?php
class ControllerLocationsCountries extends Controller {
private $error = array();
public function index() {
$this->language->load('locations/countries');
$this->document->setTitle($this->language->get('heading_title'));
$this->getList();
}
protected function getList() {
$this->data['heading_title'] = $this->language->get('heading_title');
$this->template = 'locations/countries_list.tpl';
$this->response->setOutput($this->render());
}
}
Here's my simple view /view/template/locations/countries_list.php
<h1>Hello</h1>
Then in my header I include this code for displaying a link in menu navigation /controller/common/header.php
$this->data['text_locations'] = $this->language->get('text_locations');
....
$this->data['locations'] = $this->url->link('locations/countries', 'token=' . $this->session->data['token'], 'SSL');
That's all. I don't know what part is wrong. Can you help me with this?
The error is pretty self explanatory. The template file doesn't exist. Make sure the template filename and path is exactly the same and that you're using .tpl as the extension and not .php. It's also possible there could be a permissions error, but on WAMP that's not likely

Opencart add additional tpl layout for a category

I am trying to setup additional page and category layouts in Opencart 1.5.4.
I have got to a stage where if I enter the route of the new category into the address bar, the new template shows as I want, but I don't seem to be able to register that route change in OC.
If I specify the change in the .htaccess file the new template loads as expected, but I don't this is the right answer to the problem (although it works).
Addition to .htaccess (not correct method I'm sure)
RewriteRule ^skis$ index.php?route=product/categories&path=1 [L,QSA]
I have created two new files
/catalog/view/theme/default/template/product/categories.tpl
/catalog/controller/product/categories.php
In /catalog/controller/product/categories.php, I have changed the content to reflect new tpl file;
class Controllerproductcategories extends Controller {
.
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/categories.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/categories.tpl';
} else {
$this->template = 'default/template/product/categories.tpl';
}
So in summary
If I specify the rewrite in the .htaccess file the layout loads, if I don't it doesn't.
I have added a new layout in OC and selected it against the category
Anyone got any ideas I might try to get this working correctly? I have a load of templates to create for products, categories and information pages so would like to do this correctly.
Tx in advance
Stu
I opted for this approach in the end.. might help someone else.
if ($this->data['heading_title'] == "Skis") {
$this->template = $this->config->get('config_template') . '/template/product/categories.tpl';
} elseif ($this->data['heading_title'] == "Softgoods") {
$this->template = $this->config->get('config_template') . '/template/product/category.tpl';
} elseif ($this->data['heading_title'] == "Outlet Store") {
$this->template = $this->config->get('config_template') . '/template/product/category.tpl';
} else {
$this->template = $this->config->get('config_template') . '/template/product/category.tpl';
}
From what I can see, you're trying to hard code each individual template and it's not really going to work in the way you've chosen using layouts for the category. Layout's are designed to arrange content on a page rather than specify templates
I've actually created a commercial version of what you are trying to achieve which can be found here. It allows you to create templates and assign it to one or more pages. It works for products, categories, manufacturers and information pages. It also allows you to specify the template for products under a whole category or manufacturer too

How to duplicate product template in opencart

I am very new to opencart but I think this is easy and best solution out there. Although playing with templates is not a joy...
I am struggling to create some additional template pages. For example I have two types of products and category pages. I want to different templates for different products. In opencart you have only one layout for products.
What I thought to do is to make a duplicate of product layout. I got some help online but I am still not sure what more is needed. This is what I've done so far...
1 - Copy the controller file of catalog/controller/product/product.php and changed to catalog/controller/product/product-2.php. Then I changed this in controller:
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product-2.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/product-2.tpl';
} else {
$this->template = 'default/template/product/product-2.tpl';
}
2 - Then I copied the product language file and save as product-2
3 - After that I copied the actual tpl file and save as product-2
It was looking fine but if I try to make some changes to product-2.tpl nothing changes. Do I have to copy some more files to complete it?
After search and doing lot of research and mind-boggling, i found a very useful method to do what i want to do. in this way i have fully control of the opencart theme system. i can make as many different layouts as i wanted. i dont have to use VQmod neither have to make any controller but you have to use the existing controllers like product, category etc. if you are making your own controller even then it works.
here are the steps to follow to achieve different template for different categories, products and common pages.
i am here doing the example of products.
1- create custom template of product in product folder of the theme. e.g customproduct.tpl
2- now customize it as you wanted.create a product and take the id of it. id is very important here.
3- go to the controller catalog/controller/product/product.php
4- find this code
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/customproduct.tpl';
} else {
$this->template = 'default/template/product/product.tpl';
}
5- now you have to use the simple if else condition. for example
//42 is the id of the product
if($this->request->get['product_id'] == 42){
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/customproduct.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/customproduct.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
}
else{
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/customproduct.tpl';
}
}
that's it job Done ;)
you can do the same thing for other controllers.
As you can see here ;
http://pranshuarya.com/jaal/Development/opencart-creating-a-new-viewtemplate.html
This was the second option of the real solution for "completely new layout" request.
Both can be used. Your solution is time saver if you will not need a new controller routine.But that way is more flexible.
Here are the steps ;
Add a new controller file to /catalog/controller. call something like new_layout.php... copy if there is a similar controller already. Be sure to change controller name as i told in my comment below.
Add a new view file in your theme folder ... like controller if you wish you can copy a simiplar view files content and modify as you wish.
Add the new layout either from admin panel System/Design/Layouts or from MYSQL table directly as explained in the link above.
It's ok now. Just add some modules in this layout view and enjoy.

Load header and footer view in CI

Is there any way to load view 'header'/'footer' without calling $this->load->view('header') or $this->load->view('footer') in every controller? Maybe a template that can be use in every view?
Here are a couple simple approaches to get you started:
Create a template class:
class Template {
function load($view)
{
$CI = &get_instance();
$CI->load->view('header');
$CI->load->view($view);
$CI->load->view('footer');
}
}
Usage in controller:
$this->template->load('my_view');
Use a master view file:
<!-- views/master.php -->
<html>
<header>Your header</header>
<?php $this->load->view($view, $data); ?>
<footer>Your footer</footer>
</html>
In the controller:
$this->load->view('master', array(
'view' => 'my-view-file',
'data' => $some_data
));
I prefer the Template class approach, as it's easy to add methods to append templates areas, load javascript files, and whatever else you need. I also prefer to automatically select the view file based on the method being called. Something like this:
if ( ! isset($view_file)) {
$view_file = $CI->router->fetch_class().'/'.$CI->router->fetch_method();
}
This would load views/users/index.php if the controller is Users and the method is index.
You need to load view files somehow, this the way CI use to include the files.
Stick to the standard, I think it's the best practice.
Make a function that loads header and footer and places data in between.
Anyway the model on which CI is built requires the explicit loading of views (afaik).
I usually extend CI's Loader class to accomplish this...
<?php
class MY_Loader extends CI_Loader {
public function view($view, $vars = array(), $return = FALSE, $include_header = TRUE, $include_footer = TRUE)
{
$content = '';
if ($include_header)
{
$content .= parent::view('header', $vars, $return);
}
$content .= parent::view($view, $vars, $return);
if ($include_footer)
{
$content .= parent::view('footer', $vars, $return);
}
return $content;
}
}