Project

General

Profile

« Previous | Next » 

Revision 522

Added by Matthias over 16 years ago

- Update codepress to Version 0.9.6
- Update phplib to Version 7.4a

View differences:

template.inc
5 5
 * (C) Copyright 1999-2000 NetUSE GmbH
6 6
 *                    Kristian Koehntopp
7 7
 *
8
 * $Id: template.inc,v 1.1.1.1 2005/01/30 10:32:06 rdjurovich Exp $
8
 * $Id: template.inc,v 1.15 2004/07/23 20:36:29 layne_weathers Exp $
9 9
 *
10 10
 */
11 11

  
......
32 32
 * loadfile would try to load a file if the varval had been set to "" (rha)
33 33
 * in get_undefined, only match non-whitespace in variable tags as in finish (Layne Weathers & rha)
34 34
 * more elegant fix to the problem of subst stripping '$n', '\n' and '\\' strings (rha)
35
 * parse uses get_var to obtain return value (Jordi via SF)
36
 * set_block reports an error if the block could not be extracted (rha)
37
 * filename is now windows-pathname aware (krabu @ SF)
35 38
 *
36 39
 *
37 40
 * Changes in functionality which go beyond bug fixes:
38 41
 *
42
 * added ability for filename comments to be output (from phplib-users layne)
39 43
 * changed debug handling so set, get and internals can be tracked separately (rha)
40 44
 * added debug statements throughout to track most function calls (rha)
41 45
 * debug output contained raw HTML -- is now escaped with htmlentities (rha)
......
85 89
  var $debug    = false;
86 90

  
87 91
 /**
92
  * Determines whether Template outputs filename comments.
93
  * false = no filename outputs
94
  * true = HTML comments (e.g. <!-- START FILE $filename -->) placed in output
95
  *
96
  * @var       int
97
  * @access    public
98
  */
99
  var $filename_comments = false;
100

  
101
 /**
102
  * Determines the regular expression used to find unknown variable tags.
103
  * "loose"  = traditional match all curly braces with no whitespace between
104
  * "strict" = adopts PHP's variable naming rules
105
  *              ("loose" has a nasty habit of deleting JavaScript RegEx components)
106
  *              (should future major version releases of PHPLib default this "strict"?)
107
  *
108
  * @var       string
109
  * @access    public
110
  */
111
  var $unknown_regexp = "loose";
112

  
113
 /**
88 114
  * The base directory from which template files are loaded.
89 115
  *
90 116
  * @var       string
......
194 220
  * @return    boolean
195 221
  */
196 222
  function set_root($root) {
223
    if(ereg('/$', $root)) {
224
      $root = substr($root, 0, -1);
225
    }
197 226
    if ($this->debug & 4) {
198 227
      echo "<p><b>set_root:</b> root = $root</p>\n";
199 228
    }
......
266 295
      $this->file[$varname] = $this->filename($filename);
267 296
    } else {
268 297
      reset($varname);
269
      while(list($v, $f) = each($varname)) {
298
      while (list($v, $f) = each($varname)) {
270 299
        if ($this->debug & 4) {
271 300
          echo "<p><b>set_file:</b> (with array) varname = $v, filename = $f</p>\n";
272 301
        }
......
316 345
    $str = $this->get_var($parent);
317 346
    $reg = "/[ \t]*<!--\s+BEGIN $varname\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $varname\s+-->\s*?\n?/sm";
318 347
    preg_match_all($reg, $str, $m);
319
    $str = preg_replace($reg, "{" . "$name}", $str);
348
    if (!isset($m[1][0])) {
349
      $this->halt("set_block: unable to set block $varname.");
350
      return false;
351
    }
352
    $str = preg_replace($reg, "{" . $name . "}", $str);
320 353
    $this->set_var($varname, $m[1][0]);
321 354
    $this->set_var($parent, $str);
322 355
    return true;
......
364 397
      }
365 398
    } else {
366 399
      reset($varname);
367
      while(list($k, $v) = each($varname)) {
400
      while (list($k, $v) = each($varname)) {
368 401
        if (!empty($k)) {
369 402
          if ($this->debug & 1) {
370 403
            printf("<b>set_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $k, htmlentities($v));
......
410 443
      }
411 444
    } else {
412 445
      reset($varname);
413
      while(list($k, $v) = each($varname)) {
446
      while (list($k, $v) = each($varname)) {
414 447
        if (!empty($v)) {
415 448
          if ($this->debug & 1) {
416 449
            printf("<b>clear_var:</b> (with array) <b>%s</b><br>\n", $v);
......
452 485
      }
453 486
    } else {
454 487
      reset($varname);
455
      while(list($k, $v) = each($varname)) {
488
      while (list($k, $v) = each($varname)) {
456 489
        if (!empty($v)) {
457 490
          if ($this->debug & 1) {
458 491
            printf("<b>unset_var:</b> (with array) <b>%s</b><br>\n", $v);
......
492 525

  
493 526
    // quote the replacement strings to prevent bogus stripping of special chars
494 527
    reset($this->varvals);
495
    while(list($k, $v) = each($this->varvals)) {
528
    while (list($k, $v) = each($this->varvals)) {
496 529
      $varvals_quoted[$k] = preg_replace(array('/\\\\/', '/\$/'), array('\\\\\\\\', '\\\\$'), $v);
497 530
    }
498 531

  
......
578 611
      }
579 612
    } else {
580 613
      reset($varname);
581
      while(list($i, $v) = each($varname)) {
614
      while (list($i, $v) = each($varname)) {
582 615
        if ($this->debug & 4) {
583 616
          echo "<p><b>parse:</b> (with array) target = $target, i = $i, varname = $v, append = $append</p>\n";
584 617
        }
......
594 627
    if ($this->debug & 4) {
595 628
      echo "<p><b>parse:</b> completed</p>\n";
596 629
    }
597
    return $str;
630
    return $this->get_var($target);
598 631
  }
599 632

  
600 633

  
......
644 677
      echo "<p><b>get_vars:</b> constructing array of vars...</p>\n";
645 678
    }
646 679
    reset($this->varkeys);
647
    while(list($k, $v) = each($this->varkeys)) {
680
    while (list($k, $v) = each($this->varkeys)) {
648 681
      $result[$k] = $this->get_var($k);
649 682
    }
650 683
    return $result;
......
682 715
      return $str;
683 716
    } else {
684 717
      reset($varname);
685
      while(list($k, $v) = each($varname)) {
718
      while (list($k, $v) = each($varname)) {
686 719
        if (isset($this->varvals[$v])) {
687 720
          $str = $this->varvals[$v];
688 721
        } else {
......
719 752
      return false;
720 753
    }
721 754

  
722
    preg_match_all("/{([^ \t\r\n}]+)}/", $this->get_var($varname), $m);
755
    preg_match_all(
756
        (("loose" == $this->unknown_regexp) ? "/{([^ \t\r\n}]+)}/" : "/{([_a-zA-Z]\\w+)}/"),
757
        $this->get_var($varname),
758
        $m);
723 759
    $m = $m[1];
724 760
    if (!is_array($m)) {
725 761
      return false;
726 762
    }
727 763

  
728 764
    reset($m);
729
    while(list($k, $v) = each($m)) {
765
    while (list($k, $v) = each($m)) {
730 766
      if (!isset($this->varkeys[$v])) {
731 767
        if ($this->debug & 4) {
732 768
         echo "<p><b>get_undefined:</b> undefined: $v</p>\n";
......
762 798
      break;
763 799

  
764 800
      case "remove":
765
        $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);
801
        $str = preg_replace(
802
            (("loose" == $this->unknown_regexp) ? "/{([^ \t\r\n}]+)}/" : "/{([_a-zA-Z]\\w+)}/"),
803
            "",
804
            $str);
766 805
      break;
767 806

  
768 807
      case "comment":
769
        $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template variable \\1 undefined -->", $str);
808
        $str = preg_replace(
809
             (("loose" == $this->unknown_regexp) ? "/{([^ \t\r\n}]+)}/" : "/{([_a-zA-Z]\\w+)}/"),
810
            "<!-- Template variable \\1 undefined -->",
811
            $str);
770 812
      break;
771 813
    }
772 814

  
......
829 871
    if ($this->debug & 4) {
830 872
      echo "<p><b>filename:</b> filename = $filename</p>\n";
831 873
    }
832
    if (substr($filename, 0, 1) != "/") {
874
    if (substr($filename, 0, 1) != "/" 
875
       && substr($filename, 0, 1) != "\\"
876
       && substr($filename, 1, 2) != ":\\"
877
       && substr($filename, 1, 2) != ":/"
878
    ) {
833 879
      $filename = $this->root."/".$filename;
834 880
    }
835 881

  
......
853 899
  * @return    string
854 900
  */
855 901
  function varname($varname) {
856
    return preg_quote("{".$varname."}");
902
    return preg_quote("{" . $varname . "}");
857 903
  }
858 904

  
859 905

  
......
907 953
      return false;
908 954
    }
909 955

  
956
    if ($this->filename_comments) {
957
      $str = "<!-- START FILE $filename -->\n$str<!-- END FILE $filename -->\n";
958
    }
910 959
    if ($this->debug & 4) {
911 960
      printf("<b>loadfile:</b> loaded $filename into $varname<br>\n");
912 961
    }

Also available in: Unified diff