Page 1 of 3

SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Apr 15, 2009 1:24 am
by treebhoph
I want to contribute source code.

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/

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Apr 15, 2009 2:31 pm
by treebhoph
SOURCE CODE

I write English. Maybe you meet missing word or missing grammy because I'm asian from Thailand.
I hope you can understand my word.

>:D
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/
3. Copy a "controllerBase.php" to folder "system/engine/", so a file structure must be..

Code: Select all

system/engine/controllerBase.php
4. Edit a "startup.php" located at "system/" folder. Edit by write a following code.

Code: Select all

require_once(DIR_SYSTEM . 'engine/controllerBase.php');
include(DIR_SYSTEM . 'library/smarty/libs/Smarty.class.php'); 
5. Create folder named "templates_c" located at web root directory, then CMOD to 757.
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 . '!');
    	}
	}
}
?>
O0
I attached source-code.zip include startup.php, controllerBase.php and Smarty files. All of thems located in the file structure that i described.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Apr 15, 2009 3:25 pm
by treebhoph
How to use.

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( ) {
		.
		.
	}
}
STEP1.2 Go to file header.tpl , a Template View that working with ControllerCommonHeader ,
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>
**Important
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

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Apr 15, 2009 4:01 pm
by treebhoph
Example 2

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 {
}
STEP 2.2
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>
Explanation
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.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Apr 15, 2009 5:03 pm
by JNeuhoff
This is my example web site that a partial of Store Front using Smarty Template.
http://treebhoph.6te.net/
Doesn't work in Opera 9.6x.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Apr 15, 2009 6:05 pm
by treebhoph
JNeuhoff wrote:
This is my example web site that a partial of Store Front using Smarty Template.
http://treebhoph.6te.net/
Doesn't work in Opera 9.6x.
A problem from Thai language encoding, I alredy solve it. please try again.
It can work in Opera 9.6.X. now.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu Apr 16, 2009 1:28 am
by maddog986
What is "Smarty Tempalte"??

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu Apr 16, 2009 3:00 am
by phpuk
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?

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu Apr 16, 2009 5:25 pm
by JNeuhoff
Here is a summary, including a crash course about the reasons for using Smarty. Having looked at it, it seems Opencart's templates already offers quite a similar range of features. But hey, it's all about choice, and if someone is experienced with Smarty, why not have it?

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu May 21, 2009 12:32 am
by tussler
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?
Hello phpuk... what is MVCL framework?
Is a tool for creating templates?
Could you please tell me more? I'm interested in making templates...
thanks a lot
bye

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu May 21, 2009 2:09 am
by JNeuhoff
what is MVCL framework?

MVC stands for Model-View-Controller framework. Not sure what L stands for.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu May 21, 2009 2:19 am
by phpuk
L = Language

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Thu May 21, 2009 6:22 am
by tussler
Thanks for the answers guys.
I'm not familiar with MVC (i Guess it's that famous frame, isn't it?)
Could you help me with some other suggestions on how to create templates?
Thanks anyway!
bye bye

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Tue May 26, 2009 1:57 pm
by readyman
The ONLY good thing that can come out of this setup is the fact that you can quickly use an EXISTING smarty template design and plug it in by just changing a few of the block element names. But, you're still going to need to change & things.

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.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Tue May 26, 2009 10:03 pm
by Daniel
I can make the page controller us the template class. Using that method means you can swap in and out the template class with the smarty one.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Mar 03, 2010 4:10 pm
by ferisoft
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!

Re: SMARTY TEMPLATE. I'm coding Controller that support Smarty

Posted: Wed Mar 03, 2010 10:50 pm
by Qphoria
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!
Who would have access to the view files? Its no different if he has access to any files.

Re: SMARTY TEMPLATE. I'm coding Controller that support Smar

Posted: Sun Feb 06, 2011 6:21 am
by SapporoGuy
Wow!

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?

Re: SMARTY TEMPLATE. I'm coding Controller that support Smar

Posted: Sun Feb 06, 2011 9:39 am
by Qphoria
never have.. never will

Re: SMARTY TEMPLATE. I'm coding Controller that support Smar

Posted: Sun Feb 06, 2011 1:42 pm
by SapporoGuy
Any reason why?