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);
#
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;
}
# 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;
}
# 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) {
# 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;
}
}
-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);