Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/template_parser.inc
<?php

function template_parser ($html_template_name, $tag_values)	{
	GLOBAL $include_root;
	GLOBAL $html_template_path;
	
    //echo "<hr color=red>".pretty_var_dump($tag_values["{REPORT_PROFILE}"]).pretty_var_dump($html_template_name)."<hr color=red>";
    //echo "<hr color=red>template:".pretty_var_dump($html_template_name)."tags:".pretty_var_dump($tag_values)."<hr color=red>";
    
	// Read in template file, suppress error messages
	//$template_path = getenv("DOCUMENT_ROOT")."/".$include_root."/html_templates/".$html_template_name;
	$template_path = $html_template_path.$html_template_name;
	//$template_path = "html_templates\\".$html_template_name;
	$final_html = @implode("", (@file($template_path)));
	if(!$final_html){echo "No Template";}
	
	//Place any tag marked with "{" and "}" into an array named $tags	
	preg_match_all( '/\{.*?\}/', $final_html, $tags );

	//Loop through the array of tags found above
	foreach ($tags[0] as $tag)	{

		
		$replace_tag_html = $tag_values[$tag];

		if ($tag == "{REMAINING_TAGS}")	{$replace_tag_html = "{REMAINING_TAGS}";}

		//If no tag value was found to replace the tag with, replace tag with error message
		if (!$replace_tag_html){$replace_tag_html = "VALUE TO REPLACE ".$tag." NOT FOUND";}
		
		//Replace the template tag with the dynamic HTML in "$replace_tag_html"
		$final_html = str_replace($tag, $replace_tag_html, $final_html);
		unset ($tag_values[$tag]);
	}
	
	//If there are leftover tags not defined in the HTML template, put them into a dynamically developed table
	if($tag_values)	{
		$remaining_tags_table .= "\n<table>";
		$td_count = 0;
		
		foreach($tag_values AS $key => $current_tag_value)	{
		
			//Make rows two questions long using <tr> and </tr>
			if ($td_count == 2)	{$remaining_tags_table .= "</tr>\n"; $td_count = 0; }
			if ($td_count == 0) {$remaining_tags_table .= "<tr>\n"; }
			
			//if the array is not the tag id's, produce HTML
			if($key != "tag_id")	{
				$remaining_tags_table .= "\n<td class=\"wrap\" id=\"QUEST_".$tag_values["tag_id"][$key]."\">";
				$remaining_tags_table .= $current_tag_value;
				$remaining_tags_table .= "</td>\n";
				$td_count++;
			}
			
		}

		//If there are an odd numer of questions, finish the table row and close it
		if($td_count == "1"){$remaining_tags_table .= "<td>&nbsp;</td></tr>\n";}
		if($td_count == "2"){$remaining_tags_table .= "</tr>\n";}

		$remaining_tags_table .= "</table>\n";

		

		}
	
	//Replace the "{REMAINING_TAGS}" tag in the HTML template with the new table of questions, or nothing if there wer no leftover tags.
	$final_html = str_replace("{REMAINING_TAGS}", $remaining_tags_table, $final_html);
	
	//Display the final result of all the replacing above
	echo $final_html;
}
?>
Return current item: Homeless Mangement Information System