Location: PHPKode > scripts > QTemplate > qtemplate/class.template.inc
<?
////////////////////////////////////////////////////////////////
// QTemplate class ( build 031118 )
// (c) Gregory A. Rozanoff, 2003
////////////////////////////////////////////////////////////////

class template {

 // Private

 var $TEMPLATES = array();	// templates
 var $VALUES = array();		// entryes

 /*
 template(string file_name)
 Class constructor
 */
 function template($path) {
	 $file = implode("", file($path));
	 $this->get_templates($file);
 }

 /*
 get_templates(string template)
 Parse template file and assigns $TEMPLATES array
 */
 function get_templates(&$html) {
	$rez = preg_match_all("/<tpl:([a-z0-9_-]+)>(.*)<\/tpl:\\1>/si", $html, $matches);
	for($i = 0; $i < $rez; $i++) {
		$this->TEMPLATES[$matches[1][$i]] = preg_replace(" /<tpl:([a-z0-9_-]+)>(.*)<\/tpl:\\1>/si", "<tpl:\\1 />", $matches[2][$i]);
		$this->get_templates($matches[2][$i]);
	}
 }

 /*
 wrap(string root_entry_id)
 */
 function wrap($tid) {
 	$tmp = $this->TEMPLATES[$tid];
	$res = preg_match_all("/<tpl:([a-z0-9_-]+)([\s]+)\/>/si", $tmp, $matches);
	for ($i = 0; $i < $res; $i++)
		if (isset($this->VALUES[$matches[1][$i]])) $tmp = preg_replace("/<tpl:".$matches[1][$i]."([\s]+)\/>/s", $this->VALUES[$matches[1][$i]], $tmp);
			else $tmp = preg_replace("/<tpl:".$matches[1][$i]."([\s]+)\/>/s", $this->wrap($matches[1][$i]), $tmp);
	return $tmp;
 }

 // Public

 /*
 assign (string entry_id, [string value])
 If a "." (dot) is the first symbol of entry_id, than value will be appended to the entry
 */
 function assign($tid, $val = '') {
	if ("." == substr($tid, 0, 1)) {
		$tid = substr($tid, 1);
		$append = TRUE;
	}
	if (isset($this->TEMPLATES[$val]) && $val) $tmp = $this->wrap($val);
		else $tmp = $val ? $val : $this->wrap($tid);
	
	$this->VALUES[$tid] = $append ? $this->VALUES[$tid].$tmp : $tmp;
}

 /*
 out(string entry_id)
 */
 function out($id) {
 	return $this->VALUES[$id];
 }
 
 /*
 reset (string entry_id)
 Unset the entry
 */
 function reset($tid) {
	 if ($tid) unset($this->VALUES[$tid]);
		 else $this->VALUES = array();
 }

}
?>
Return current item: QTemplate