Hier meine:
<?php
class tpl {
var $templates = array();
var $templatevars = array();
var $templatebitvars = array();
var $templatefolder;
var $templatefile;
/* Klasse erstellen */
function tpl($templatefolder="templates/" {
$this->templatefolder = $templatefolder;
}
/* template als Variable */
function get($templatename, $templatefile) {
$this->$templatefile = $this->templatefolder.$templatefile.".tpl";
if(file_exists($this->$templatefile)) {
$this->templates[$templatename]=implode("",file($this->$templatefile));
} else {
//Sucht Template im Standart-Template-Odner
$this->$templatefile = "templates/".$templatefile.".tpl";
if(file_exists($this->$templatefile)) {
$this->templates[$templatename]=implode("",file($this->$templatefile));
} else {
$this->templates[$templatename]="Template "".$templatename."" (".$this->$templatefile." doesnt exist";
}
}
return $this->templates[$templatename];
}
/* template als Variable BIT */
function getbit($templatename, $templatefile) {
$this->$templatefile = $this->templatefolder.$templatefile.".tpl";
if(file_exists($this->$templatefile)) {
$this->templates[$templatename].=implode("",file($this->$templatefile));
//Variablen werden sofort ersetzt
reset ($this->templatebitvars);
while (list($key, $value) = each($this->templatebitvars)) {
$this->templates[$templatename] = str_replace($key ,$value, $this->templates[$templatename]);
}
} else {
//Sucht Template im Standart-Template-Odner
$this->$templatefile = "templates/".$templatefile.".tpl";
if(file_exists($this->$templatefile)) {
$this->templates[$templatename].=implode("",file($this->$templatefile));
//Variablen werden sofort ersetzt
reset ($this->templatebitvars);
while (list($key, $value) = each($this->templatebitvars)) {
$this->templates[$templatename] = str_replace($key ,$value, $this->templates[$templatename]);
}
} else {
$this->templates[$templatename]="Template "".$templatename."" (".$this->$templatefile." doesnt exist";
}
}
return $this->templates[$templatename];
}
/* Gibt template aus */
function output($template) {
$template = $this->parse($template);
print($template);
}
/* Sprachvariablen in Variablen für Template */
function set_lang($key, $value) {
$this->templatevars["{lang_".strtolower($key)."}"] = $value;
}
/* Variablen in Variablen für Template */
function set_var($key, $value="" {
$this->templatevars["{var_".$key."}"] = $value;
}
/* Bit Variablen in Variablen für Template (Bei Schleifen) */
function set_bit($key, $value="" {
$this->templatebitvars["{bit_".$key."}"] = $value;
}
/* Array in Variablen für Template */
function set_vars($array) {
foreach($array as $key => $value) {
$this->templatevars["{var_".$key."}"] = $value;
}
}
/* Array in Array für Template */
function set_arr($arrayname, $array) {
foreach($array as $key => $value) {
if(is_array($value)) {
$arrayname .= ":".$key;
$this->set_arr($arrayname, $value);
} else {
$arrayname .= ":".$key;
$this->templatevars["{arr_".$arrayname."}"] = $value;
}
}
}
/* Template parsen */
function parse ($template) {
//Subtemplates parsen
reset ($this->templates);
while (list($key, $value) = each($this->templates)) {
$template = str_replace('{tpl_' . $key . '}',$value, $template);
}
//Varibalen parsen + Sprachvariablen parsen
reset ($this->templatevars);
while (list($key, $value) = each($this->templatevars)) {
$template = str_replace($key ,$value, $template);
}
// Fix for German "Umlaute"
$template = str_replace('ä', 'ä', $template);
$template = str_replace('ö', 'ö', $template);
$template = str_replace('ü', 'ü', $template);
$template = str_replace('Ä', 'Ä', $template);
$template = str_replace('Ö', 'Ö', $template);
$template = str_replace('Ü', 'Ü', $template);
$template = str_replace('ß', 'ß', $template);
//Sucht nicht verwendete tpl|lang|var|arr|bit Variablen und elemeniert sie
$suchmuster = "#{(tpl|lang|var|arr|bit)_(.*)}#U";
$template = preg_replace($suchmuster, '', $template);
return $template;
}
}
?>