X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=template.php;h=6aba82b4b6d1139f0f1757d1f3ed9d1a16ba1518;hb=c4575213df3077d1b9956dd12132f13ba7567970;hp=3eef030cd3bb36599c70a1a4b0c0b5ee081f1fa4;hpb=14216a566d093c076e2d36628dda780abfbcbcea;p=wfpl.git
diff --git a/template.php b/template.php
index 3eef030..6aba82b 100644
--- a/template.php
+++ b/template.php
@@ -67,23 +67,22 @@ function parse_template($string) {
# note: for some reason this captures ''.
$matches = preg_split("/()/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($matches as $match) {
- if(substr($match,0,1) == '~') {
+ if($match == '~~') $match = '~';
+ if(substr($match,0,1) == '~' and strlen($match) > 2) {
$args = explode(' ', substr($match,1,-1));
if(count($args) == 1 and $args[0] == '}') $name = '';
else $name = array_shift($args);
if(last($args) == '{') { # open block
- array_pop($args);
- # create a new sub-template
- # and add it to the parent.
- $tem =& tem_push($tem);
- $tem['parent']['pieces'][] =& $tem;
+ array_pop($args); # drop '{'
+ $tem =& tem_push($tem); # create a new sub-template
+ $tem['parent']['pieces'][] =& $tem; # as a piece of the parent
$tem['name'] = $name;
$tem['pieces'] = array();
$tem['args'] = $args;
} elseif(last($args) == '}') { # close block
- array_pop($args);
+ array_pop($args); # drop '}'
$cur = $tem['name'];
if($name && $name != $cur) {
die("Invalid template: tried to close '$name', but '$cur' is current.");
@@ -100,27 +99,23 @@ function parse_template($string) {
}
function fill_template($template, &$data, &$context = NULL) {
- if(!$context) {
- $context =& tem_push($context);
- $context['data'] =& $data;
- }
+ $context =& tem_push($context);
+ $context['data'] =& $data;
foreach($template['pieces'] as $tem) {
if(is_string($tem)) $output .= $tem;
elseif($tem['pieces']) { # sub-template
- $rows =& tem_get_rows($tem, $context);
+ $rows =& tem_row_data($tem, $context);
+ $context['rows'] =& $rows;
foreach($rows as $key => &$row) {
- $context =& tem_push($context);
- $context['data'] =& $row;
- $context['rows'] =& $rows;
- $output .= fill_template($tem, $row, $context);
- $context =& $context['parent'];
-
+ $context['cur'] = $key;
+ $output .= fill_template($tem, $row, $context);
}
} else { # variable
- $output .= tem_get_enc($tem, $context);
+ $output .= tem_encoded_data($tem, $context);
}
}
+ $context =& $context['parent'];
return $output;
}
@@ -181,7 +176,9 @@ function tem_data_as_rows($value) {
# 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) {
+function tem_data_scope($key, $context) {
+ static $refs = array();
+
$scope = $context;
do{
if(array_key_exists($key, $scope['data'])) {
@@ -190,27 +187,31 @@ function tem_find_scope($key, $context) {
} while($scope = $scope['parent']);
# not found; return empty scope.
- return array('parent' => $context);
+ $refs[] = array();
+ $ret = array();
+ $ret['parent'] =& $context;
+ $ret['data'] =& last($refs);
+ return $ret;
}
-function tem_find_value($key, $context) {
- $scope = tem_find_scope($key, $context);
+function tem_get_data($key, $context) {
+ $scope = tem_data_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)
+function &tem_row_data($tem, $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.
\n");
- }
- $scope = tem_find_scope($key, $context);
+ $key = $tem['name'];
+ $scope = tem_data_scope($key, $context);
- if($func) $value = $func($key, $scope);
+ if(count($tem['args'])) {
+ $auto_func = "tem_auto_" . $tem['args'][0];
+ function_exists($auto_func)
+ or die("ERROR: template auto function '$auto_func' not found.
\n");
+ }
+ if($auto_func) $value = $auto_func($scope['data'][$key], $key, $scope);
else $value = $scope['data'][$key];
$rows = tem_data_as_rows($value);
@@ -220,10 +221,10 @@ function &tem_get_rows($tag, $context)
}
# Return the value for a tag as an encoded string.
-function tem_get_enc($tag, $context)
+function tem_encoded_data($tag, $context)
{
$key = $tag['name'];
- $value = tem_find_value($key, $context);
+ $value = tem_get_data($key, $context);
foreach($tag['args'] as $encoding) {
$func = "enc_$encoding";
if(function_exists($func)) $value = $func($value, $key);
@@ -233,12 +234,16 @@ function tem_get_enc($tag, $context)
}
+function is_sub_template($piece) {
+ return is_array($piece) and $piece['pieces'];
+}
# Return a hash containing the top-level sub-templates of tem.
-function top_sub_templates($tem) {
+function top_sub_templates($tem, $is_sub = 'is_sub_template') {
+ function_exists($is_sub) or die("no such function '$is_sub'.");
$subs = array();
foreach($tem['pieces'] as $piece) {
- if(is_array($piece) and $piece['pieces']) {
+ if($is_sub($piece)) {
$subs[$piece['name']] = $piece;
}
}
@@ -252,7 +257,8 @@ function merge_templates($main, $tem) {
$subs = top_sub_templates($tem);
foreach($main['pieces'] as $piece) {
- if(is_array($piece) and !$piece['pieces'] and $subs[$piece['name']]) {
+ $sub = $subs[$piece['name']];
+ if(is_array($piece) and !$piece['pieces'] and $sub and $sub['args'][0] != 'hide') {
$piece = $subs[$piece['name']];
}
$out['pieces'][] = $piece;
@@ -270,39 +276,249 @@ function merge_templates($main, $tem) {
# 'sep' (separator) sections will be shown for all but the last parent row.
# Sample usage:
-#
-#
+#
+#
# row content...
-#