<?php
/******************************************\
* IceDice template class
* http://icedice.org
* php based template engine
* email: icediceint at gmail dot com
\******************************************/
define('IDTPL',true);
class IDtpl {
const _VER= '1.3'; //class version
private $_tplPath= './'; //path for tpl directory
private $_data= array(); //tpl variables
private $_output= ''; //parsed output will be stored here
private $_exec= 0; //parsing time
private $_keep= false; //store or rewrite output
//class constructor
//$path - path for tpl directory
public function __construct($path= '',$keep=false) {
if (!empty($path) && substr($path, -1)<>'/')
$path.= '/';
if (is_dir($path))
$this->_tplPath= $path;
if ($keep) $this->_keep= true;
return true;
}
//return current time
private function ETime() {
$temp= explode(' ', microtime());
return $temp[1] + $temp[0];
}
//return tpl executing time, it's only for debugging purpose
public function Exectime($round=4) {
return round($this->_exec,$round);
}
//define template variable that can be shown while parsing tpl file
public function Define($key, $value, $rewrite=true) {
if (!$this->isdefined($key) or $rewrite) {
$this->_data[$key]= $value;
return true;
} else return false;
}
//delete defined variable
public function UnDefine($key) {
unset($this->_data[$key]);
return true;
}
//clear all defined variables
public function Clear() {
$this->_data= array();
return true;
}
//check is var already defined
public function IsDefined($key) {
return (isset($this->_data[$key])) ? true : false;
}
//check is there any defined variables at all
public function IsEmpty() {
return (empty($this->_data)) ? true : false;
}
//parse template file and prepare output
//$file - template file
public function Template($file) {
if (is_file($this->_tplPath.$file)) {
$start= $this->etime();
$d= &$this->_data;
ob_start();
require $this->_tplPath.$file;
if ($this->_keep)
$this->_output.= ob_get_contents();
else
$this->_output= ob_get_contents();
ob_end_clean();
$this->_exec += $this->etime()-$start;
return true;
}
die('<b>Can\'t load template file:</b> "'.$this->_tplPath.$file.'".');
return false;
}
//prints output on the screen
public function Display() {
echo $this->_output;
}
//return stored output
public function GetHtml() {
return $this->_output;
}
}
?>