View Full Version : Two html pages and a php driver

04-19-2004, 05:48 PM

First post, searched a bit but didn't know how to phrase it, so I came up empty.

I have two files and a php driver page. I want to combine the two files with the driver page. Both files are html.

Basically what I am doing is using one html file as a template and the other page is used as the content. Depending on the variable passed in the link, the corresponding content page will be served as the content.

I also have another scenario where I would just like to have a template page with some php code in the content section that grabs another html page and puts that information into that section.

All of these file reside on the same server.

04-19-2004, 06:30 PM
This is what I came up with. This snippet works, but, I'm sure there's a lot of overhead here, especially when I start adding more content and the file sizes start getting larger. Please advise.


<b> Hello this content management system works! </b>


<title>This is a template test</title>


<font color = 'blue'>Yes it works quite well</font>



$template = "template.html";
$str = implode('', file( $template)) ;

$content1 = fopen("content.html", "r");
$info1 = '';
while (!feof($content1))
$info1 .= fgets($content1);

$content2 = fopen("content2.html", "r");
$info2 = '';
while (!feof($content2))
$info2 .= fgets($content2);

$chunk = explode("{content}" , $str);

$chunk2 = explode("{content2}", $chunk[1]);

print $chunk[0];
print $info1;
print $chunk2[0];
print $info2;
print $chunk2[1];


to see the results, go to www.extremausa.com/test/driver.php

04-19-2004, 07:32 PM
Glad you got it working, but I think there should be a better way to do this...

For your current simple page an include would do;


<?php include("content.html"); ?>
<hr /><hr />
<?php include("content2.html"); ?>

04-19-2004, 09:24 PM
Yes that works well, and I did not think of that simple solution!

I figured out another solution that still lets you have a template, content, and driver page. This is a lot simpler than what I had.

The next thing that I am going to work on is putting this into a function so you can feed it an array of filenames with their corresponding keys.


$str = implode('', file("template.html"));

$content1str = implode('', file("content.html"));

$content2str = implode('', file("content2.html"));

$str = str_replace("{content}" , $content1str, $str);
$str = str_replace("{content2}" , $content2str, $str);

print $str;


04-19-2004, 09:45 PM
Completed function looks like this. It's overloaded to take both files that will be converted to string variables in the function and it will also take plain string variables. Enjoy!


function templatedriver($templatefilename, $vararray){

$str = implode('', file($templatefilename));
// Changes template to string

foreach($vararray as $block=>$content) {

$contentstr = implode('', file($content));
// Changes file content to string
else if(is_string($content)){
$contentstr = $content;
else {
die("This is not compatible with the templatedriver function.... yet!");

$str = str_replace($block, $contentstr, $str);
} // end foreach

return $str;
}// This function returns a string with the replaced content

print templatedriver("template.html",array(
'{content}' => 'content.html',
'{content2}' => 'Ahah! this is overloaded!')

// template.html should be replaced by your template,
* In the above example, the bracketed part is what will be replaced
* in your template page. The filename or variable is what will replace it.
* You can have as many blocks of content that you want, this only
* works on files that reside on your server. Offsite files may or may
* not work depending on your and their configuration.


04-19-2004, 11:43 PM
hmm, still seems a bit lengthly... Includes would be fine however the other methods would work... include is much faster though.

Another tip, you can use ob_start(); and other output buffers to store includes as variables rather than having them added to the page, but for what you want... Includes should be fine, as this is also the fastest solution.

04-19-2004, 11:57 PM
I wouldn't use the "die()" to abort the template driver for unknown variable content. If you have 100 template variables and one of them is corrupted/wrong, the user might prefer to see the most of the file with only one part missing than looking at a very verbose error message that doesn't tell him anyhing. Templates aren't that crucial IMHO that it would justify to halt the execution completely upon an error.

Better: Use trigger_error() to throw a PHP error. These errors can be logged and tailored to suit your needs.