In PHP I have the need to generate HTML pages from a template and insert variables in specific places then save them to disk.
So far this code works well:
<?php
$client = "John doe";
$template = "
<html>
<body>
<div>
$client
</div>
</body>
</html>
";
$htmlCode = $template;
$fh = fopen("page.html", 'w') or die("can't open file");
fwrite($fh, $htmlCode);
fclose($fh);
?>
ultimately I want to slurp and read in the $template from a file on disk, using something like:
$template = file_get_contents("template.html");
But using this approach I can not get the $client replaced with the "John doe" instead it shows as $client in the HTML code when I save it to disk.
I did read some posts about using regex and printf to replace substings, but i could not see how can I use them in my scenario. I am positive there must be easier and better way.
There will be a need of a lot of variables plopped in many places so I do not want to create a regex for each one. I just want to be able to stick a $variable anywhere in the external html file and PHP should replace it with a variable content (if it exist). Unfortunately file_get_contents() only slurps it into a string exactly as it is without any interpretations. I was wandering if there is another function that I can use for slurping that will actually work as I indented.
Any suggestions are greatly appreciated.