There is another post on Stack Overflow that includes the following code for serving multiple product templates based on product ID
//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';
}
}
I would like to check for an alternate product field value that I won't be using instead of ID so it is something that can be managed from the admin panel.
For example, a statement that reads "If product location = accessory then get product/accessory.tpl"
Would I have to load that field in the product controller before I can request it with the if statement?
What would the syntax look like?
You should be able to use any of the fields in product data in the admin panel such as Location that you already referenced.
Everything from the product table for your requested row should be present in the $product_info array.
Try something like this:
$template = ($product_info['location'] == 'accessory') ? 'accessory.tpl' : 'product.tpl';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
If you anticipate there will be many different templates for different locations it would be more efficient to use a switch control.
switch ($product_info['location']):
case 'accessory':
$template = 'accessory.tpl';
break;
case 'tool':
$template = 'tool.tpl';
break;
default:
$template = 'product.tpl';
break;
endswitch;
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
Hope that helps.
Related
I am trying to understand the MVC method with the use of OOP. However it seems like I've hit the wall here.
I am trying to pass multiple objects to the view. But all I can do so far is pass just one object. The ideal result would be passing multiple objects, while keeping the names that are assigned to them in the controller.
The render, start and end functions in the View class go something like this:
public function render($viewName, $data){
$viewAry = explode('/', $viewName);
$viewString = implode(DS, $viewAry);
if(file_exists(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php')){
include(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php');
include(ROOT . DS . 'app' . DS . 'views' . DS . 'layouts' . DS . $this->_layout . '.php');
}else{
die('The view \"' . $viewName . '\" does not exist.');
}
}
public function content($type){
if($type == 'head'){
return $this->_head;
}elseif ($type == 'body'){
return $this->_body;
}
return false;
}
public function start($type){
$this->_outputBuffer = $type;
ob_start();
}
public function end(){
if($this->_outputBuffer == 'head'){
$this->_head = ob_get_clean();
}elseif($this->_outputBuffer == 'body'){
$this->_body = ob_get_clean();
}else{
die('You must first run the start method.');
}
}
And this is how would the controller look like:
public function indexAction(){
$items = $this->PortalModel->getItems();
$collections = $this->PortalModel->getCollections();
$this->view->render('home/index', $items);
}
So this is how I get the one $data object to the view and loop trough it.
But how could I store multiple results from the database to the view?
You should pass an array of variables into view instead of one variable.
public function indexAction(){
$variables = [
'items' => $this->PortalModel->getItems(),
'collections' => $this->PortalModel->getCollections()
];
$this->view->render('home/index', $variables);
}
I have a website which already works on 2 languages ,russian and english(everything runs well in both languages), now i have added armenian language.
The Problem --- when i switch on the website into armenain language , i see ,for example,in breadcrumbs
text_home button_continue button_login ....
i have checked \catalog\language\armen\armenian.php file and noticed that values of this varables exist.
By the way ,when i add from armenian.php into ,for example, language/armen/common/header .php this code
$_['text_home'] = 'arm_home';
it works , but thit means that i should add by hand in every single page this general variable...
i would like to have more optimal solution ...
from admin panel i set armenain as default language
Maybe ,i should edit system\library\language.php ???
Here is the structure
<?php
class Language {
private $default = 'en-gb';
private $directory;
private $data = array();
public function __construct($directory = '') {
$this->directory = $directory;
}
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : $key);
}
public function set($key, $value) {
$this->data[$key] = $value;
}
// Please dont use the below function i'm thinking getting rid of it.
public function all() {
return $this->data;
}
// Please dont use the below function i'm thinking getting rid of it.
public function merge(&$data) {
array_merge($this->data, $data);
}
public function load($filename, &$data = array()) {
$_ = array();
$file = DIR_LANGUAGE . 'english/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . 'english/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->default . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->default . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->directory . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$this->data = array_merge($this->data, $_);
return $this->data;
}
}
Thank you in advance
I used an old package of armenain language ,which wasn't compatible with oc 2.3,
solution https://crowdin.com/project/opencart-translation-v2/hy-AM#
I have a view, in which I want to, on the push of the button, send data to a specific function in a controller, which would then manipulate it a little and pass it forward to another view. However, whenever the button is pushed, the screen is blank. The URL changes to the correct URL, but in Chrome Dev Tools it gives the:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
error. I am not sure what is failing to load, or what is going on. Any help is greatly appreciated!
My view(the relevant part):
<script>
$('#begin_practice').on('click', function(){
location.href= "<?php echo base_url() . 'test/setupTest/' . $test_id . '/' . $lang . '/' . $practice; ?>";
});
</script>
My controller (the relevant function):
public function setupTest($test_id, $lang, $practice){
$this->load->model('Test_model');
$test_key= ($practice) ? $this->Test_model->getKey($test_id, $lang, $practice) : $this->Test_model->getKey($test_id, $lang, $practice);
$data['test_key'] = $test_key;
$data['test_id'] = $test_id;
$data['lang'] = $lang;
$data['practice'] = $practice;
if($practice){
$this->load->view('tests/test_sample/' . $lang . 'practice_test_view', $data);
}else{
$this->load->view($lang . '/test_view', $data);
}
}
My model:
public function getKey($test_id, $lang, $practice){
if($test_id == 7){
$image_array = ($practice) ? $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '_practice_' . $lang . '.json') : $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '_' . $lang . '.json');
}else{
$image_array = ($practice) ? $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '_practice.json') : $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '.json');
}
$image_array = json_decode($image_array, true);
if($test_id !=8){
shuffle($image_array);
}
return $image_array;
}
If anything else is needed, please let me know. Thanks in advance!
I am trying to render product_list.tpl file in home.tpl but it's giving me NULL
Controller File:
/controller/product/product_list.php
Code:
class ControllerProductProductList extends Controller {
public function index() {
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->load->model('tool/image');
$filter_data = array(
'filter_tag' => 'featured',
'limit' => 9
);
$data['results'] = $this->model_catalog_product->getProducts($filter_data);
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/common/productlist.tpl', $data));
}
}
}
Template to render
/template/product/productlist.tpl
Code:
<?php var_dump($results); ?>
<h2>Product are here</h2>
Then adding this line in home.php controller
$data['special_mod'] = $this->load->controller('product/product_list');
and printing $special_mod in common/home.tpl file
The problem was in /controller/product/product_list.php
the method $this->response->setOutput doesn't just return the value but send the user to the different page while what I wanted was to just output the productlist.tpl as string so for that I had to replace the code
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/common/productlist.tpl', $data));
}
with
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data);
} else {
return $this->load->view('default/template/common/productlist.tpl', $data);
}
I am using codeigniter. I need to get all variables from a language file to an array.Is it possible?
Is there any method available like as follows?
$a = $this->load->language('editor');
print_r($a);
I was tried $this->lang->language; But,This will return labels from another language files loaded.
$CI = & get_instance();
$arr = $CI->lang->language;
Or Use following library
Class My_language {
var $language = array();
/**
* List of loaded language files
*
* #var array
*/
var $is_loaded = array();
function __construct() {
log_message('debug', "Language Class Initialized");
}
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
$langfile = str_replace('.php', '', $langfile);
if ($add_suffix == TRUE) {
$langfile = str_replace('_lang.', '', $langfile) . '_lang';
}
$langfile .= '.php';
if (in_array($langfile, $this->is_loaded, TRUE)) {
return;
}
$config = & get_config();
if ($idiom == '') {
$deft_lang = (!isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path . 'language/' . $idiom . '/' . $langfile)) {
include($alt_path . 'language/' . $idiom . '/' . $langfile);
} else {
$found = FALSE;
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) {
if (file_exists($package_path . 'language/' . $idiom . '/' . $langfile)) {
include($package_path . 'language/' . $idiom . '/' . $langfile);
$found = TRUE;
break;
}
}
if ($found !== TRUE) {
show_error('Unable to load the requested language file: language/' . $idiom . '/' . $langfile);
}
}
if (!isset($lang)) {
log_message('error', 'Language file contains no data: language/' . $idiom . '/' . $langfile);
return;
}
if ($return == TRUE) {
return $lang;
}
$this->is_loaded[] = $langfile;
$this->language = array();
$this->language = $lang;
return $this->language;
unset($lang);
log_message('debug', 'Language file loaded: language/' . $idiom . '/' . $langfile);
return TRUE;
}
}
Call like this
$this->load->library('my_language');
$arr = $this->my_language->load('demo');
print_r($arr);
I know this is quite an old question, but I just want to give my solution for this problem since no answers has done the trick for this problem. (tested on codeigniter 3)
$this->load->helper('language');
$foo = $this->lang->load('lang_file', 'english', true);
print_r($foo);
notice that the third parameter for load method determines whether to return the loaded array of translations. source: codeigniter 3 docs.
hope this helps
Yeah ofcourse its possible. You can do like this :
//load helper for language
$this->load->helper('language');
//test is the language file in english folder
$this->lang->load('test','english');
//fetch all the data in $var variable
$var=$this->lang->language;
//print $var
print_r($var);
$var will return the array. :)
If you want to return language file data in Array than you need to pass the third parameter in load function.
$this->lang->load('header','hindi',true) // filename,language,true