-# Then we do a depth-first traversal of the template tree,
-# replacing all tags with the data values.
-
-function fill_template($data, $template, $context = NULL, $keychain = NULL) {
- $context[] = $data;
- foreach($template['pieces'] as $piece) {
- if(is_string($piece)) $output .= $piece;
- else {
- if($piece['pieces']) { # sub-template
- $keychain[] = $piece['name'];
- $rows = tem_get_rows($piece, $context, $keychain);
- foreach($rows as $key => $row) {
- $keychain[] = $key;
- $output .= fill_template($row, $piece, $context, $keychain);
- array_pop($keychain);
- }
- array_pop($keychain);
- } else { # variable
- $output .= tem_get_enc($piece, $context);
+# To fill out a template, we do a depth-first traversal of the template
+# tree, replacing all tags with the data values.
+
+# The data starts out as a nested set of key/value pairs, where the
+# values can be:
+
+ # a string to fill a value slot
+ # a hash to fill one instance of a sub-template
+ # an array of hashes to fill multiple instances of a sub-template
+
+# The middle form will be converted to the last form as we use it.
+
+function tem_data_as_rows($value) {
+ if(is_array($value)) {
+ # numeric keys, is already array of arrays -- expand sub-template for each.
+ if(array_key_exists(0, $value)) return $value;
+ # key/value pairs -- expand sub-template once.
+ else return array($value);
+ } elseif($value) {
+ # value -- expand sub-template once using only parent values
+ return array(array());
+ } else {
+ # empty value -- don't expand sub-template
+ return array();
+ }
+}
+
+
+
+# We use dynamic scoping, so we keep a stack of namespaces.
+# Each scope references a hash and the parent scope.
+
+function &tem_scope(&$data, $push = true) {
+ static $stack = array();
+
+ if($push) {
+ $parent =& last($stack);
+ $stack[] = array();
+
+ $scope =& last($stack);
+ $scope['data'] =& $data;
+ if($parent) $scope['parent'] =& $parent;
+ } else { # pop
+ array_pop($stack);
+ }
+ return last($stack);
+}
+
+function &tem_end_scope() { return tem_scope($x, false); }
+
+# To look up a key, we check each namespace (starting with the
+# innermost one) until a value is found.
+
+function tem_find_scope($key, $context) {
+ $scope = $context;
+ do{
+ if(array_key_exists($key, $scope['data'])) {
+ return $scope;
+ }
+ } while($scope = $scope['parent']);
+
+ # not found; return empty scope.
+ return array('parent' => $context);
+}
+
+function tem_get($key, $context) {
+ $scope = tem_find_scope($key, $context);
+ if($scope) return $scope['data'][$key];
+}
+
+# Return the value for a tag as a set of rows to fill a sub-template.
+# If $tag has an arg, call the tem_auto function to munge the data.
+function &tem_get_rows($tag, $context)
+{
+ $key = $tag['name'];
+ if(count($tag['args'])) {
+ $func = "tem_auto_" . $tag['args'][0];
+ function_exists($func)
+ or die("ERROR: template auto function '$func' not found.<br>\n");
+ }
+ $scope = tem_find_scope($key, $context);
+
+ if($func) $value = $func($key, $scope);
+ else $value = $scope['data'][$key];
+
+ $rows = tem_data_as_rows($value);
+ if(is_array($value)) $scope['data'][$key] = $rows;
+
+ return $rows;
+}
+
+# Return the value for a tag as an encoded string.
+function tem_get_enc($tag, $context)
+{
+ $key = $tag['name'];
+ $value = tem_get($key, $context);
+ foreach($tag['args'] as $encoding) {
+ $func = "enc_$encoding";
+ if(function_exists($func)) $value = $func($value, $key);
+ else die("ERROR: encoder function '$func' not found.<br>\n");
+ }
+ return $value;
+}
+
+function fill_template($template, &$data, &$context = NULL) {
+ if(!$context) $context =& tem_scope($data);
+ foreach($template['pieces'] as $tem) {
+ if(is_string($tem)) $output .= $tem;
+ elseif($tem['pieces']) { # sub-template
+ $rows =& tem_get_rows($tem, $context);
+ foreach($rows as $key => &$row) {
+ $context =& tem_scope($row);
+ $context['rows'] =& $rows;
+ $output .= fill_template($tem, $row, $context);
+ $context =& tem_end_scope();
+