-1

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Kevin Jung
  • 2,973
  • 7
  • 32
  • 35
  • 1
    that's generally not how template files work, you don't create new html files, you dynamically populate the template in accordance with some criteria –  Oct 11 '12 at 02:28
  • I am only doing it this way so that i can dynamically generate static pages for each user. I guess the examples might be misleading. – Kevin Jung Oct 11 '12 at 02:29
  • 2
    but you usually wouldn't want to do that. It makes maintenance and updates much easier, if something needs to change you have to edit each file as opposed to just one. –  Oct 11 '12 at 02:31
  • possible duplicate of [Modify an Existing PHP Function to Return a String](http://stackoverflow.com/questions/8730847/modify-an-existing-php-function-to-return-a-string) – hakre Oct 11 '12 at 03:12

2 Answers2

1

Look at the answer I gave to this question a while back. There I explain how "variable parsing" usually works.

When I create a template loader I generally use output buffering with php include. This allows you to run the page as a php file without displaying its content before you are ready. The advantage to "parsing" your php files this way is that you can still run loops and functions.

Here's an example of how to use output buffering with PHP to create what you're wanting.

The Template template.php

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

Where your database code and stuff is index.php

<?php

$name = 'John Doe';

if( /* some sort of caching system */ ) {
  $parsed_template = file_get_contents('parsed_template.html');
}else {
  ob_start();
  include 'template.php';
  $parsed_template = ob_get_clean();

  file_put_contents('parsed_template.html', $parsed_template);
}

echo $parsed_template;
?>
Community
  • 1
  • 1
Baylor Rae'
  • 3,995
  • 20
  • 39
1

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.

Community
  • 1
  • 1
sachleen
  • 30,730
  • 8
  • 78
  • 73
  • the catching system really makes sense, having tons of static pages like you said would not only take up space but make it extremely difficult to manage. thanks for the answer! – Kevin Jung Oct 11 '12 at 02:43
  • on an average traffic site generating on the fly every time really should not cause any load issues. –  Oct 11 '12 at 02:48