<?php
# minimal templating support.
# the function below will read a file that contains HTML
# and some special markup that is processed by the function.
# The file is a <em>template</em>, most of it is printed without
# change, but any occurrance of "<=foo>" is replaced with the
# value of $hash['foo'].
#
# For example, if the template looks like this:
#
# <h3 style="color:<=clr>">Hello, <=name></h3>
#
# and the parameter hash is an array such that:
# $hash['clr'] = "red"
# $hash['name'] = "Joe Smith"
#
# the function with do the replacements and produce (print)
# this:
#
# <h3 style="color:red">Hello, Joe Smith</h3>
#
# If something like <=foo> is found in the template and there # no value for $hash['foo'], the output will replace <=foo> with nothing
# (it will be removed - the browser won't see it).
function include_with_replacement ($filename, $hash = array()) {
global $hfield;
# read in the file
if ($f = fopen($filename,'r')) {
$str = fread($f,filesize($filename));
fclose($f);
}
foreach( array_keys($hash) as $token) {
$str = preg_replace("/<=$token>/",$hash["$token"],$str);
}
# remove any lingering tokens
$str = preg_replace("/<=[a-zA-Z]+>/","",$str);
echo $str;
}
?>