[phplib] Conditional blocks From: Smith, David (Student Assistant) (David.Smith <email protected>)
Date: 07/20/00

I really like your implementation of templates in PHP. Incredibly useful.
Unfortunately, I felt it was missing a feature - conditional blocks.

I have added a conditional block to your template.inc file. I thought you
may like to include this in the official version. It works like this...

in template file:
<!-- IFDEF con -->
This stuff is conditional
<!-- ENDIF con -->

in the code:
$t->set_var("con", "x"); //the value can be anything solong as it becomes a
var

then the IFDEF statement will be evaluated when the block is parsed.

Dave Smith
dave.s <email protected>

  function parse($target, $handle, $append = false) {
    if (!is_array($handle)) {
      $str = $this->subst($handle);
      if ($append) {
        $this->set_var($target, $this->get_var($target) . $str);
      } else {
        $this->set_var($target, $str);
      }
>> $this->dodefine($target);
    } else {
      reset($handle);
      while(list($i, $h) = each($handle)) {
        $str = $this->subst($h);
        $this->set_var($target, $str);
      }
    }
    return $str;
  }

> /* public: dodefine()
> * Will sort out <!-- IFDEF x --> ... <!-- ENDIF x --> stuff
> */
> function dodefine($parent) {
> if (!$this->loadfile($parent)) {
> $this->halt("dodefine: unable to load $parent.");
> return false;
> }
>
> $str = $this->get_var($parent);
> $reg = "/<!--\s+IFDEF (\w*)\s+-->/sm";
> $x = preg_match_all($reg, $str, $m);
> $z = 0;
> while ($z < $x) {
> $reg = "/<!--\s+IFDEF ".$m[1][$z]."\s+-->\s*\n(.*)\n".
> "\s*<!--\s+ENDIF ".$m[1][$z]."\s+-->/sm";
> if (!($this->get_var($m[1][$z])))
> $str = preg_replace($reg, "", $str);
> else {
> if (preg_match_all($reg, $str, $n) != 0){
> $str = preg_replace($reg, $n[1][0], $str);
> }
> }
> $z++;
> }
> $this->set_var($parent, $str);
> }

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>