Template Based Scripts - A short script that will help you to seperate the HTML from PHP codes. Tested and well commented.
Compatibility: PHP 3.0, PHP 4.0
>>>>> Copy the codes below to a .txt file and name it 'template.php' <?php // Begin Template Based Scripts // == This Script Free To Use Providing This Notice Remains == // == This Script Has Been Found In The http://snapbuilder.com Free Public Codes Library == // == NOTICE: Though This Material May Have Been In A Public Depository, Certain Author Copyright Restrictions May Apply == class classtemplate { var $filename; var $contents; function getFileTemplate($filename = "") { // This function loads the entire template file to $this->contents variable if(file_exists($filename)) { // check if the template file exists $handle = fopen($filename, "rb"); $this->contents = fread($handle, filesize($filename)); fclose($handle); $this->filename = $filename; $returnValue = TRUE; } else { $returnValue = FALSE; } return $returnValue; }// End of function getFileTemplate function setPattern($template = "", $ArrVariable = array()) { // this function replace the pattern or keywords in a template // $template = the template with keywords or pattern to be replaced // $ArrVariable[$ArrVariableKeyName]= the replace value //$ArrVariableKeyName= pattern or the keyword to be replaced // Ex. //$ArrVariable['{HELLO_MARKER}'] = "HELLO WORLD"; // $template= "<TABLE><TR><TD>{HELLO_MARKER}</TD></TR></TABLE>"; // $this->setPattern($template, $ArrVariable); // the output will be: // <TABLE><TR><TD>HELLO WORLD</TD></TR></TABLE> $returnValue = "";// initialize $returnValue Variable $tmpVariableName= array_keys($ArrVariable);// Get Pattern or the keyword to be replaced to an Array $tmpVariableValue= array_values($ArrVariable);// Get Replace Value to an Array // replace the pattern/keyword from the template and replace it with values $returnValue = str_replace($tmpVariableName, $tmpVariableValue, $template); return $returnValue;// Return the proccesed template } // End of setPattern function getPattern($marker ="") { $returnValue= ""; $ArrCodeLines = explode("\n", $this->contents);// break it into array of lines $CntCodeLines= count($ArrCodeLines);// get the number of code lines $KeyCodeLines= 0;// Array Keys of code lines $isCaptureIsOn= FALSE; for($KeyCodeLines=0; $KeyCodeLines<$CntCodeLines; $KeyCodeLines++) {// Start of for($KeyCodeLines=0; $KeyCodeLines<$CntCodeLines; $KeyCodeLines++) if(preg_match('#<!-- '.$marker.' START -->#', $ArrCodeLines[$KeyCodeLines], $ArrYanks)) { //$returnValue.= $ArrCodeLines[$KeyCodeLines]."\n"; $isCaptureIsOn = TRUE; } elseif(preg_match('#<!-- '.$marker.' END -->#', $ArrCodeLines[$KeyCodeLines], $ArrYanks)) { //$returnValue.= $ArrCodeLines[$KeyCodeLines]."\n"; $isCaptureIsOn = FALSE; } else { if($isCaptureIsOn) { $returnValue.= $ArrCodeLines[$KeyCodeLines]."\n"; } } }// End of for($KeyCodeLines=0; $KeyCodeLines<$CntCodeLines; $KeyCodeLines++) return $returnValue; }// End of getPattern }// end class /// sample ?> >>>>> End template.php page file above >>>>> Copy the codes below to a .txt file and name it 'index.php' <?php include ("./template.php"); $clsTemplate = new classTEMPLATE; $clsTemplate->getFileTemplate("./templates/temp.index.php"); $HEADpattern = $clsTemplate->getPattern("HEADER"); $ROWpattern = $clsTemplate->getPattern("ROW"); $COLpattern = $clsTemplate->getPattern("COL"); $ArrVariableH['{HEAD1}'] = "header 1"; $ArrVariableH['{HEAD2}'] = "header 2"; $HEADvalue = $clsTemplate->setPattern($HEADpattern, $ArrVariableH); $ROWvalue = ""; for($y=0; $y<10; $y++){ $tmpCOLvalue = ""; for($x=0; $x<10; $x++){ $ArrVariableC['{DATA}'] = "$y,$x "; // replace the pattern with value // ex. // if the template is HELLO // and the pattern is LL // and the replace value is x // if you loop it with (.=) 4 times // it will look like this // $tmpCOLvalue = HExxxxO $tmpCOLvalue .= $clsTemplate->setPattern($COLpattern, $ArrVariableC); } $ArrVarTmpCOL[$COLpattern] = $tmpCOLvalue; $tmpROWvalue .= $clsTemplate->setPattern($ROWpattern, $ArrVarTmpCOL); } $ArrVarTmpROW[$ROWpattern] = $tmpROWvalue; $ROLvalue .= $clsTemplate->setPattern($ROWpattern, $ArrVarTmpROW); $ArrVariable[$ROWpattern] = $ROLvalue; $ArrVariable[$HEADpattern] = $HEADvalue; print $clsTemplate->setPattern($clsTemplate->contents, $ArrVariable); ?> >>>>> End index.php page file above >>>>> Copy the codes below to a .txt file and name it 'temp.index.php' and place it in a folder named 'templates' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <body style="{font-family:Arial;}"> <!-- BODY START --> <!-- HEADER START --> <b>{HEAD1}</b> <p>{HEAD2}</p> <!-- HEADER END --> <TABLE cellpadding="0" cellspacing="0" border="1" width="550"> <!-- ROW START --> <TR> <!-- COL START --> <TD>{DATA}</TD> <!-- COL END --> </TR> <!-- ROW END --> </TABLE> <!-- BODY END --> </body> </html>