Search code examples
phphtmlreplacefile-get-contentsfwrite

replacing file contents in php and saving to a new file


So I would have a template file called template.php and inside, it would have:

<html>
some other content
<?php $name ?>
</html>

Essentially, I want to pull the name from the database and insert it into $name and save that entire page as a new file.

So let say in the database, I had a name called Joe, the new file I create would have the following content inside:

<html>
some other content
Joe
</html>

The only issue I am having is finding the right functions to actually replace the variable from the template file, and being able to save it into a new file.


Solution

  • That's not how templates usually work. You should have a template file, substitute variables for their values, and display it to the user. You can cache these generated templates (the ones that are always the same) but you never make a new template file for each user and save it permanently. That makes managing and updating the application way more difficult than you want.

    I recommend using a simple template system like RainTPL. That's what I use for most of my projects... it's very simple to use and provides all the basic functionality you would need.

    You can also use just PHP for this... there are a few answers on SO that cover this so I won't get into it.