All pastes #783605 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-11-19 08:08 UTC
rendered paste body
<?php
class template_core
{	
	function compile_template( $template_dir, $template_name )
	{
		// /\{[a-z]+\.[a-z_]+\}/ ignore kthx.
		
		$dir = "templates/$template_dir/$template_name.tpl";
		$template = file_get_contents( $dir );
		
		$replaceall = preg_match_all( "/\{[a-z]+\.[a-z_]+\}/", $template, $matches, PREG_OFFSET_CAPTURE );
		//print_r($lawl);
		
		for( $i = 0; $i <= (count( $matches ) + 1); $i++ )
		{
			
			//echo( $matches[0][$i][0] );
			
			/**
			 * First we get the stuff we're gonna be replacing.
			 */
			
			$get_command = str_replace( "{", "", $matches[0][$i][0] );
			$get_command = str_replace( "}", "", $get_command );
			$get_command = explode( ".", $get_command );
			// That's it, we have the commands, we just need to get the offset, but we can do that later.
			
			switch( $get_command[0] )
			{
				case "phrase":
					$parse = $this->parse_phrase( $get_command[1] );
					break;
				
				case "setting":
					$parse = $this->parse_setting( $get_command[1] );
					break;
			}
			
			$get_offset = $matches[0][$i][1];
			$get_length = strlen( $matches[0][$i][0] );
			
			$get_new_len = strlen( $parse );
			
			if( $get_length > $get_new_len )
			{
				$get_new_length = $get_new_len;
				$get_new_offset = $get_offset - ($get_length - $get_new_len);
			}
			elseif( $get_length == $get_new_len ) 
			{
				$get_new_length = $get_length;
				$get_new_offset = $get_offset;
			}
			else 
			{
				$get_new_length = $get_new_len;
				$get_new_offset = $get_offset + ($get_new_len - $get_length);
			}
			
			if( isset( $template_replaced ) )
			{
				$template_replaced_start = substr( $template_replaced, 0, $get_new_offset );
				$template_replace_end = substr( $template_replaced, $get_new_offset, $get_new_length );
				$template_replaced = $template_replaced_start . $parse . $template_replace_end;
			}
			else 
			{
				$template_replaced_start = substr( $template, 0, $get_offset );
				$template_replace_end = substr( $template, $get_offset, $get_length );
				$template_replaced = $template_replaced_start . $parse . $template_replace_end;
			}
		}
		
		echo( $template_replaced );
	}
	
	function parse_phrase( $phrase_name )
	{
		$return = $this->core->db->fetch_array( "SELECT `cont` FROM `phrases` WHERE `name` = '$phrase_name'" );
		if($return == false)
		{
			return "Could not parse.";
		}
		else 
		{
			return $return;
		}
	}
	
	function parse_setting( $setting_name )
	{
		
	}

}
?>