Version: 1.0 Beta 1

Type: Function

Category: HTML

License: GNU General Public License

Description: ASTemplate is a powerful but easy to use template engine for PHP. It uses an XML format to specify templates, and it then links to other files specified in the root XML code. It supports looping (see documentation) which is a unique but very useful feature, especially for sites with dynamic content.



<?PHP

/* ASTemplate: Advanced PHP template engine.
   Copyright (C) 2002 Adam Atlas
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

$outputhtml = "";
$templateconsts = "";
$templatesearchpath = "";

function MunchASTemplate ($tempxml, $tempconsts, $searchpath) {
	global $outputhtml;
	global $templateconsts;
	global $templatesearchpath;
	
	$templateconsts = $tempconsts;
	$templatesearchpath = $searchpath;
	
	$parser = xml_parser_create();
	
	xml_set_element_handler($parser, "MTStartElement", "MTEndElement");
	xml_set_character_data_handler($parser, "MTCharacterData");
	xml_parse($parser, $tempxml);
	xml_parser_free($parser);
	
	return $outputhtml;
}

function file2string($filepath) {
	$filehandle = fopen($filepath, "r");
	$result = "";

	while ($data = fread($filehandle, 4096)) {
		$result .= $data;
	}
	
	fclose($filehandle);
	return $result;
}

function MTStartElement($parser_instance, $element_name, $attrs) {
	global $outputhtml;
	global $templateconsts;
	global $templatesearchpath;
	
	switch($element_name) {
		case "STATIC" :
			$filecontents = file2string($templatesearchpath . "/" . $attrs['SRC']);
			$keys = array_keys($templateconsts['static']);
			$values = array_values($templateconsts['static']);
			
			for ($a = 0; $a < count($templateconsts['static']); $a++) {
				$filecontents = str_replace("<!-- ASTemplateConst $".$keys[$a]." -->", $values[$a], $filecontents);
			}
			
			$outputhtml .= $filecontents;
			break;
			
		case "LOOP" :
			$filecontents = file2string($templatesearchpath . "/" . $attrs['SRC']);
			$statickeys = array_keys($templateconsts['static']);
			$staticvalues = array_values($templateconsts['static']);
			
			
			for ($a = 0; $a < count($templateconsts['static']); $a++) {
				$filecontents = str_replace("<!-- ASTemplateConst $".$statickeys[$a]." -->", $staticvalues[$a], $filecontents);
			}
			
			$baseforloop = $filecontents;
			
			$loopconsts = $templateconsts['loops'][$attrs['VALCONST']];
			
			for ($e = 0; $e < count($loopconsts); $e++) {
				$baseforloop = $filecontents;
				$currentconstants = $loopconsts[$e];
				$currentloopkeys = array_keys($currentconstants);
				$currentloopvalues = array_values($currentconstants);
				for ($i = 0; $i < count($currentconstants); $i++) {
					$baseforloop = str_replace("<!-- ASTemplateLoopItem $".$currentloopkeys[$i]." -->", $currentloopvalues[$i], $baseforloop);
				}
				$outputhtml .= $baseforloop;
			}
			
			break;
			
		case "ASTEMPLATE" :
			break;
	}
}

function MTCharacterData($parser_instance, $xml_data) {
	// Do nothing!
}

function MTEndElement($parser_instance, $element_name) {
	// Actually, we don't need to do anything here either.
}

/*

$tempconsts:
    $static:
        Array of global static constants, in the form of key -> value.
    $loops:
        $loopname:
            Array of arrays of constants that will be enumerated for loops using $loopname.
        $foo:
            More, blah blah...

*/
?>