Hallo!
Ich hab emir mal ein eigenes template System geschrieben!
Dazu hätte ich aber noch ein par fragen.
Hier erstmal der Source:
tpl.class.php
PHP-Code:
<?
class tpl
{
var $tpl;
var $fields = Array();
// initiate template
function tpl($filename, $tpldir = 'default/', $tplext = '.tpl')
{
// check if template file exists
if($tpldir != 'templates/') {
$tpldir = "templates/$tpldir";
}
if (!file_exists($tpldir . $filename . $tplext)) {
$this->tpl = "Fehler: Template '".$filename.$tplext."' existiert leider nicht!\n";
}
// if template file exists , put line breaks on every line in the html source
else {
$this->tpl = implode('', file($tpldir . $filename . $tplext));
}
}
function Add($name, $value) {
$this->fields["$name"] = $value;
}
function Out() {
foreach($this->fields as $name => $value) {
$this->tpl = str_replace("{".$name."}", $value, $this->tpl);
}
echo $this->tpl;
}
}
?>
So rufe ich das Template auf:
Code:
$tpl = new tpl('index_body');
$tpl->add('TEXT', 'Index.php');
$tpl->out();
Meine Fragen:
- Was kann man daran besser machen, bzw. was würdet ihr anders machen?
Wie kann ich die replacements in arrays packen, also dass ich das so schreiben kann:
Code:
$tpl->add('TEXT', 'Index.php',
'HALLO', 'halo');
- Wenn es nicht so kompliziert ist, möchte ich gerne die switch funktion aus dem phpbb board bei mir einfügen, also in den template dateien Inhalt wenn zutrifft
ich bedanke mich im vorraus!
?>[/php]