X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=template.php;h=45857afcecde5532be9c7651ac7a7bbb552bf29a;hb=0c70f6a8cdf46b61c8d30f918833b8f2d8522009;hp=1667cdead3e1c59287a0f422311b3c5b748e406e;hpb=dfd0f158f89020b3142e41127d21e48c8512aa1e;p=wfpl.git diff --git a/template.php b/template.php index 1667cde..45857af 100644 --- a/template.php +++ b/template.php @@ -57,15 +57,14 @@ function parse_template_file($filename) { return parse_template(file_get_contents($filename)); } -# We parse the template string into a tree of strings and sub-templates. +# We parse the template string into a tree of strings and sub-templates. # A template is a hash with a name string, a pieces array, and possibly # an args array. function parse_template($string) { $tem =& tem_push(); $tem['pieces'] = array(); - # note: for some reason this captures ''. - $matches = preg_split("/()/", $string, -1, PREG_SPLIT_DELIM_CAPTURE); + $matches = preg_split('/()/', preg_replace('//', '$1', $string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach($matches as $match) { if($match == '~~') $match = '~'; if(substr($match,0,1) == '~' and strlen($match) > 2) { @@ -85,13 +84,13 @@ function parse_template($string) { array_pop($args); # drop '}' $cur = $tem['name']; if($name && $name != $cur) { - die("Invalid template: tried to close '$name', but '$cur' is current."); + die("Invalid template: tried to close '$name', but '$cur' is current."); } $tem =& $tem['parent']; } else { # value slot $tem['pieces'][] = array('name' => $name, 'args' => $args); } - } elseif($match and $match != ' # # row content... -#
+#
# # # function tem_auto_sep(&$value, $key, $context) { $rows =& $context['parent']['parent']; if($rows['cur'] != count($rows['rows'])-1) # last row? - return $value = true; # show once + return $value = true; # show once +} + +# auto-show once, only when this is the first row of the parent +function tem_auto_last(&$value, $key, $context) { + $rows =& $context['parent']['parent']; + if($rows['cur'] == count($rows['rows'])-1) # last row? + return $value = true; # show once +} + +# auto-show once, only when this is the last row of the parent +function tem_auto_first(&$value, $key, $context) { + $rows =& $context['parent']['parent']; + if($rows['cur'] == 0) # first row? + return $value = true; # show once } # 'show' sections will be shown unless the corresponding data value -# is false. We check only for false; 0 or '' will not work. +# is false (only false, not 0 or '' or array()). function tem_auto_show(&$value) { if($value !== false) $value = array(array()); @@ -301,8 +314,8 @@ function tem_auto_show(&$value) { # alternates between 'even' and 'odd'. function tem_auto_evenodd(&$values) { - $even = false; - foreach($values as &$value) { + $even = true; + if($values) foreach($values as &$value) { $value['evenodd'] = $even ? 'even' : 'odd'; $even = !$even; } @@ -359,7 +372,7 @@ class tem { function tem() { $this->template = array('pieces' => array()); $this->data = array(); - } + } function set($key, $value) { $this->data[$key] = $value;