tpl_file_array as $this->tpl_file) { if(file_exists($this->tpl_file)){ $fp = fopen($this->tpl_file,'rb'); while(!feof($fp)){ $chunk = fgets($fp); $this->printLine($chunk); } } else{ $this->error("No such template file. Make sure file exists. File: $this->tpl_file"); } } } /** * Prints out an error * * @param string $error_msg */ function error($error_msg){ echo $error_msg; } /** * Returns the number of variables that needs to be inserted into the template file * * @return integer */ function numOfVarsToInsert(){ return count($this->vars_to_insert); } /** * Returns number of left delimiters in one line * * @param string $line * @return integer */ function numOfDlmsInOneLine($line){ return substr_count($line,$this->dlm_left); } /** * Calculates the total number of delimiters in the template file * * @return integer */ function numOfDlms(){ $fp = fopen($this->tpl_file,'rb'); $num = 0; while (!feof($fp)){ $chunk = fgets($fp); $num += substr_count($chunk,$this->dlm_left); } fclose($fp); return $num; } /** * Prints out each line, and if needed places the variable in the variable place * * @param string $line */ function printLine($line){ $num_of_dlms_in_one_line = $this->numOfDlmsInOneLine($line); if($num_of_dlms_in_one_line>0){ for($i=0;$i<$num_of_dlms_in_one_line;$i++){ $from = strpos($line,$this->dlm_left); $to = strpos($line,$this->dlm_right); $replace = substr($line,$from,$to-$from+strlen($this->dlm_left)); $line = str_replace($replace,$this->vars_to_insert[$this->num_of_inserted_vars],$line); $this->num_of_inserted_vars++; } } echo $line; } } ?>