Einzelnen Beitrag anzeigen
Alt 28.10.2008, 09:54  
KarlEgon
Erfahrener Benutzer
 
Registriert seit: 24.10.2008
Beiträge: 291
PHP-Kenntnisse:
Fortgeschritten
KarlEgon befindet sich auf einem aufstrebenden Ast
Standard

Mhhh das Ding kann ja schon ganz schön viel, aber ich denke das ist mir zu überladen.

Im moment sieht meine Template Klasse so aus:
PHP-Code:
<?php
class Template {

    private 
$templateDir "templates";
    private 
$mainTemplate "main.tpl";
    private 
$leftDelimiter '{$';
    private 
$rightDelimiter '}';
    private 
$leftComments '\{\*';
    private 
$rightComments '\*\}';
    private 
$vars = array ();
    private 
$arrays = array ();
    private 
$sections = array();
    private 
$multiArrays = array();
    private 
$hideAreas = array();
    private 
$template;
    private 
$templatePath;

    public function 
__construct($templateDir ""$mainTemplate "") {
        if (!empty (
$templateDir) && $templateDir != "") {
            
$this->templateDir $templateDir;
        }
        if (!empty (
$mainTemplate) && $mainTemplate != "") {
            
$this->mainTemplate $mainTemplate;
        }
    }

    public function 
setTemplateDir($templateDir) {
        
$this->templateDir $templateDir;
    }

    public function 
setMainTemplate($mainTemplate) {
        
$this->mainTemplate $mainTemplate;
    }

    public function 
setLeftDelimiter($leftDelimiter) {
        
$this->leftDelimiter $leftDelimiter;
    }

    public function 
setRightDelimiter($rightDelimiter) {
        
$this->rightDelimiter $rightDelimiter;
    }

    public function 
assign($key$value) {
        if (
is_array($value)) {
            
$this->addArray($key$value);
        } else {
            
$this->vars[$key] = $value;
        }
    }

    private function 
addArray($key$value) {
        if(
$this->isDeepMultiArray($value)) {
            
$this->addMultiArray($key,$value);
        } else {
            
$this->arrays[$key] = $value;
        }
    }

    public function 
addTemplate($file$section) {
        if(!@
is_array($this->sections[$section])) {
            
$this->sections[$section]=array();
        }
        
array_push($this->sections[$section],$file);
    }

    private function 
addMultiArray($key,$value) {
        
$this->multiArrays[$key] = $value;
    }

    public function 
hideArea($key) {
        
array_push($this->hideAreas,$key);
    }

    public function 
display() {
        
$this->templatePath $this->templateDir "/" $this->mainTemplate;
        if (!empty (
$this->templatePath)) {
            if (
$fp fopen($this->templatePath"r")) {
                
// Den Inhalt des Templates einlesen
                
$this->template fread($fpfilesize($this->templatePath));
                
fclose($fp);
            } else {
                return 
false;
            }
        }
        
$this->replaceIncludes();
        
$this->replaceSections();
        
$this->replaceIncludes();
        
$this->replaceMultiArrays();
        
//$this->replaceMultiArrays2();
        
$this->replaceValues();
        
$this->replaceArrays();
        
$this->replaceComments();
        
$this->hideAreas();
        
$this->removePHP();
        echo 
$this->template;
    }

    private function 
replaceIncludes() {
        
$pattern '/\{include\sfile=[\"\'](.*)\.(.*)[\"\']\}/isUe';
        while (
preg_match($pattern$this->template)) {
            
$this->template preg_replace($pattern"file_get_contents(\$this->templateDir.'/\\1'.'.'.'\\2')"$this->template);
        }
    }

    private function 
replaceValues() {
        foreach (
$this->vars as $key => $value) {
            
$this->template str_replace($this->leftDelimiter $key $this->rightDelimiter$value$this->template);
        }
    }

    private function 
replaceArrays() {
        foreach (
$this->arrays as $key => $value) {
            foreach (
$value as $valKey => $valValue) {
                
$pattern '/^(\{multiply\sname=[\"\'](.*)[\"\']\})'.$this->leftDelimiter.$key.'\.'.$valKey.$this->rightDelimiter.'^(\{\/multiply\})/is';
                if(
preg_match($pattern,$this->template,$subpattern)) {
                    
//debugprint($subpattern);
                
}
                
$this->template str_replace($this->leftDelimiter $key "." $valKey $this->rightDelimiter$valValue$this->template);
            }
        }
    }

    private function 
replaceMultiArrays($sub="",$pos=0) {
        foreach(
$this->multiArrays as $key => $value) {
            if(
$sub=="") {
                
$pattern '/\{multiply\sname=[\"\']'.$key.'[\"\']\}(.*)\{\/multiply}/isUe';
            } else {
                
$pattern '/\{multiply\sname=[\"\']'.$sub.'[\"\']\}(.*)\{\/multiply}/isUe';
            }
            if(
preg_match($pattern,$this->template,$subpattern,null,$pos)) {
                
//debugprint($subpattern,false,$sub);
                
$out="";
                
//ausgabe($sub." ".count($value));
                
foreach($value as $valKey => $valValue) {
                        
//ausgabe($valKey."---".$valValue);
                        
$output=$subpattern[1];
                        foreach(
$valValue as $valvalKey => $valvalValue) {
                            if(!
is_array($valvalValue)) {
                                
$output str_replace($this->leftDelimiter.$key.'.'.$valvalKey.$this->rightDelimiter,$valvalValue,$output);
                                
//$output = str_replace($this->leftDelimiter.$key.'.'.$valvalKey.$this->rightDelimiter,$subpattern[1],$output);
                            
}
                        }
                        
$out .= $output;
                }
                
//ausgabe(trim(htmlspecialchars($out)));
                //ausgabe($subpattern[0]);
                
$this->template str_replace($subpattern[0],$out,$this->template);
            }
        }
        
$pattern '/\{multiply\sname=[\"\'](.*)[\"\']\}(.*)\{\/multiply}/isUe';
        
$this->template preg_replace($pattern,"",$this->template);
    }

    private function 
replaceMultiArrays2() {
        
$pattern_begin '/\{multiply\sname\=[\"\'](.*)[\"\']\}/isU';
        
$pattern_end '/\{\/multiply\}/isU';
        
$start=0;
        
$index=1;
        
$Begins=array();
        
$error=false;
        while(
preg_match($pattern_begin,$this->template,$subpatternPREG_OFFSET_CAPTURE,$start)) {
            
$start=$subpattern[0][1]+strlen("{multiply name='".$subpattern[1][0]."'}");
            
array_push($Begins,array("type"=>"begin",
                                     
"name"=>$subpattern[1][0],
                                     
"index"=>$index,
                                     
"pos"=>$subpattern[0][1]));
            
$index++;
        }
        
$countBegins count($Begins);
        
$start=0;
        
$index=$Begins[count($Begins)-1]['index'];
        
$Ends=array();
        
$belegt=array();
        while(
preg_match($pattern_end,$this->template,$subpatternPREG_OFFSET_CAPTURE,$start)) {
            
$start=$subpattern[0][1]+strlen("{/multiply}");
            while(
$Begins[$index-1]['pos']>$subpattern[0][1] AND $index>1) {
                
$index--;
            }
            while(
in_array($index,$belegt)) {
                
$index--;
            }
            if(
$index<=0) {
                
$error=true;
            }
            
array_push($Ends,array("type"=>"end",
                                   
"index"=>$index,
                                   
"pos"=>$subpattern[0][1]));
            
array_push($belegt,$index);
            
$index=$Begins[count($Begins)-1]['index'];
        }
        
$countEnds count($Ends);
        if(
$countEnds!=$countBegins || $error) {
            throw new 
Exception("Template Fehlerhaft");
        }
        
usort($Begins,array(&$this,"compareMultiArrayPos"));
        
//debugprint($Begins);
        //usort($Ends,array(&$this,"compareMultiArrayPos"));
        
foreach($Begins as $value) {
            if(
$value['name']!='infos') {
                
$this->replaceMultiArrays4($value['name'],$value['pos']);
            }
            
//ausgabe($value['name']."---".$value['pos']);
        
}
        
//debugprint($Begins);
        //debugprint($Ends);
        
$All array_merge($Begins,$Ends);
        
usort($All,array(&$this,"compareMultiArrayPos"));
        
//debugprint($All);
        //debugprint($this->multiArrays);
        /*for($i=0;$i<count($All)-1;$i++) {
            if($All[$i]['type']=='begin' AND $All[$i+1]['type']=='end') {
                //$this->replaceMultiArrays3($this->multiArrays[$All[$i]['name']],$All[$i]['pos'],$All[$i]['name']);
            }
            if($All[$i]['type']=='begin' AND $All[$i+1]['type']=='begin') {
                $this->replaceMultiArrays3($this->multiArrays[$All[$i]['name']],$All[$i]['pos'],$All[$i+1]['name'],true);
                $i++;
            }
        }*/
    
}

    private function 
hideAreas() {
        foreach(
$this->hideAreas as $area) {
            
$pattern '/\{area\sname=[\"\']'.$area.'[\"\']\}(.*)\{\/area}/isUe';
            while(
preg_match($pattern,$this->template)) {
                
$this->template preg_replace($pattern,"",$this->template);
            }
        }
        
$pattern '/\{area\sname=[\"\'](.*)[\"\']\}/isUe';
        
$this->template preg_replace($pattern,"",$this->template);
        
$pattern '/\{\/area}/isUe';
        
$this->template preg_replace($pattern,"",$this->template);
    }

    private function 
replaceSections() {
        foreach(
$this->sections as $key => $value) {
            
//ausgabe($key."---".$value);
            
$output="";
            foreach(
$value as $file) {
                
$output .= "{include file='".$file."'}";
            }
            
$pattern "/\{section name=\"$key\"\}/";
            
$this->template preg_replace($pattern,$output,$this->template);
        }
        
$pattern '/\{section\sname=[\"\'](.*)[\"\']\}/isUe';
        while(
preg_match($pattern,$this->template)) {
            
$this->template preg_replace($pattern,"",$this->template);
        }
    }

    private function 
replaceComments() {
        
$pattern "/".$this->leftComments."(.*)".$this->rightComments."/isUe";
        while(
preg_match($pattern,$this->template)) {
            
$this->template preg_replace($pattern,"",$this->template);
        }
    }

    private function 
removePHP() {
        
$pattern '/\<\?[ph]{0,3}(.*)[ph]{0,3}\?\>/isUe';
        while(
preg_match($pattern,$this->template)) {
            
$this->template preg_replace($pattern,"",$this->template);
        }
    }

    private function 
compareMultiArrayIndex($a,$b) {
        if(
$a['index']==$b['index']) {
            return 
0;
        }
        return (
$a['index']<$b['index']) ? : -1;
    }

    private function 
compareMultiArrayPos($a,$b) {
        if(
$a['pos']==$b['pos']) {
            return 
0;
        }
        return (
$a['pos']<$b['pos']) ? -1;
    }

    private function 
isDeepMultiArray($multiarray$level 2) {  // default is simple multiarray
    
if (is_array($multiarray)) {  // confirms array
        
if ($level == 1) {  // $level reaches 1 after specified # of recursions
            
return true;  // returns true to recursive function conditional
        
}  // end conditional
        
foreach ($multiarray as $array) {  // goes one level deeper into array
            
if ($this->isDeepMultiArray($array$level 1)) {  // check subarray
                
return true;  // best if $message = true so function returns boolean
            
}  // end recursive function
        
}  // end loop
    
} else {  // not an array at specified level
    
return false;  // is also used recursively so can't change to message
    
}
}
}
?>
Habe viel herumprobiert, deswegen sieht es manchmal vielleicht ein bisschen chaotisch aus. Ich weiß nicht wie ich es realisieren soll, dass alles ersetzt wird so wie ich es brauche.
KarlEgon ist offline   Mit Zitat antworten