<?php
class Template {
private $FileName;

public function __cunstruct($Name){
$this->FileName = $Name;
}

public function FileName($Name){
$this->FileName = $Name;
}

public function Create($Code){
preg_match_all('#<!-- ([^<].*?) (.*?)? ?-->#', $Code, $Blocks);

$Text_Blocks = preg_split('#<!-- [^<].*? (?:.*?)? ?-->#', $Code);

$PHP_Blocks = Array();

for($i = 0, $size = sizeOf($Blocks[1]); $i < $size; $i++){
$Block = &$Blocks[1][$i];

switch($Blocks[1][$i]){
case 'IF':
$PHP_Blocks[] = '<?php ' . $this->BuildIF($Blocks[2][$i], false) . ' ?>';
break;

case 'ELSE':
$PHP_Blocks[] = '<?php }else{ ?>';
break;

case 'ELSEIF':
$PHP_Blocks[] = '<?php ' . $this->BuildIF($Blocks[2][$i], true) . ' ?>';
break;

case 'ENDIF':
$PHP_Blocks[] = '<?php } ?>';
break;

default:

break;
}
}

$Template = '';
for ($i = 0, $size = sizeof($Text_Blocks); $i < $size; $i++){
$Trim_Check = trim($Text_Blocks[$i]);
$Template .= (($Trim_Check != '') ? $Text_Blocks[$i] : '') . ((isset($PHP_Blocks[$i])) ? $PHP_Blocks[$i] : '');
}

$Template = str_replace(' ?><?php ', ' ', $Template);

$Template = preg_replace('#\?\>([\r\n])#', '?>\1\1', $Template);

return $Template;
}

function BuildIF($Statement, $ElseIF){
preg_match_all('/(?: "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | [(),] | [^\s(),]+)/x', $Statement, $Tokens);

$Tokens = $Tokens[0];

for($i = 0, $size = sizeOf($Tokens); $i < $size; $i++){
$Token = &$Tokens[$i];

switch($Token){
case '!=':
$Token = '!=';
break;

case '==':
$Token = '==';
break;

case '<':
$Token = '<';
break;

case '>':
$Token = '>';
break;

case '<=':
$Token = '<=';
break;

case '>=':
$Token = '>=';
break;

case '&&':
$Token = '&&';
break;

case '||':
$Token = '||';
break;

case '%':
$Token = '%';
break;

case '*':
$Token = '*';
break;

case '/':
$Token = '/';
break;

default:
break;
}

if(!sizeof($Tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $Tokens)) == ''){
$Tokens = array('false');
}

return (($ElseIF) ? '} else if (' : 'if(') . (implode(' ', $Tokens) . '){ ');
}
}

function Write($Template){
if ($Handle = @fopen($this->FileName, 'wb')){
@fwrite ($Handle, $Template);
@fclose($Handle);
}
}
}
?>[/0][/2][/2][/1][/1][/1]




An example usage would be like so...



Test.html;
<?php
$Name = "Hi";
$Pass = "Google";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test &bull; Home</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css; charset=utf-8" />
</head>

<body>
<!-- IF $Name == 'Hi' && $Pass != 'Google' -->Welcome Hi!<!-- ELSE -->Loser!<!-- ENDIF -->
</body>
</html>




Index.php;
<?php
include("Template.class.php");

$Template = new Template('Test.php');

$HTML = @file_get_contents('Test.html');

$PHP = $Template->Create($HTML);

$Template->Write($PHP);

ob_start();
include("Test.php");
$Contents = ob_get_contents();
ob_end_clean();

echo str_replace(__FILE__, $Contents, __FILE__);
?>




Output'd Test.php;
<?php
$Name = "Hi";
$Pass = "Google";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test &bull; Home</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css; charset=utf-8" />
</head>

<body>
<?php if($Name == 'Hi' && $Pass != 'Google'){ ?>Welcome Hi!<?php }else{ ?>Loser!<?php } ?>

</body>
</html>