Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

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