Moin!
Ganz einfach: Ziel bei meiner Lösung war auch gleichseitig den Code sowohl in HTML als auch in PHP lesbar zu halten. Wenn man dann sowohl HTML als auch PHP einrücken will gibts Probleme.
Dann wäre es an der Zeit über ein Template-Sytem nachzudenken, das heißt die HTML-Seite (Darstellung) vom PHP-Quellode (Logik) zu trennen. Ich habe mir für diesen "Job" eine ziemlich schlanke Lösung gebastelt:
<?php
## des fastix kleine 'Template-Engine'
define('HTML_DEBUG',false);
## read an print the template
function Print_Template($template_file, $template_data=array()) {
@$template_size=filesize($template_file);
if (! $template_size) {
$template_data['<strong>Error (fatal)</strong>']='<strong>File "<em>'.$template_file.'"</em> not exists or have size 0!<strong>';
$template='<html><head><title>Testing the Output...</title><meta http-equiv=\'Content-Type\' content=\'text/html; charset=\'ISO-8859-15\' /><style type=\'text/css\'>th {background-color:#ccf;padding:0.2em} td {padding:0.2em}</style></head><body><table border=\'1\'><tr><th colspan=\'2\'>Values of $template_data</th></tr><tr><th>Key</th><th>Value</th></tr>';
$counter=0;
foreach (array_keys($template_data) as $key) {
$line='l'.$counter++;
$template.='<tr id="'.$line.'" onmouseover=\'document.getElementById("'.$line.'").style.backgroundColor="#ffc"\' onmouseout=\'document.getElementById("'.$line.'").style.backgroundColor="#fff"\'>
<td>'.$key.'</td><td>'.$template_data[$key].'</td></tr>';
}
$template.='</table></body></html>';
} else {
$FH = fopen ($template_file, "r") or die ('fatal: can not read '.$template_file);
$template = fread ($FH, $template_size);
fclose ($FH);
if (! HTML_DEBUG) {
$search=array("\n", "\r", "\t");
$template = str_replace ($search, '', $template);
}
foreach (array_keys($template_data) as $key) {
$search = '[[tpl id=\''.$key.'\']]';
#die('str_replace ('.$search.', '.$template_data[$key].', $template);)');
$template = str_replace ($search, $template_data[$key], $template);
$search = '[[tpl id="'.$key.'"]]';
$template = str_replace ($search, $template_data[$key], $template);
$search = '[[tpl id='.$key.']]';
$template = str_replace ($search, $template_data[$key], $template);
}
}
print $template;
}
## functions:
function Encoding_This($str, $str_encoding_to='ISO-8859-15', $str_encoding_from='ISO-8859-15') {
if ($str_encoding_to != $str_encoding_from) {
return mb_convert_encoding($str, $str_encoding_to, $str_encoding_from);
} else {
return $str;
}
}
function Print_Template_HTML_Header ($encoding_to='ISO-8859-15') {
header('Content-Type: text/html; charset='.$encoding_to);
}
?>
In Template.html sieht es dann so aus:
<html>
<head>
....
</head>
<body>
<h1>[[tpl id='Seitentitel']]</h1>
<p>Heute werden [[tpl id="Bemerkung"]] [[tpl id=MaxGradZahl]] °C.</p>
...
</body>
</html>
Im PHP-Code muss
- die template.php mit include './includes/template.php' geladen werden.
- ein hasch existieren, der die Schlüssel und Daten beinhaltet. z.B...
$tpl['Seitentitel']='Wie warm wird es heute?';
$tpl['MaxGradZahl']=3*4;
if ($tpl['MaxGradZahl'] < 12) {
$tpl['Bemerkung']='gerade mal';
} else {
$tpl['Bemerkung']='immerhin';
}
...
- Die Funktion aufgerufen werden:
Print_Template('./templates/Template.html', $tpl)
Die Funktionen Encoding_This() und Print_Template_HTML_Header() haben hier keine Bedeutungen werden aber manchmal benötigt...
Ein solches template-System ist die beste Art deine sauberen Einrückungen zu bewirken. Mit define('HTML_DEBUG',true);
in der template.php kannst Du diese im resultieren HTML-Quelltext auch sichtbar machen.
MFFG (Mit freundlich- friedfertigem Grinsen)
fastix®
Als Freiberufler bin ich immer auf der Suche nach Aufträgen: Schulungen, Seminare, Training, Development