root/www-website/template.inc

Revision 782, 9.2 kB (checked in by asterix, 3 years ago)

templates tests, any comment ? this is better than all the print !

Line 
1<?php
2/*
3 * Session Management for PHP3
4 *
5 * (C) Copyright 1999-2000 NetUSE GmbH
6 *                    Kristian Koehntopp
7 *
8 * $Id: template.inc,v 1.5 2000/07/12 18:22:35 kk Exp $
9 *
10 */
11
12class Template {
13  var $classname = "Template";
14
15  /* if set, echo assignments */
16  var $debug     = false;
17
18  /* $file[handle] = "filename"; */
19  var $file  = array();
20
21  /* relative filenames are relative to this pathname */
22  var $root   = "";
23
24  /* $varkeys[key] = "key"; $varvals[key] = "value"; */
25  var $varkeys = array();
26  var $varvals = array();
27
28  /* "remove"  => remove undefined variables
29   * "comment" => replace undefined variables with comments
30   * "keep"    => keep undefined variables
31   */
32  var $unknowns = "remove";
33
34  /* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly */
35  var $halt_on_error  = "yes";
36
37  /* last error message is retained here */
38  var $last_error     = "";
39
40
41  /***************************************************************************/
42  /* public: Constructor.
43   * root:     template directory.
44   * unknowns: how to handle unknown variables.
45   */
46  function Template($root = ".", $unknowns = "remove") {
47    $this->set_root($root);
48    $this->set_unknowns($unknowns);
49  }
50
51  /* public: setroot(pathname $root)
52   * root:   new template directory.
53   */
54  function set_root($root) {
55    if (!is_dir($root)) {
56      $this->halt("set_root: $root is not a directory.");
57      return false;
58    }
59
60    $this->root = $root;
61    return true;
62  }
63
64  /* public: set_unknowns(enum $unknowns)
65   * unknowns: "remove", "comment", "keep"
66   *
67   */
68  function set_unknowns($unknowns = "keep") {
69    $this->unknowns = $unknowns;
70  }
71
72  /* public: set_file(array $filelist)
73   * filelist: array of handle, filename pairs.
74   *
75   * public: set_file(string $handle, string $filename)
76   * handle: handle for a filename,
77   * filename: name of template file
78   */
79  function set_file($handle, $filename = "") {
80    if (!is_array($handle)) {
81      if ($filename == "") {
82        $this->halt("set_file: For handle $handle filename is empty.");
83        return false;
84      }
85      $this->file[$handle] = $this->filename($filename);
86    } else {
87      reset($handle);
88      while(list($h, $f) = each($handle)) {
89        $this->file[$h] = $this->filename($f);
90      }
91    }
92  }
93
94  /* public: set_block(string $parent, string $handle, string $name = "")
95   * extract the template $handle from $parent,
96   * place variable {$name} instead.
97   */
98  function set_block($parent, $handle, $name = "") {
99    if (!$this->loadfile($parent)) {
100      $this->halt("subst: unable to load $parent.");
101      return false;
102    }
103    if ($name == "")
104      $name = $handle;
105
106    $str = $this->get_var($parent);
107    //$reg = "/<!--\s+BEGIN $handle\s+-->(.*)\n\s*<!--\s+END $handle\s+-->/sm";
108    $reg = "/<!--\s+BEGIN $handle\s+-->(.*)<!--\s+END $handle\s+-->/sm";
109    preg_match_all($reg, $str, $m);
110    $str = preg_replace($reg, "{" . "$name}", $str);
111    $this->set_var($handle, $m[1][0]);
112    $this->set_var($parent, $str);
113  }
114
115  /* public: set_var(array $values)
116   * values: array of variable name, value pairs.
117   *
118   * public: set_var(string $varname, string $value)
119   * varname: name of a variable that is to be defined
120   * value:   value of that variable
121   */
122  function set_var($varname, $value = "") {
123    if (!is_array($varname)) {
124      if (!empty($varname))
125        if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n";
126        $this->varkeys[$varname] = "/".$this->varname($varname)."/";
127        $this->varvals[$varname] = $value;
128    } else {
129      reset($varname);
130      while(list($k, $v) = each($varname)) {
131        if (!empty($k))
132          if ($this->debug) print "array: set *$k* to *$v*<br>\n";
133          $this->varkeys[$k] = "/".$this->varname($k)."/";
134          $this->varvals[$k] = $v;
135      }
136    }
137  }
138
139  /* public: subst(string $handle)
140   * handle: handle of template where variables are to be substituted.
141   */
142  function subst($handle) {
143    if (!$this->loadfile($handle)) {
144      $this->halt("subst: unable to load $handle.");
145      return false;
146    }
147
148    $str = $this->get_var($handle);
149    $str = @preg_replace($this->varkeys, $this->varvals, $str);
150    return $str;
151  }
152
153  /* public: psubst(string $handle)
154   * handle: handle of template where variables are to be substituted.
155   */
156  function psubst($handle) {
157    print $this->subst($handle);
158
159    return false;
160  }
161
162  /* public: parse(string $target, string $handle, boolean append)
163   * public: parse(string $target, array  $handle, boolean append)
164   * target: handle of variable to generate
165   * handle: handle of template to substitute
166   * append: append to target handle
167   */
168  function parse($target, $handle, $append = false) {
169    if (!is_array($handle)) {
170      $str = $this->subst($handle);
171      if ($append) {
172        $this->set_var($target, $this->get_var($target) . $str);
173      } else {
174        $this->set_var($target, $str);
175      }
176    } else {
177      reset($handle);
178      while(list($i, $h) = each($handle)) {
179        $str = $this->subst($h);
180        $this->set_var($target, $str);
181      }
182    }
183
184    return $str;
185  }
186
187  function pparse($target, $handle, $append = false) {
188    print $this->parse($target, $handle, $append);
189    return false;
190  }
191
192  /* public: get_vars()
193   */
194  function get_vars() {
195    reset($this->varkeys);
196    while(list($k, $v) = each($this->varkeys)) {
197      $result[$k] = $this->varvals[$k];
198    }
199
200    return $result;
201  }
202
203  /* public: get_var(string varname)
204   * varname: name of variable.
205   *
206   * public: get_var(array varname)
207   * varname: array of variable names
208   */
209  function get_var($varname) {
210    if (!is_array($varname)) {
211      return $this->varvals[$varname];
212    } else {
213      reset($varname);
214      while(list($k, $v) = each($varname)) {
215        $result[$k] = $this->varvals[$k];
216      }
217
218      return $result;
219    }
220  }
221
222  /* public: get_undefined($handle)
223   * handle: handle of a template.
224   */
225  function get_undefined($handle) {
226    if (!$this->loadfile($handle)) {
227      $this->halt("get_undefined: unable to load $handle.");
228      return false;
229    }
230
231    preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
232    $m = $m[1];
233    if (!is_array($m))
234      return false;
235
236    reset($m);
237    while(list($k, $v) = each($m)) {
238      if (!isset($this->varkeys[$v]))
239        $result[$v] = $v;
240    }
241
242    if (count($result))
243      return $result;
244    else
245      return false;
246  }
247
248  /* public: finish(string $str)
249   * str: string to finish.
250   */
251  function finish($str) {
252    switch ($this->unknowns) {
253      case "keep":
254      break;
255
256      case "remove":
257        $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);
258      break;
259
260      case "comment":
261        $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
262      break;
263    }
264
265    return $str;
266  }
267
268  /* public: p(string $varname)
269   * varname: name of variable to print.
270   */
271  function p($varname) {
272    print $this->finish($this->get_var($varname));
273  }
274
275  function write($varname,$file)
276  {
277   $myfile=fopen($file,"w+");
278   fputs($myfile,$this->finish($this->get_var($varname)));
279  }
280
281  function get($varname) {
282    return $this->finish($this->get_var($varname));
283  }
284
285  /***************************************************************************/
286  /* private: filename($filename)
287   * filename: name to be completed.
288   */
289  function filename($filename) {
290    if (substr($filename, 0, 1) != "/") {
291      $filename = $this->root."/".$filename;
292    }
293
294    if (!file_exists($filename))
295      $this->halt("filename: file $filename does not exist.");
296
297    return $filename;
298  }
299
300  /* private: varname($varname)
301   * varname: name of a replacement variable to be protected.
302   */
303  function varname($varname) {
304    return preg_quote("{".$varname."}");
305  }
306
307  /* private: loadfile(string $handle)
308   * handle:  load file defined by handle, if it is not loaded yet.
309   */
310  function loadfile($handle) {
311    if (isset($this->varkeys[$handle]) and !empty($this->varvals[$handle]))
312      return true;
313
314    if (!isset($this->file[$handle])) {
315      $this->halt("loadfile: $handle is not a valid handle.");
316      return false;
317    }
318    $filename = $this->file[$handle];
319
320    $str = implode("", @file($filename));
321    if (empty($str)) {
322      $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
323      return false;
324    }
325
326    $this->set_var($handle, $str);
327
328    return true;
329  }
330
331  /***************************************************************************/
332  /* public: halt(string $msg)
333   * msg:    error message to show.
334   */
335  function halt($msg) {
336    $this->last_error = $msg;
337
338    if ($this->halt_on_error != "no")
339      $this->haltmsg($msg);
340
341    if ($this->halt_on_error == "yes")
342      die("<b>Halted.</b>");
343
344    return false;
345  }
346
347  /* public, override: haltmsg($msg)
348   * msg: error message to show.
349   */
350  function haltmsg($msg) {
351    printf("<b>Template Error:</b> %s<br>\n", $msg);
352  }
353}
354?>
Note: See TracBrowser for help on using the browser.