Posted here a set of files to make a simple OOP template system to work. Unfortunately it doesn't correctly - contents do not display in correct positions when variable values are set through functions. Please help:
Here is the html template (template.html):
<html>
<head>
<title></title>
</head>
<body>
<center>
<table><tr><td>
<h1>LOGO</h1>
</td></tr>
<tr><td>
{MENU}
</td></tr>
<tr><td>
{TEXT}
</td></tr>
<tr><td>
{IMG}
</td></tr></table>
</center>
</body>
</html>
Here is the class (templateclass.php)
<?php # HtmlTemplate2.class
// This class reads in a template, sets the different values, and sends it to the browser.
class HtmlTemplate2 {
// Set the attributes.
var $template;
var $html;
var $parameters = array();
function HtmlTemplate2 ($template) { // This function sets which template will be used.
$this->template = $template;
$this->html = implode ("", (file($this->template))); // Read the template into an array, then create a string.
}
function SetParameter ($variable, $value) { // This function sets the particular values.
$this->parameters[$variable] = $value;
}
function menu (){
echo '<center>| <a href=page.php>Home</a> | About Us | Contact Us |</center>';
}
function img(){
echo '<img src="images/check.gif">';
}
function CreatePage () { // This function does the bulk of the work.
foreach ($this->parameters as $key => $value) { // Loop through all the parameters and set the variables to values.
$template_name = '{' . $key . '}';
$this->html = str_replace ($template_name, $value, $this->html);
}
echo $this->html;
}
}
?>
Here is the page to send content to browser (page.php):
<?php
require_once "templateclass.php"; // Include the class.
$page = new HtmlTemplate2 ("template.html");
$page->SetParameter ("MENU", $page->menu());//set value
$page->SetParameter ("TEXT", "This is a test");//set value
$page->SetParameter ("IMG", $page->img());//set value
$page->CreatePage(); // Send the page to the browser.
?>
Test them here. The first link shows what you get with above codes. Note the menu and image (check) do not display where they should be as shown in the second link.
http://www.waaha.net/oop/page.php
http://www.waaha.net/oop/page0.html