-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.

4
  • 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 Commented Oct 11, 2012 at 2: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. Commented Oct 11, 2012 at 2: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. Commented Oct 11, 2012 at 2:31
  • possible duplicate of Modify an Existing PHP Function to Return a String Commented Oct 11, 2012 at 3:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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!
on an average traffic site generating on the fly every time really should not cause any load issues.
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;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.