X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=blobdiff_plain;f=template.php;h=fa0affbe3268535879395d2c8bf2402049784542;hp=44db726bf9eb75949d05fb385c41e19f62d5e1b2;hb=9a3136a5bee66e1055ffb566373952b6054dd7bf;hpb=f22aa9e82ee43e5d390bcb5ab076b24a93e8fda2 diff --git a/template.php b/template.php index 44db726..fa0affb 100644 --- a/template.php +++ b/template.php @@ -37,9 +37,9 @@ # tem_auto_* function to munge the data, automating certain common use # cases. See the comments on the tem_auto functions for more details. -require_once('code/wfpl/encode.php'); -require_once('code/wfpl/file.php'); -require_once('code/wfpl/misc.php'); +require_once(__DIR__.'/'.'encode.php'); +require_once(__DIR__.'/'.'file.php'); +require_once(__DIR__.'/'.'misc.php'); # Top-Level Functions @@ -53,19 +53,18 @@ function template_file($filename, $data) { return fill_template(parse_template_file($filename), $data); } -function parse_template_file($filename) { +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) { +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) { @@ -74,24 +73,24 @@ function parse_template($string) { if(count($args) == 1 and $args[0] == '}') $name = ''; else $name = array_shift($args); - if(last($args) == '{') { # open block + if(count($args) && $args[count($args)-1] == '{') { # open block 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 + } elseif(count($args) && $args[count($args)-1] == '}') { # close block 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 != '