Code: Alles auswählen
////////TEMPLATE_SYSTEM/////////
function template($name) {
$path = $_SERVER['DOCUMENT_ROOT'].'/templates/'.trim($name);
if(file_exists($path)) {
if(trim(file_get_contents($path)) != '') {
$cachepath = $_SERVER['DOCUMENT_ROOT'].'/cache/'.trim($name).".php";
if(!file_exists($cachepath) || filemtime($path)>filemtime($cachepath)) {
$handle = fopen($path, 'r');
$data = fillVars(fread($handle, filesize($path)));
fclose($handle);
$cachehandle = fopen($cachepath, 'w');
fwrite($cachehandle,$data);
fclose($cachehandle);
}
return $cachepath;
} else {
echo 'Das Template '.$name.' ist leer!';
die();
}
} else {
echo 'Das Template '.$name.' wurde nicht gefunden!';
die();
}
}
function fillVars($data) {
$data = str_replace('<?php ','<? ',$data);
$data = preg_replace('/([^\\\])\{\$([\w.]*)\}/U',
'\1<?php echo $\2; ?>',
$data); //normale Variablen
$data = preg_replace('/([^\\\])\{\+([\w.]*)\}/U',
'\1<?php include $\2; ?>',
$data); //normale Variablen
$data = preg_loop('/([^\\\])\{\?([\w.]*)[\s<](.*)([^\\\])\?\}/Us',
'\1<?php if ( $\2 ) {?>\3\4<?php }?>',
$data); //nur wenn Variable gesetzt
$data = preg_loop('/([^\\\])\{!([\w.]*)[\s<](.*)([^\\\])!\}/Us',
'\1<?php if (!( $\2 )) {?>\3\4<?php }?>',
$data); //nur wenn Variable NICHT gesetzt
$data = preg_loop('/([^\\\])\{§([\w.]*)(\[(\w*)\])?>(\w*)(\s|<)(.*)([^\\\])§\}/Us',
'\1<?php if ( $\2 ) {if (!is_array($\2))$templatetemp =array($\2); else $templatetemp= $\2; foreach($templatetemp as $\4=> $\5) {?>\6\7\8<?php }} ?>',
$data); //foreach mit und ohne Key
$data = preg_loop('/<\?php(.*)\$=>(.*)\?>/Us',
'<?php\1\2?>',
$data); //Ergänzung für foreach ohne Key
$data = preg_loop('/<\?php\s([^<]*)\$([^\s;)]+)\.([^\s.;[\-)]*)([[\-][^\s;)]*)?([\s;)<])(.*)\?>/U',
'<?php \1(is_object($\2) ? $\2->\3\4 : (is_array($\2) ? $\2["\3"]\4 : false))\5\6?>',
$data); //Arrays und Objekte
$data = preg_loop('/<\?php\s([^<]*)\s\$([^\s;.=\-)]*)(\s|;)(.*)\?>/U',
'<?php \1(isset($\2) ?$\2: false)\3\4?>',
$data); //Probe, ob Variablen existieren
return $data;
}