Code: Select all
<?php
/*
* Author:Deewendra G. Shrestha
* This class provides basic smarty type functionalities for PHP based template
* Last Modification Date : March 02, 2009 by Deewendra G. Shrestha
*/
class Template
{
private $variables;
private $errors;
private $templatePath;
private $templateDir;
private $css;
/*
* Constructor for the class. You can assign default values here.
* Please use upper case letters for all the variable assignments
*/
public function Template()
{
$this->variables = array();
$this->errors = array();
$this->templateDir = CONST_ROOT_BASE.'Templates/';
$this->assign("TPL_FRAMEWORKBASE", CONST_FRAMEWORK_BASE);
$this->assign("TPL_ROOTBASE", CONST_ROOT_BASE_URL);
$this->assign("TPL_CSSBASE", CONST_CSS_BASE_URL);
$this->assign("TPL_IMGBASE", CONST_IMG_BASE_URL);
$this->assign("ROOT_BASE", CONST_ROOT_BASE);
$this->css = array(
array('NAME'=>'reset','TITLE'=>NULL,'TYPE'=>NULL),
array('NAME'=>'Red/css','TITLE'=>NULL,'TYPE'=>NULL),
array('NAME'=>'Red/menu','TITLE'=>NULL,'TYPE'=>NULL),
array('NAME'=>'Blue/css','TITLE'=>'blueCss','TYPE'=>"ALT"),
array('NAME'=>'Blue/menu','TITLE'=>'blueMenu','TYPE'=>"ALT"),
);
}
/*
* This function adds css style to the existing css style lists
* Parameter:
$cssArray = an array of pattern array('NAME'=>'Blue/menu','TITLE'=>'blueMenu','TYPE'=>"ALT")
*/
public function addCss($cssArray)
{
$cssFile = CONST_CSS_BASE.$cssArray['NAME'].'.css';
if(file_exists($cssFile))
array_push($this->css,$cssArray);
else
array_push($this->errors,'Invalid css file name provided.');
}
/*
* This function unsets or deletes the value assigned to the class variable "variables"
* Parameter:
$index = any string or array
*/
public function destroy($index=NULL)
{
if(!is_null($index))
{
if(is_array($index))
{
$index = array_change_key_case($index,CASE_UPPER);
foreach($index as $val)
if(isset($this->variables[$val]))
unset($this->variables[$val]);
}
else
{
$index = strtoupper($index);
if(isset($this->variables[$index]))
unset($this->variables[$index]);
}
}
}
/*
* assigns property to the template file, for standard compliant all the
* array index has been converted to upper case
* Please make sure that you use UPPER CASED variable names in the template files
*/
public function assign($key=NULL,$val=NULL)
{
if(!is_null($key))
{
if(is_array($key)){
$key = array_change_key_case($key,CASE_UPPER);
$this->variables = array_merge($this->variables,$key);
}
else
$this->variables[strtoupper($key)] = $val;
}
else
array_push($this->errors,'Assignment done on null key.');
}
/*
* This function sets the template file
* Parameters -
* $template is the template file name
* $path accepts two values relative/absolute(questionable) to identify the type of path
* default value is "relative"
* relative path would set the template path from the default template path(check the constructor to find out)
* absolute path would set the template path from the current working file
*/
public function setTemplate($template=NULL,$path="relative")
{
$path=is_null($path) || trim($path)==''?"relative":strtolower($path);
$pathOptions = array("absolute","relative");
if(!is_null($template))
{
if(in_array($path,$pathOptions))
{
switch($path)
{
case "absolute":
$this->templatePath = $template;
break;
case "relative":
$this->templatePath = $this->templateDir.$template;
break;
}
if(file_exists($this->templatePath))
return true;
else
{
array_push($this->errors,'Invalid template file: '.$this->templatePath);
$this->templatePath = NULL;
return false;
}
}
else{
array_push($this->errors,'Template path must be either relative or absolute.');
}
}
else
array_push($this->errors,'Cannot assign null values for template paths.');
}
/*
* This function returns the HTML equivalent of the template after PHP preocessing
*/
public function fetch($template=NULL,$path=NULL)
{
if(!is_null($template))
$this->setTemplate($template,$path);
if(!is_null($this->templatePath))
{
$this->assign('TPL_CSS',$this->css);
ob_start();
foreach($this->variables as $key=>$val)
$$key = $val;
include_once($this->templatePath);
return ob_get_clean();
}
return false;
}
/*
* This function prints the HTML equivalent of the template after PHP preocessing
*/
public function display($template=NULL,$path=NULL)
{
echo $this->fetch($template,$path);
}
public function showErrors()
{
print_r($this->errors);
}
public function displayError($errs,$title='Errors')
{
$title=!is_null($title)?$title:'Errors';
$this->assign('TPL_WIDTH','300');
$this->assign('TPL_ERR_TITLE',$title);
$this->assign('TPL_ERRORS',$errs);
$content = $this->fetch('error.php');
$this->destroy(array('TPL_WIDTH','TPL_ERR_TITLE','TPL_ERRORS'));
return $content;
}
}
?>
If i do :
Code: Select all
$objTemplate = new Template();
$objTemplate->setTemplate('footer.php','absolute');
$objTemplate->display();//THIS ONE OUTPUTS THE CONTENT
$objTemplate->display();//THIS ONE DOESN'T
Thanks a lot in advance