Her er et forslag:
template.class.php
<?php
class Template {
//SETTINGS
protected $File; //CONTAIN TEMPLATE FILE
protected $Values = array(); //CONTAIN TAGS
//CONSTRUCTOR
public function __construct($File)
{
$this->File = $File;
}
//SET KEYS IN TEMPLATE
public function Set($Key, $Value)
{
$this->Values[$Key] = $Value;
}
//OUTPUT THE TEMPLATE
public function Output()
{
//IF THE FILE DONT EXIST
if (!file_exists($this->File)) {
return "Error loading template file ($this->File).<br />";
}
//LOAD TEMPLATE FILE FROM CLASS
$Output = file_get_contents($this->File);
//REPLACE ALL THE TAGS IN THE TEMPLATE
foreach ($this->Values as $Key => $Value)
{
$TagToReplace = "[@$Key]";
$Output = str_replace($TagToReplace, $Value, $Output);
}
return $Output;
}
}
?>
example.php
$Side = new Template("Fil".$Data['Template'].".tpl");
$Side->Set("Noget",$Data['Tekst']);
echo $Side->Output();
Lav så fx. 3 filer kald dem fx. Fil1.tpl, Fil2.tpl osv..
Fil1.tpl
<div class="Container"><div class="Style">@Noget</div></div>
@Noget bliver så erstattet af $Data['Tekst'] som er en del af noget fra databasen...
dvs. $Data er dataen fra databasen!