- Joined
- Jul 23, 2012
- Messages
- 201
- Reaction score
- 128
Hello, in this tutorial i will show you how to create basic template system using PHP.
Last Update: 2013.02.12 / 2:00AM
So here is a list of what we need to do
Explanation for functions and variables
Lets start by creating class skeleton, witch should look something like this:
Next task is to figure out that problems we could encounter for each functions
__construct
this simple function will set active template file, this parametr is optional since it has a default value, viewFile can be set from parseView function too.
addLocalVariable,addGlobalVariable
formatAsArray
parseView
Full class:
Sorry didnt have time to finish this tut, but i will finish this asap...
Last Update: 2013.02.12 / 2:00AM
So here is a list of what we need to do
- Create main class
- create basic functions that we will need
- Variables
- we will need static variable $globalVariables which will be an array
- we will need local variable $localVariables which will be an array
- we will need local variable $viewFile which will be a string
- we will need local variable $viewContent which will be a string
- we will need local variable $cacheEnabled which will be a bool (NEW)
- Functions
- we will need local function __construct
- we will need local function addLocalVariable
- we will need static function addGlobalVariable
- we will need local function formatArrayAs (NEW)
- we will need local function parseView
- Variables
- Write code & Debug
Explanation for functions and variables
- Variables
- $globalVariables:this array will contain all global variables, global variables can be accessed from all view's.
- $localVariables: this array will contain all locally added variables for current view only.
- $viewFile: this string will contain view file name and latter full path of view file.
- $viewContent: this string will contain current template file source.
- $cacheEnabled: this will enabled or disable cache system for active view file
- Functions
- __construct: within this function you can set active template file (optional).
- addLocalVariable: this function will add new variable to local variable list.
- addGlobalVariable:this function will add new variable to global variable list.
- formatArrayAs: this function will parse array to an html formatted code.
- parseView: this function will parse and check all requirements for template file.
Lets start by creating class skeleton, witch should look something like this:
PHP:
class TemplateParser {
/**
* Contains all global scope variables that can be accessed from all template files.
*
* @var array
*/
private static $globalVariables = array();
/**
* Contains current template specified variables, that can be accessed only from current template.
*
* @var array
*/
private $localVariables = array();
/**
* Current view file location.
*
* @var null
*/
private $viewFile = NULL;
/**
* Current view file content.
*
* @var string
*/
private $viewContent = "";
/**
* Enable current view caching.
*
* @var bool
*/
public $cacheEnabled = false;
/**
* Setting up default values for current template
*
* @param null $viewFile
* @param bool $cacheEnabled
*/
public function __construct($viewFile = NULL,$cacheEnabled = false)
{
}
/**
* Adds a local scope variable into array.
*
* @param $id
* @param $value
* @throws Exception
*/
public function addLocalVariable($id,$value)
{
}
/**
* Adds a global scope variable into array.
*
* @param $id
* @param $value
* @throws Exception
*/
public static function addGlobalVariable($id,$value)
{
}
/**
* Formats array items into html scripts, table or list.
*
* @param string $type
* @param $data
* @return string
* @throws Exception
*/
public function formatArrayAs($type = 'table',$data)
{
}
/**
* Parses a view file and outputs generated content.
*
* @param null $viewFile
* @throws Exception
*/
public function parseView($viewFile = NULL)
{
}
}
Next task is to figure out that problems we could encounter for each functions
- addLocalVariable,addGlobalVariable
- we should add a check if $id already exists in variable array's.
- How can we check that? simple,
You must be registered to see links!
- How can we check that? simple,
- we should add a check if $id is set and not empty.
- How can we check that? simple,
You must be registered to see links!
- How can we check that? simple,
- we should insert new variable into array.
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should add a check if $id already exists in variable array's.
- formatAsArray
- we should check if passed $data variable is an array.
- how can we do that? simple
You must be registered to see links!
- how can we do that? simple
- we should check if array contains any items.
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should add a check if $type exists in formating types array.
- How can we check that? simple,
You must be registered to see links!
- How can we check that? simple,
- we should check if passed $data variable is an array.
- parseView (Not updated)
- we should check if view file is set.
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should add a check to check that view file exists
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should read all template contents in to variable!
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should merge local and global variable into one array
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should add a check if that local or global variables array have any items in them
- How can we do that? simple,
You must be registered to see links!
- How can we do that? simple,
- we should check if view file is set.
__construct
PHP:
public function __construct($viewFile = NULL,$cacheEnabled = false)
{
$this->viewFile = $viewFile;
$this->cacheEnabled = $cacheEnabled;
}
addLocalVariable,addGlobalVariable
PHP:
public function addLocalVariable($id,$value)
{
if(array_key_exists($id,$this->localVariables))
{
throw new Exception("Selected key '$id' already exist in array 'localVariables'");
}
if(!isset($id))
{
throw new Exception('Please recheck if $id value is set correctly!');
}
$this->localVariables[$id] = $value;
}
public static function addGlobalVariable($id,$value)
{
if(array_key_exists($id,self::$globalVariables))
{
throw new Exception("Selected key '$id' already exist in array 'self::globalVariables'");
}
if(!isset($id) OR !isset($id))
{
throw new Exception('Please recheck if $id,$value is set correctly!');
}
self::$globalVariables[$id] = $value;
}
PHP:
public function formatArrayAs($type = 'table',$data)
{
if(!is_array($data))
{
throw new Exception('Wrong data type passed. $data only supports arrays! Your input data type:'.gettype($data));
}
$content = '';
if(count($data) > 0)
{
$formatingTypes = array(
'table' => array('<tr>','<td>','</tr>','</td>'),
'list' => array('','<li>','','</li>')
);
if(!array_key_exists($type,$formatingTypes))
{
throw new Exception('Unknown type provided. Your input:'.$type);
}
foreach($data as $string)
{
$content .= $formatingTypes[$type][1].$string.$formatingTypes[$type][3];
}
return $formatingTypes[$type][0].$content.$formatingTypes[$type][2];
}
return $content;
}
PHP:
public function parseView($viewFile = NULL)
{
if($this->viewFile == NULL)
{
if($viewFile == NULL)
{
throw new Exception('Template view file cannot be empty');
}
else
{
$this->viewFile = $viewFile;
}
}
$this->viewFile = 'application/views/'.$this->viewFile.'.view';
if(!is_file($this->viewFile))
{
throw new Exception('You must point to a valid view file!');
}
if($this->cacheEnabled == true AND is_file($this->viewFile.'.cache') AND time() - (10 * 60) < filemtime($this->viewFile.'.cache'))
{
$this->viewContent = file_get_contents($this->viewFile.'.cache');
}
else
{
$this->viewContent = file_get_contents($this->viewFile);
$mergedVariables = array_merge(self::$globalVariables,$this->localVariables);
if(count($mergedVariables) > 0)
{
foreach($mergedVariables as $key => $value)
{
$this->viewContent = str_replace("{".$key."}",$value,$this->viewContent);
}
}
if($this->cacheEnabled == true)
{
file_put_contents($this->viewFile.'.cache',$this->viewContent);
}
}
echo $this->viewContent;
}
Full class:
PHP:
class TemplateParser {
/**
* Contains all global scope variables that can be accessed from all template files.
*
* @var array
*/
private static $globalVariables = array();
/**
* Contains current template specified variables, that can be accessed only from current template.
*
* @var array
*/
private $localVariables = array();
/**
* Current view file location.
*
* @var null
*/
private $viewFile = NULL;
/**
* Current view file content.
*
* @var string
*/
private $viewContent = "";
/**
* Enable current view caching.
*
* @var bool
*/
public $cacheEnabled = false;
/**
* Setting up default values for current template
*
* @param null $viewFile
* @param bool $cacheEnabled
*/
public function __construct($viewFile = NULL,$cacheEnabled = false)
{
$this->viewFile = $viewFile;
$this->cacheEnabled = $cacheEnabled;
}
/**
* Adds a local scope variable into array.
*
* @param $id
* @param $value
* @throws Exception
*/
public function addLocalVariable($id,$value)
{
if(array_key_exists($id,$this->localVariables))
{
throw new Exception("Selected key '$id' already exist in array 'localVariables'");
}
if(!isset($id))
{
throw new Exception('Please recheck if $id,$value is set correctly!');
}
$this->localVariables[$id] = $value;
}
/**
* Adds a global scope variable into array.
*
* @param $id
* @param $value
* @throws Exception
*/
public static function addGlobalVariable($id,$value)
{
if(array_key_exists($id,self::$globalVariables))
{
throw new Exception("Selected key '$id' already exist in array 'self::globalVariables'");
}
if(!isset($id) OR !isset($id))
{
throw new Exception('Please recheck if $id,$value is set correctly!');
}
self::$globalVariables[$id] = $value;
}
/**
* Formats array items into html scripts, table or list.
*
* @param string $type
* @param $data
* @return string
* @throws Exception
*/
public function formatArrayAs($type = 'table',$data)
{
if(!is_array($data))
{
throw new Exception('Wrong data type passed. $data only supports arrays! Your input data type:'.gettype($data));
}
$content = '';
if(count($data) > 0)
{
$formatingTypes = array(
'table' => array('<tr>','<td>','</tr>','</td>'),
'list' => array('','<li>','','</li>')
);
if(!array_key_exists($type,$formatingTypes))
{
throw new Exception('Unknown type provided. Your input:'.$type);
}
foreach($data as $string)
{
$content .= $formatingTypes[$type][1].$string.$formatingTypes[$type][3];
}
return $formatingTypes[$type][0].$content.$formatingTypes[$type][2];
}
return $content;
}
/**
* Parses a view file and outputs generated content.
*
* @param null $viewFile
* @throws Exception
*/
public function parseView($viewFile = NULL)
{
if($this->viewFile == NULL)
{
if($viewFile == NULL)
{
throw new Exception('Template view file cannot be empty');
}
else
{
$this->viewFile = $viewFile;
}
}
$this->viewFile = 'application/views/'.$this->viewFile.'.view';
if(!is_file($this->viewFile))
{
throw new Exception('You must point to a valid view file!');
}
if($this->cacheEnabled == true AND is_file($this->viewFile.'.cache') AND time() - (10 * 60) < filemtime($this->viewFile.'.cache'))
{
$this->viewContent = file_get_contents($this->viewFile.'.cache');
}
else
{
$this->viewContent = file_get_contents($this->viewFile);
$mergedVariables = array_merge(self::$globalVariables,$this->localVariables);
if(count($mergedVariables) > 0)
{
foreach($mergedVariables as $key => $value)
{
$this->viewContent = str_replace("{".$key."}",$value,$this->viewContent);
}
}
if($this->cacheEnabled == true)
{
file_put_contents($this->viewFile.'.cache',$this->viewContent);
}
}
echo $this->viewContent;
}
}
Sorry didnt have time to finish this tut, but i will finish this asap...
Last edited: