Use the Replace function, that is, the str_replace function of PHP, to replace the keywords in the content read in the template file with the content in the variable, thereby achieving simple template separation.
Template file template.htm:
%body%
php file:
//The Replace function is used to replace the keywords in the content read from the template file with the content in the variable
function Replace($row)
{
//Define the variable used for replacement
$title = "Article title ";
$body = "Here is the body of the article";
//Replace the keywords in the parameters
$row = str_replace("%title%", $title, $row);
$row = str_replace("%body %", $body, $row);
//Return the replaced result
return $row;
}
//Template file pointer
$f_tem = fopen("template.htm","r");
/ /Generated file pointer
$f_new = fopen("new.htm","w");
//Loop through the template file, reading one line at a time
while(!feof($f_tem))
{
$ row = fgets($f_tem);
$row = Replace($row); //Replace the keywords in the read content
fwrite($f_new, $row); //Write the replaced content into the generated HTML File
}
//Close the file pointer
fclose($f_new);
fclose($f_tem);
?>
Generate a new html page: new.html
Here is the body of the article