• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

[PHP/MYSQL] Template Engine

sileo

New member
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

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
 
nettes gimmik ...

wobei ich so 1, 2 Sachen hätt:

frage:
warum kein oop und eine ordentliche Klasse daraus gemacht mit Exceptionhandling ?

ich pers. mag es ja gut leiden wenn ein error_reporting(E_ALL) am Anfang zu finden ist,

'SELECT * FROM Elements

-- php-faq --
16.14. Warum soll ich nicht SELECT * schreiben?
http://www.php-faq.de/q/q-sql-select.html

if (!$_GET['id']) {
return 'no id given';
}
else {
$queryAppend = ' WHERE `id` = ' . $_GET['id'];
}

unnecessary else


WHERE `id` = ' . $_GET['id'];

eine Superglobale im SQL Statement ohne vorher zu prüfen/sichern
( da könnt man im weitesten Sinne an SQLInjection denken )

das wars schon eventuell kannst den ein oder anderen Tipp gebrauchen


mfg
 
Zurück
Oben