I understand what you mean by "in templates", but what you are doing does not make much sense - e.g. creating html files on the fly... Can you show us some code and elaborate on use case/goal? i.e. the logic is the opposite - you have static template files created, then you populate content dynamically. That's why they are templates...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs