Falls jemand Zeit und Bock hat wär's 'ne edle Sache wenn ich etwas Feedback zu meinem Script haben könnte. Leider habe ich mir nie angewöhnt Kommentare in den Code einzufügen, aber ich
Funktioniert so weit ganz gut...
Vielen Dank für eure Kommentare!
Marco
PHP:
<?php
$llimiter = '{';
$rlimiter = '}';
/*****
* output of the compiled template code
*
* @parameter: $file: template file to compile
* @return: true
*/
function compile($file) {
global $llimiter, $rlimiter;
foreach(file($file) as $line) {
echo preg_replace_callback('$' . $llimiter . '(.*?)' . $rlimiter . '$', 'fillElement', $line);
}
return true;
}
/*****
* replaces the tags with the according fields from the db
*
* @parameter: $element: name of the tag from the man template
* @return: array with the compiled code lines
*/
function fillElement($element) {
global $llimiter, $rlimiter;
//gets the query corresponding to the tag given by $element[1]
$result = mysql_query('SELECT * FROM Elements WHERE name = \'' . $element[1] . '\'') or die('error querying db');
$elementRecord = mysql_fetch_assoc($result);
mysql_free_result($result);
//checks the id for the main content part
if (strpos(strtolower($element[1]), 'main')) {
if (!$_GET['id']) {
return 'no id given';
}
else {
$queryAppend = ' WHERE `id` = ' . $_GET['id'];
}
}
//gets the records and replaces the tags
$result = mysql_query($elementRecord['query'].$queryAppend) or die('error querying db');
while ($placeholderRecord = mysql_fetch_assoc($result)) {
$placeholderRecords[] = $placeholderRecord;
}
foreach (file($elementRecord['name'] . '.tpl.html') as $key => $line) {
if (substr($line, 0, 1) == 'l') {
!$index ? $index = $key : true ;
foreach ($placeholderRecords as $key => $row) {
$lines[$index+$key][] = preg_replace('$(' . $llimiter . ')(.*?)(' . $rlimiter . ')$e', '$placeholderRecords[$key][strtolower(\2)]', substr($line, 1));
}
}
else {
$index = false;
$lines[] = preg_replace('$(' . $llimiter . ')(.*?)(' . $rlimiter . ')$e', '$placeholderRecords[$key][strtolower(\2)]', $line);
}
}
$lines = smartImplode($lines);
return $lines;
}
function smartImplode ( $arr1, $separator = '' ) {
return implode($separator, help($arr1, $asdf));
}
function help ( $arr2, &$help ) {
foreach ($arr2 as $value) {
if (is_array($value)) {
help($value, $help);
}
else {
$help[] = $value;
}
}
return $help;
}
?>
Funktioniert so weit ganz gut...
Vielen Dank für eure Kommentare!
Marco