X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=template.php;h=3845c7c9bf9ec61b1b28829bfc30f4053f2d7035;hb=e99321a886b30fc6d601c7065384bfe2a6d34bc4;hp=1667cdead3e1c59287a0f422311b3c5b748e406e;hpb=dfd0f158f89020b3142e41127d21e48c8512aa1e;p=wfpl.git diff --git a/template.php b/template.php index 1667cde..3845c7c 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 +329,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 +387,7 @@ class tem { function tem() { $this->template = array('pieces' => array()); $this->data = array(); - } + } function set($key, $value) { $this->data[$key] = $value; @@ -429,7 +457,14 @@ class tem { # merge the data arrays and template trees (new style) $this->data = array_merge($this->data, $tem->data); - $this->template = merge_templates($this->template, $tem->template); + merge_templates($this->template, $tem->template); + } + + # see merge() above + function merge_file($filename) { + $other_tem = new tem(); + $other_tem->load($filename); + $this->merge($other_tem); } function top_sub_names($is_sub = 'is_sub_template') { @@ -497,6 +532,16 @@ function tem_load($filename) { $GLOBALS['wfpl_template']->load($filename); } +function tem_merge($tem) { + tem_init(); + $GLOBALS['wfpl_template']->merge($tem); +} + +function tem_merge_file($filename) { + tem_init(); + $GLOBALS['wfpl_template']->merge_file($filename); +} + function tem_output($filename = false) { tem_init(); $GLOBALS['wfpl_template']->output($filename);