The Final Code
Now the templating script is finished. I can store the code in a separate file and reference it when needed.
<?php
class Page
{
var $page;
function Page($template = "template.html") {
if (file_exists($template))
$this->page = join("", file($template));
else
die("Template file $template not found.");
}
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? $this->parse($data) : $data;
$this->page = eregi_replace("{" . $tag . "}", $data,
$this->page);
}
else
die("No tags designated for replacement.");
}
function output() {
echo $this->page;
}
}
?>
After initializing the object with the name of the HTML template file, pass an array of place holders found within the template and the data files or information to the replace_tags method and use the output method to flush the processed page from the class to standard output.
<?php
require_once("lib/template.php");
$page = new Page("template.html");
$page->replace_tags(array(
"title" => "HOME",
"descript" => "Welcome to my website!",
"main" => "dat/index.dat",
"menu" => "dat/menu.dat",
"left" => "dat/submenu.dat",
"right" => "dat/right.dat",
"footer" => "dat/footer.php"
));
$page->output();
?>
Source:
http://codewalkers.com/tutorials/58/8.html