Location: PHPKode > scripts > Cached TemplatEze > cached-templateze/tpl.php
<?
////////////////////////////////////////////////////////////////
// TemplatEze 2
//
// Based on TemplatEze class by David Agar
// Code improved by Gregory A. Rozanoff
//
// Changelist (2004-10-04)
// (Legend: + major update; - minor update; * change)
//
//	+ Added Template Set support
//	+ Added perfomance measurement
//      + Added conditional blocks support
//	+ Added Content Caching
//	+ Added Output Buffering
//	+ Added array assignment to assign() function
//	- Removed 'Change Tag' feature
//	- Removed Error Tracking feature: faster, but not safe :)
//	* Functions set(), setloop() and tplblock() merged into one assign() function
//	* Code cleaned up and some functions renamed for more readability
//	* substr_replace() used instead of str_replace() in _block() function
//	* Template sintax changed for more readablity
//
////////////////////////////////////////////////////////////////

class tpl {
 var
	$tpl_ext = '.html',	// Default template extension
	$start = "<tpl:",	// Opening tag branch
	$close = "</tpl:",	// Closing tag branch
	$end = ">";

 function assign($tag, $var = NULL) {
	if ('array' == gettype($tag)) $this->TMPL = $this->_eval($this->TMPL, $tag);
	elseif ($var) $this->TMPL = str_replace($this->start.$tag.$this->end, $var, $this->TMPL);
	elseif ($block = $this->summon($tag)) $this->TMPL = str_replace($this->start.$tag.$this->end, $block, $this->TMPL);
	else $this->_block($tag);
 }
 
 function summon($tpl){
	$path = $this->TemplateDir.$this->_VER_."/".$tpl.$this->tpl_ext;
	if (!file_exists($path)) $path = $this->TemplateDir.$tpl.$this->tpl_ext;
	if ($fp = @fopen($path, "r")) {
		$data = fread($fp, filesize($path));
		fclose($fp);
		return $data;
	} else  return FALSE;
 }

 function _block($tag) {
	global $$tag;
	$start_tag = $this->start.$tag.$this->end;
	$stop_tag = $this->close.$tag.$this->end;

	$pattern_start = strpos($this->TMPL, $start_tag);
	$start_pos = $pattern_start + strlen($start_tag);
	$end_pos = strpos($this->TMPL, $stop_tag);
	$pattern_end = $end_pos + strlen($stop_tag);

	if ($pattern_start != $end_pos) {
		$new_code = "";
		$loop_code = substr($this->TMPL, $start_pos, $end_pos - $start_pos);
		if ('array' == gettype($$tag))
			foreach ($$tag as $chank) $new_code .= $this->_eval($loop_code, $chank);
		$this->TMPL = substr_replace($this->TMPL, $new_code, $pattern_start, $pattern_end - $pattern_start);
	}
 }

 function _eval($html, &$arr) {
	$tags = array(); $vars = array();
	foreach ($arr as $key => $val) {
		array_push($tags, $this->start.$key.$this->end);
		array_push($vars, $val);
	}
	return @str_replace($tags, $vars, $html);
 }
 
 function _getmicrotime() {
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec); 
 }

}
?>
Return current item: Cached TemplatEze