simple templating in PHP


simple templating in PHP

19.12.2001, 16:05 · Martin · Permalink

Ok. This NOT rocket-science. It is NOT very cool or effective. It simply works and is nice and short code. Templating done the quick way. I just though I'd share this cade with the world, as it is so easy to do ;)
function parsetemplate ($templatestr, $infohash) {
	// replace placeholders like {foo}
	// in templatestr with 
	// corresponding values from infohash
	function lookup ($s, $h) {
		if ($h{$s}) {
			return $h{$s};
		} else {
			return "<!-- unknown: $s -->";
		}
	}
	$result = preg_replace (
		"/{(w+)}/e",
		"lookup('$1', $infohash)",
		$templatestr
		);
	return $result;
}

ähnliche Einträge:


alles Bild, Text und Tonmaterial ist © Martin Spernau, Verwendung und Reproduktion erfordert die Zustimmung des Authors

Martin Spernau
© 1994-2026


amazon.de Wunschliste

Facebook me!
Google+

powered by Traumtank