<?php
/************************************************
* MicketPro Template Engine v2.3
* By: Chris "TunkeyMicket" Watford
* [tunkeymicket@tunkeymicket.com]
*
* http://www.tunkeymicket.com
*
*************************************************/

/************************************************
* __registerTemplateFile(String $templateFile);
*
* Description: Sets up a new template file to be
* parsed. Returns the $uid of the file so you can
* add variables that should be replaced.
*
* Parameters: You send to it the file you wish to
* be added into the template file array.
*
* Returns: $uid, which can be used to reference
* the template file to add variables to be
* replaced.
*
* Usage:
*
* $handle = __registerTemplateFile("file.txt");
*
*************************************************/
function __registerTemplateFile ($templateFile) {
	global $__templateFile;

	$__templateFile[] = $templateFile;

	end($__templateFile);
	$uid = key($__templateFile);

	return $uid;
}

/************************************************
* __registerWatch(UID $uid, String $watchItem,
*	String $replaceWith);
*
* Description: Adds a token to watch for and what
* to replace it with into an array keyed by the
* template UID.
*
* Parameters: You send to it the UID of the
* template file you have registered, along with
* the token to "watch" for [$watchItem], and
* what to replace it with [$replaceWith].
*
* Returns: void
*
* Usage:
*
* __registerWatch($handle,"%%EMAIL%%",
*	"user@host.net");
* __registerWatch($handle,"%%NAME%%",$name);
*
*************************************************/
function __registerWatch ($uid, $watchItem, $replaceWith) {
	global $__watchList;

	$__watchList[$uid][$watchItem] = $replaceWith;
}

/************************************************
* __parseTemplate(UID $uid);
*
* Description: Replaces all of the tokens in the
* template file with their respective
* replacements and adds them to an array.
*
* Parameters: You send to it the UID of the
* template file you have registered.
*
* Returns: It returns a parsed array with the
* replaced data inside of it. You can then
* iterate through the indexed array and use the
* data as needed.
*
* Usage:
*
* $newData = __parseTemplate($handle);
*
*************************************************/
function __parseTemplate ($uid) {
	global $__watchList, $__templateFile;

	$fileArray = file($__templateFile[$uid]);

	for($i = 0; $i < sizeof($fileArray); $i++) {
		foreach($__watchList[$uid] as $item => $replacement) {
			$fileArray[$i] = str_replace($item,$replacement,$fileArray[$i]);
		}
	}

	return $fileArray;
}

/****************************************************************
* SAMPLE CODE:
*
* TEMPLATE1.TXT:
*
* <html><head><title>{%TITLE%}</title></head>
* <body>
* %{BODYTEXT}%
* <br><div align=center>
*
* TEMPLATE2.TXT:
*
* <a href="mailto:^^EMAIL^^">##NAME##</a>
* </div>
* </body>
* </html>
*
* INDEX.PHP:
*
* <?php
* 	require("mp_template_engine.php");
*
*	$h_id = __registerTemplateFile("TEMPLATE1.TXT");
*	__registerWatch($h_id,"{%TITLE%}","TITLE-O-DOC");
*	__registerWatch($h_id,"%{BODYTEXT}%","Hi2u From Body");
*
*	$body_header = __parseTemplate($h_id);
*
*	$f_id = __registerTemplateFile("TEMPLATE2.TXT");
*	__registerWatch($f_id,"^^EMAIL^^","user@host.net");
*	__registerWatch($f_id,"##NAME##","John Doe");
*
*	$body_footer = __parseTemplate($f_id);
*
*	for($i = 0; $i < sizeof($body_header); $i++) {
*		echo $body_header[$i];
*	}
*
*	for($i = 0; $i < sizeof($body_footer); $i++) {
*		echo $body_footer[$i];
*	}
* ?>
****************************************************************/
?>