newwarrior
Erfahrenes Mitglied
Hallo,
ich habe eine kleine Templateklasse geschrieben für meine eigene Projekte. Ja ich weiß es gibt sehr gute andere Projekte, doch sind diese mir meistens zu groß bzw. einfach nicht mein Ding und ich lerne ja auch noch dabei.
Also wie folgt sieht sie aus:
Aktuell arbeite ich noch ein Cache Verfahren ein.
Was kann ich noch verbessern?
Hatte überlegt noch mit Blöcken zu arbeiten, weiß aber noch nicht so wirklich wie ich das am besten machen soll.
Danke für die Hilfe.
NW
ich habe eine kleine Templateklasse geschrieben für meine eigene Projekte. Ja ich weiß es gibt sehr gute andere Projekte, doch sind diese mir meistens zu groß bzw. einfach nicht mein Ding und ich lerne ja auch noch dabei.
Also wie folgt sieht sie aus:
PHP:
<?php
error_reporting(E_ALL);
class template {
/*
Variables that be use in that class
and their content
*/
private $templateDIR = 'templates/';
private $templateName = '';
private $templateFile = '';
private $template = '';
private $leftborderF = '{';
private $rightborderF = '}';
private $leftborderVAR = '{$';
private $rightborderVAR = '$}';
public function __construct($tpl_dir = '', $lang_dir = '')
{
if(!empty($tpl_dir))
$this->templateDIR = $tpl_dir;
if(!empty($lang_dir))
$this->languageDIR = $lang_dir;
}
public function load($file)
{
$this->templateName = $file;
$this->templateFile = $this->templateDIR.$file;
if( !empty($this->templateFile) ) {
if( file_exists($this->templateFile) ) {
$this->template = file_get_contents($this->templateFile);
} else {
return false;
}
} else {
return false;
}
$this->parseTemplate();
}
public function assign($replace, $replacement)
{
$this->template = str_replace($this->leftborderVAR.$replace.$this->rightborderVAR, $replacement, $this->template);
}
private function parseTemplate() {
while( preg_match( "/" .$this->leftborderF ."include file=\"(.*)\.(.*)\""
.$this->rightborderF ."/isUe", $this->template) )
{
$this->template = preg_replace( "/" .$this->leftborderF ."include file=\"(.*)\.(.*)\""
.$this->rightborderF."/isUe",
"file_get_contents(\$this->templateDIR.'\\1'.'.'.'\\2')",
$this->template );
}
}
public function display() {
echo $this->template;
}
}
?>
Aktuell arbeite ich noch ein Cache Verfahren ein.
Was kann ich noch verbessern?
Hatte überlegt noch mit Blöcken zu arbeiten, weiß aber noch nicht so wirklich wie ich das am besten machen soll.
Danke für die Hilfe.
NW