X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=template.php;h=3845c7c9bf9ec61b1b28829bfc30f4053f2d7035;hb=e99321a886b30fc6d601c7065384bfe2a6d34bc4;hp=45857afcecde5532be9c7651ac7a7bbb552bf29a;hpb=ece89d7b32c6eb58bf3704ea360b9b292c551c7f;p=wfpl.git diff --git a/template.php b/template.php index 45857af..3845c7c 100644 --- a/template.php +++ b/template.php @@ -233,10 +233,14 @@ function tem_encoded_data($tag, $context) } -function is_sub_template($piece) { +function is_sub_template(&$piece) { return is_array($piece) and $piece['pieces']; } +function is_value_slot(&$piece) { + return is_array($piece) and !$piece['pieces']; +} + # Return a hash containing the top-level sub-templates of tem. function top_sub_templates($tem, $is_sub = 'is_sub_template') { function_exists($is_sub) or die("no such function '$is_sub'."); @@ -249,22 +253,33 @@ function top_sub_templates($tem, $is_sub = 'is_sub_template') { return $subs; } -# Replace top-level values in $main with top-level templates from $tem. -function merge_templates($main, $tem) { - $out = array('name' => $main['name'], 'pieces' => array()); - - $subs = top_sub_templates($tem); - - foreach($main['pieces'] as $piece) { - $sub = $subs[$piece['name']]; - if(is_array($piece) and !$piece['pieces'] and $sub and $sub['args'][0] != 'hide') { - $piece = $subs[$piece['name']]; +# merge $subs (sub_templates) into variables in $main (template) +function merge_sub_templates(&$main, &$subs) { + foreach($main['pieces'] as &$piece) { + if(is_array($piece)) { # not just text + if($piece['pieces']) { + # a sub-template in main + merge_sub_templates($piece, $subs); + } else { + # a value-slot in main + $sub = $subs[$piece['name']]; + if($sub and $sub['args'][0] != 'hide') { + $piece = $subs[$piece['name']]; + $piece['parent'] =& $main; + } + } } - $out['pieces'][] = $piece; } return $out; } +# Replace values in $main with top-level templates from $tem. +function merge_templates(&$main, &$tem) { + $subs = top_sub_templates($tem); + + merge_sub_templates($main, $subs); +} + # tem_auto functions @@ -442,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') { @@ -510,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);