Location: PHPKode > scripts > Templato > templato/class.template.php
<?php
/*
 * Class.Template
 * Written by Niels Scheffers (hide@address.com)
 * 
 * Last revision: July 25, 2002
 * 
 * This code may be freely used for non-commercial 
 * purposes as long as this copyright notice is kept 
 * intact.
 * 
 * If you use this code, please drop me a line and
 * tell me what you thikn about it :)
 *
 */
 
Class Template
{
	// PUBLIC
	var $output   = "";			// Container for the filled template

	// PRIVATE
	var $template = "";			// Container for the contents of the templatefile

	var $header   = "";			// Container for the header part of the loop
	var $loop     = "";			// Container for the loop part
	var $loopvars = array();	// Variables to replace
	var $footer   = "";			// Container for the footer part of the loop
	
	/**
	 * Template::Template()
	 * CONSTRUCTOR
	 * 
	 * @param $filename 	// Filename of html template
	 * @return 
	 */
	function Template($filename)
	{
		if (file_exists($filename))
		{
			$file = file($filename);
			$this->template = implode(" ", $file);
			return TRUE;
		} else {
			die('Template file $filename not found. Cannot proceed.');
			return FALSE; // Yeah yeah not needed but it just looks better :)
		}
	}
	
	/**
	 * Template::getLoop()
	 * Defines a loop in the template.
	 * 
	 * @param $repeat_string	// String-value defining the loop
	 * @return 
	 */
	function getLoop($repeat_string)
	{
		if ($this->header != "") { $this->template = $this->footer; }	// getLoop is called a second time
		if ($this->template == "") { die('Load a template before calling this function.'); } 
		
		$temp = $this->template;
		$chunks = explode("<!-- $repeat_string -->", $temp);
		
		if (sizeof($chunks) == 3)
		{
			$this->header = $chunks[0];
			$this->loop   = $chunks[1];
			$this->footer = $chunks[2];
			$this->template = "";
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	/**
	 * Template::setLoopVar()
	 * Sets variables to replace.
	 * 
	 * @param $name			// String-value to replace
	 * @param $replacewith	// Replace with
	 * @return 
	 */
	function setLoopVar ($name, $replacewith)
	{
		$this->loopvars[$name] = $replacewith;
	}
	
	/**
	 * Template::resetLoopVar()
	 * Clears the variables to replace.
	 * 
	 * @return 
	 */
	function resetLoopVar()
	{
		$this->loopvars = array();
	}
	
	/**
	 * Template::insertHeader()
	 * Replaces vars in and dumps the header part to the output container.
	 * 
	 * @return 
	 */
	function insertHeader()
	{
		$out = $this->header;
		while (list ($key, $val) = each ($this->loopvars)) {
			$out = str_replace("<!-- $key -->", $val, $out);
		}
		$this->output .= $out;
	}
	
	/**
	 * Template::insertFooter()
	 * Replaces vars in and dumps the footer part to the output container.
	 * 
	 * @return 
	 */
	function insertFooter()
	{
		$out = $this->footer;
		while (list ($key, $val) = each ($this->loopvars)) {
			$out = str_replace("<!-- $key -->", $val, $out);
		}
		$this->output .= $out;
	}
	
	/**
	 * Template::insertLoop()
	 * Replaces vars in and dumps the loop part to the output container.
	 * 
	 * @return 
	 */
	function insertLoop()
	{
		$out = $this->loop;
		while (list ($key, $val) = each ($this->loopvars)) {
			$out = str_replace("<!-- $key -->", $val, $out);
		}
		$this->output .= $out;	
	}
	
	/**
	 * Template::outputRaw()
	 * Replaces vars in and dumps the entire template to the output container.
	 * 
	 * @return 
	 */
	function outputRaw()
	{
		$out = $this->template;
		while (list ($key, $val) = each ($this->loopvars)) {
			$out = str_replace("<!-- $key -->", $val, $out);
		}
		$this->output = $out;	
	}

	
	/**
	 * Template::stripTag()
	 * Removes a template-tag from the template.
	 * 
	 * @param $tag // String-value to strip
	 * @return 
	 */
	function stripTag($tag)
	{
		$menuchunks = explode("<!-- $tag -->", $this->template);
		$this->template = $menuchunks[0] . $menuchunks[2];
	}
}
?>
Return current item: Templato