I write ControllerBase that extends Controller. The ControllerBase able to woking with Smarty Teamplate and OpenCart Template.
Smarty Template enable developer to create template that clearly and easily.
This is my example web site that a partial of Store Front using Smarty Template.
http://treebhoph.6te.net/
I write English. Maybe you meet missing word or missing grammy because I'm asian from Thailand.
I hope you can understand my word.

ControllerBase object ables to using both Smarty and OpenCart Template Framework.
So it dose not have bad effect with old Controller Object that using OpenCart Template.
Before you using ControllerBase. You have to....
1 Download Smarty from http://www.smarty.net, Now I use Smarty 2.6.22.
2 Extract it, Copy smarty to folder "system/library/", so a file structure must be..
Code: Select all
system/library/smarty/
system/library/smarty/libs/
Code: Select all
system/engine/controllerBase.php
Code: Select all
require_once(DIR_SYSTEM . 'engine/controllerBase.php');
include(DIR_SYSTEM . 'library/smarty/libs/Smarty.class.php');
Smarty used this folder to read/create Template Cache File.
This is a Source Code of ControllerBase.
Code: Select all
<?php
//ControllerBase V 0.1.1b FOR OpenCart 1.2.x
// Version Number Meaning A.B.C , A = Major Version, B = Feature Change, C = Building Number / Closed Bug
//This supports both Smarty Template and OpenCart Template Framework
// Create By Treebhoph Thoomsan, treebhoph@gmail.com
abstract class ControllerBase extends Controller {
//The Constant Variables allow programmer to determine that what a Template Engine Framework want to use, a Default value is Smarty template.
const DEFAULT_TPL = 'default';
const SMARTY_TPL = 'smarty';
private $smarty;
private $moduleOutputs = array();
//Set a smarty Template as a Default Template Engine.
protected $templateEngine = ControllerBase::SMARTY_TPL;
//Set a Default Position for module as Left, right **** Now OpenCart 1.2.6 Support only Left and Right.
protected $modulePositions = 'left,right';
protected function render() {
if( $this->smarty == null )
$this->smarty = new Smarty();
foreach ($this->children as $child) {
$file = DIR_APPLICATION . 'controller/' . $child . '.php';
$class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $child);
if (file_exists($file)) {
require_once($file);
$controller = new $class();
$controller->index();
if( $this->templateEngine == ControllerBase::DEFAULT_TPL ) {
//When this Controller working with OpenCart Template Framework.
$this->data[$controller->id] = $controller->output;
}
else if( $this->templateEngine == ControllerBase::SMARTY_TPL ) {
//When this Controller working with Smarty Template.
$mod = substr($child, 0, 7 );
//Dose a childern Controler is a Module ?.
if( strtolower($mod) == "module/" ) {
//Find a Controller ID from Module Code, ( OpenCart 1.2.X using Modue Code as a Controller ID too. ).
$pos = (int)strrpos($child, "/");
if( $pos > 0 ) {
$moduleCode = substr($child, $pos + 1);
//Assign a Template Output from the Module to a $moduleOutputs array .
$this->moduleOutputs[$moduleCode] = $controller->output;
}
}
else {
// When it is only Childen that dose not a module.
$this->data[$controller->id] = $controller->output;
}
}
} else {
exit('Error: Could not load controller ' . $child . '!');
}
}
$this->output = $this->fetch($this->template);
if ($this->layout) {
$file = DIR_APPLICATION . 'controller/' . $this->layout . '.php';
$class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->layout);
if (file_exists($file)) {
require_once($file);
$controller = new $class();
$controller->data[$this->id] = $this->output;
$controller->index();
$this->output = $controller->output;
} else {
exit('Error: Could not load controller ' . $this->layout . '!');
}
}
$this->response->setOutput($this->output);
}
protected function fetch($filename) {
$file = DIR_TEMPLATE . $filename;
if (file_exists($file)) {
ob_start();
if( strtolower($this->templateEngine) == ControllerBase::DEFAULT_TPL ) {
//When this Controller using OpenCart Template Framework.
extract($this->data);
require($file);
//Get Template Output From OpenCart Template Framework.
$content = ob_get_contents();
}
else if( strtolower($this->templateEngine) == ControllerBase::SMARTY_TPL ) {
//When this Controller using Smarty Template Framework.
$this->smarty->compile_check = true;
$this->smarty->debugging = false;
//When smarty dose not Cached.
if(!$this->smarty->is_cached($file)) {
//Assign Data From Controller to Template View.
foreach ($this->data as $key => $value)
$this->smarty->assign($key,$value);
//Assign default value for module postion.
//Thsi ControllerBase version only Support Left and Right Postion like OpenCart 1.2.6.
if( $this->modulePositions != '' ) {
$modulePos = split( $this->modulePositions, ',' );
foreach( $modulePos as $pos ) {
if( $pos != '' )
$this->smarty->assign(trim($pos), '');
}
}
//When this Controller Object is a Module Container.
if( count($this->moduleOutputs)> 0 && count($this->data['modules']) > 0 ) {
$regionOutputs = array();
foreach( $this->data['modules'] as $module ) {
if( array_key_exists($module['position'], $regionOutputs) )
$regionOutputs[$module['position']] .= $this->moduleOutputs[$module['code']];
else
$regionOutputs[$module['position']] = $this->moduleOutputs[$module['code']];
}
//Set all of modules output To Smarty Template.
foreach ($regionOutputs as $key => $value)
$this->smarty->assign($key,$value);
}
}
//Get Template Output From Smarty.
$content = $this->smarty->fetch($file);
}
ob_end_clean();
return $content;
} else {
exit('Error: Could not load template ' . $file . '!');
}
}
}
?>

I attached source-code.zip include startup.php, controllerBase.php and Smarty files. All of thems located in the file structure that i described.
Attachments
this zip file include startup.php, controllerBase.php and Smarty files. All of thems located in the file structure that i described
Example 1
I will Change ControllerCommonHeader to ControllerBase, located in "catalog\controller\common\"
And Change header.tpl to Smarty Template, located in "catalog\view\theme\default\template\common\header.tpl"
this is a old SourceCode
Code: Select all
class ControllerCommonHeader extends Controller {
public function index( ) {
.
.
}
}
STEP1.1 To Use ControllerBase. only you given ControllerCommonHeader to extends ControllerBase
**index function must declare as Public only fallow by code below.
Code: Select all
class ControllerCommonHeader extends ControllerBase {
public function index( ) {
.
.
}
}
located in "catalog\view\theme\default\template\common\header.tpl".
Change the template code to Smarty Template like this.
Code: Select all
<div class="div1">
<div class="div2">
<a href="{$home}">
<img src="catalog/view/theme/default/image/logo.png" title="{$store}" alt="{$store}" />
</a>
</div>
<div class="div3">{$language}{$search}</div>
</div>
<div class="div4">
<div class="div5">
<a href="{$home}" style="background: url('catalog/view/theme/default/image/icon_home.png') left center no-repeat;">{$text_home}</a>
<a href="{$special}" style="background: url('catalog/view/theme/default/image/icon_special.png') left center no-repeat;">{$text_special}</a>
{if !$logged}
<a href="{$login}" style="background: url('catalog/view/theme/default/image/icon_login.png') left center no-repeat;">{$text_login}</a>
{else}
<a href="{$logout}" style="background: url('catalog/view/theme/default/image/icon_logout.png') left center no-repeat;">{$text_logout}</a>
{/if}
<a href="{$account}" style="background: url('catalog/view/theme/default/image/icon_account.png') left center no-repeat;">{$text_account}</a>
</div>
<div class="div6">
<a href="{$checkout}" style="background: url('catalog/view/theme/default/image/icon_checkout.png') left center no-repeat;">{$text_checkout}</a>
<a href="{$cart}" style="background: url('catalog/view/theme/default/image/icon_basket.png') left center no-repeat;">{$text_cart}</a>
</div>
</div>
When you using ControllerBase, The index function must only be Public function.
I found some Controllers that is Modules Controller have index function declare as protected, so you have to change to Public Function
I will change ControllerCommonLayout to ControllerBase
STEP 2.1
in a file layout.php located in "catalog\controller\common\layout.php"
You only give it extends from ControllerBase like this
Code: Select all
class ControllerCommonLayout extends ControllerBase {
}
in a file layout.tpl located in "catalog\view\theme\default\template\common\layout.tpl"
Change it to Smarty Template
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="{$direction}" lang="{$language}" xml:lang="{$language}">
<head>
<title>{$title}</title>
{if $description}
<meta name="description" content="{$description}" />
{/if}
<base href="{$base}" />
<link rel="stylesheet" type="text/css" href="catalog/view/theme/default/stylesheet/stylesheet.css" />
<script type="text/javascript" src="catalog/view/javascript/jquery/jquery-1.3.min.js"></script>
<script type="text/javascript" src="catalog/view/javascript/jquery/thickbox/thickbox-compressed.js"></script>
<link rel="stylesheet" type="text/css" href="catalog/view/javascript/jquery/thickbox/thickbox.css" />
<script type="text/javascript" src="catalog/view/javascript/jquery/tab.js"></script>
</head>
<body>
<div id="container">
<div id="header">{$header}</div>
<div id="breadcrumb">
{foreach from=$breadcrumbs item=breadcrumb }
{$breadcrumb.separator}
<a href="{$breadcrumb.href}">{$breadcrumb.text}</a>
{/foreach}
</div>
<div id="column_left">
{$left}
</div>
<div id="column_right">
{$right}
</div>
<div id="content">{$content}</div>
<div id="footer">{$footer}</div>
</div>
</body>
</html>
There are 2 variables interesting declared on Template File are $left and $right, that are module positions.
You have to use module position naming as valiable with in Template. The 2 variables store all of modules output that show on it's position.
Attachments
OpenCart 1.2.6 have fixed 2 module position are left and right showing on picture below. - mod-pos.gif (4.15 KiB) Viewed 10812 times
Doesn't work in Opera 9.6x.This is my example web site that a partial of Store Front using Smarty Template.
http://treebhoph.6te.net/
Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig
A problem from Thai language encoding, I alredy solve it. please try again.JNeuhoff wrote:Doesn't work in Opera 9.6x.This is my example web site that a partial of Store Front using Smarty Template.
http://treebhoph.6te.net/
It can work in Opera 9.6.X. now.
Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig
Hello phpuk... what is MVCL framework?phpuk wrote:Hi,
Can you tell me what you think the benefits would be in adding Smarty Templates to this product please as OpenCart already uses an easy to use MVCL framework?
Is a tool for creating templates?
Could you please tell me more? I'm interested in making templates...
thanks a lot
bye
I don't think it's worth the effort though guys, seriously. The template code in opencart is already so simplified, that to add smarty templates to the mix is just adding extra parsing, extra production overhead in the long run.
Please look at the template files and decide for yourself - look at catalog/view/template folders and the catalog/admin/view/template folders. All controllers already have their 'views' separated for quick and easy editing.
http://www.alreadymade.com
Follow me on twitter.com/alreadymade
Does this still work with v 1.4.0. I want to use it because i think malitious code can be added into the view part if someone has access to nothing else just the views. Is that correct? Maybe run a database connection and get data that he is not supposed to get. Is that possible or do any other hazard if he has direct access to the views. Thanks!
Who would have access to the view files? Its no different if he has access to any files.ferisoft wrote:Hello,
Does this still work with v 1.4.0. I want to use it because i think malitious code can be added into the view part if someone has access to nothing else just the views. Is that correct? Maybe run a database connection and get data that he is not supposed to get. Is that possible or do any other hazard if he has direct access to the views. Thanks!
How did I miss this?
To be honest, smarty speeds are not the most spiffy but having this as an option could allow switch to another template engine that offers different syntax like what django uses or even near pure php speeds.
Did Daniel ever program the possibility of allowing you swap out the native opencart template engine with smarty?
930sc ... because it is fun!
Users browsing this forum: No registered users and 4 guests