JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* template.php: tem_get to tem_get_rows, _tem_get to tem_get.
authorJosh Grams <josh@qualdan.com>
Mon, 3 Aug 2009 10:47:18 +0000 (06:47 -0400)
committerJosh Grams <josh@qualdan.com>
Mon, 3 Aug 2009 10:47:18 +0000 (06:47 -0400)
template.php

index 9ccce87..5b7e631 100644 (file)
@@ -97,8 +97,8 @@ function fill_template($data, $template, $context = NULL, $keychain = NULL) {
                else {
                        if($piece['pieces']) {  # sub-template
                                $keychain[] = $piece['name'];
-                               $data = tem_get($piece, $context, $keychain);
-                               foreach(template_rows($data) as $key => $row) {
+                               $rows = tem_get_rows($piece, $context, $keychain);
+                               foreach($rows as $key => $row) {
                                        $keychain[] = $key;
                                        $output .= fill_template($row, $piece, $context, $keychain);
                                        array_pop($keychain);
@@ -145,7 +145,7 @@ function merge_templates($main, $tem) {
 #
 function tem_auto_sep($piece, $context, $keychain) {
        list($name, $index, $this_name) = array_slice($keychain, -3);
-       $array = _tem_get($name, $context);
+       $array = tem_get($name, $context);
        if($index != count($array)-1) return true;
 }
 
@@ -153,7 +153,7 @@ function tem_auto_sep($piece, $context, $keychain) {
 # is false.  We check only for false; 0 or '' will not work.
 
 function tem_auto_once($piece, $context, $keychain) {
-       $value = _tem_get(array_pop($keychain), $context);
+       $value = tem_get(array_pop($keychain), $context);
        if($value !== false) return true;
 }
 
@@ -161,7 +161,7 @@ function tem_auto_once($piece, $context, $keychain) {
 # alternates between 'even' and 'odd'.
 
 function tem_auto_evenodd($piece, $context, $keychain) {
-       $rows = _tem_get(array_pop($keychain), $context);
+       $rows = tem_get(array_pop($keychain), $context);
        $even = 0;
        $text = array('even', 'odd');
        foreach($rows as $key => $value) {
@@ -182,7 +182,7 @@ function tem_auto_evenodd($piece, $context, $keychain) {
 
 # Convert value to array of hashes for use in sub-template expansion.
 # This adds flexibility to how you represent your data.
-function template_rows($value) {
+function tem_value_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;
@@ -197,27 +197,32 @@ function template_rows($value) {
        }
 }
 
-function _tem_get($key, $context) {
+# Search the current context and return the value for key.
+function tem_get($key, $context) {
        while($context) {
                $data = array_pop($context);
                if(array_key_exists($key, $data)) return $data[$key];
        }
 }
 
-function tem_get($piece, $context, $keychain)
+# 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, $keychain)
 {
-       if(count($piece['args'])) {
-               $func = "tem_auto_" . $piece['args'][0];
-               if(function_exists($func)) return $func($piece, $context, $keychain);
+       if(count($tag['args'])) {
+               $func = "tem_auto_" . $tag['args'][0];
+               if(function_exists($func)) $value = $func($tag, $context, $keychain);
                else die("ERROR: template auto function '$func' not found.<br>\n");
-       } else return _tem_get($piece['name'], $context);
+       } else $value = tem_get($tag['name'], $context);
+
+       return tem_value_as_rows($value);
 }
 
-# $tag is a hash with keys 'name' and 'args'.
+# Return the value for a tag as an encoded string.
 function tem_get_enc($tag, $context)
 {
        $key = $tag['name'];
-       $value = _tem_get($key, $context);
+       $value = tem_get($key, $context);
        foreach($tag['args'] as $encoding) {
                $func = "enc_$encoding";
                if(function_exists($func)) $value = $func($value, $key);