JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
re-licensed under GPLv3
[wfpl.git] / template.php
1 <?php
2
3
4 # This file contains generally useful template handling code. It is wrapped in
5 # an object so that if you want/need to you can make more than one instance of
6 # it and they won't step on each other's toes. Also there are a set of global
7 # functions at the bottom so you don't have to mess around with objects if you
8 # don't want to. The documentation will be on the object methods, but just know
9 # that each has a straight function wrapper at the bottom with 'tem_' prepended
10 # to the name.
11
12 # This is designed to be as simple as it can be for your project. The simple
13 # way to use it is to set some key/value pairs with tem_set() then call
14 # tem_output('filename.html') to output the page. A more complex example
15 # including the use of sub-templates can be found in tem_test.php
16
17 # FIXME: sub-sub templates need to be cleared when the sub template containing
18 # them is run
19
20 require_once('code/wfpl/encode.php');
21 require_once('code/wfpl/misc.php');
22 require_once('code/wfpl/file.php');
23
24 class tem {
25         var $keyval;        # an array containing key/value pairs 
26         var $filename;      # template filename (sometimes not set)
27         var $template;      # contents of template
28         var $sub_templates; # tag-name/template-string pairs
29         var $sub_subs;      # key: sub-template name  value: array of names of the sub-templates of this one
30
31         # initialize variables
32         function tem() {
33                 $this->keyval = array('' => '~'); # so that ~~ in the template creates a single ~
34                 $this->sub_templates = array();
35         }
36
37         # set a key/value pair. if a ~tag~ in the template matches key it will be replaced by value
38         function set($key, $value) {
39                 $this->keyval[$key] = $value;
40         }
41
42         # like set() but appends
43         function append($key, $value) {
44                 $this->keyval[$key] .= $value;
45         }
46
47         # like set() but prepends
48         function prepend($key, $value) {
49                 $this->keyval[$key] = $value . $this->keyval[$key];
50         }
51
52         # clear a value. Functionally equivalent to set($key, '') but cleaner and more efficient
53         function clear($key) {
54                 unset($this->keyval[$key]);
55         }
56
57         # grab a value you stuck in earlier with set()
58         function get($key) {
59                 return $this->keyval[$key];
60         }
61
62         # depricated (renamed show())
63         function sub($sub_template_name) {
64                 $this->show($sub_template_name);
65         }
66
67         # run the template engine on one of the sub-templates and append the result
68         # to the keyval in the main array. See tem_test.php for an example of how
69         # this can be used.
70         function show($sub_template_name) {
71                 $this->keyval[$sub_template_name] .= template_run($this->sub_templates[$sub_template_name], $this->keyval);
72
73                 # after running a sub-template, clear its sub-templates
74                 if(isset($this->sub_subs[$sub_template_name])) {
75                         foreach($this->sub_subs[$sub_template_name] as $sub_sub) {
76                                 $this->clear($sub_sub);
77                         }
78                 }
79         }
80
81         function show_separated($sub_template_name) {
82                 if($this->get($sub_template_name)) {
83                         $this->show($sub_template_name . '_sep');
84                 }
85                 $this->show($sub_template_name);
86         }
87
88         # this is used by tem::load() and should be otherwise useless
89         function _load(&$in, &$out, &$parents, &$parent) {
90                 while($in) {
91                         # scan for one of: 1) the begining of a sub-template 2) the end of this one 3) the end of the file
92                         $n = strpos($in, '<!--~');
93                         if($n === false) { # not found
94                                 # we hit the end of the file
95                                 $out .= $in;
96                                 $in = '';
97                                 return;
98                         }
99
100                         # move everything up to (but not including) <!-- to the output
101                         $out .= substr($in, 0, $n);
102                         $in = substr($in, $n);
103
104                         # we found something.
105                         # is it an end tag?
106                         if(strcmp('<!--~end~-->', substr($in, 0, 12)) == 0) {
107                                 $in = substr($in, 12);
108                                 $parent = array_pop($parents);
109                                 return;
110                         }
111
112                         $matches = array();
113                         # this limits sub_template names to 50 chars
114                         if(ereg('^<!--~([^~]*) start~-->', substr($in, 0, 65), $matches)) {
115                                 list($start_tag, $tag_name) = $matches;
116
117                                 # keep track of the tree
118                                 if(!isset($this->sub_subs[$parent])) {
119                                         $this->sub_subs[$parent] = array();
120                                 }
121                                 array_push($this->sub_subs[$parent], $tag_name);
122                                 array_push($parents, $parent);
123                                 $parent = $tag_name;
124
125                                 $out .= '~' . $tag_name . '~';
126                                 $in = substr($in, strlen($start_tag));
127                                 $this->sub_templates[$tag_name] = '';
128                                 $this->_load($in, $this->sub_templates[$tag_name], $parents, $parent);
129                         } else {
130                                 # it's not a start tag or end tag, so let's pass it through:
131                                 $out .= substr($in, 0, 5);
132                                 $in = substr($in, 5);
133                         }
134                 } #repeat
135         }
136
137         # like load() except you pass a string instead of a filename
138         function load_str($str) {
139                 $this->template = '';
140                 $parents = array('top_level_subs' => array());
141                 $parent = 'top_level_subs';
142                 $this->_load($str, $this->template, $parents, $parent);
143         }
144
145         # This is useful when you have sub-templates that you want to mess with
146         # before the main template is run. But can also be used to simply specify
147         # the filename ahead of time.
148         function load($filename) {
149                 $this->filename = $filename;
150                 $this->load_str(read_whole_file($filename));
151         }
152                 
153         # Run the template. Pass a filename, or a string, unless you've already
154         # specified a template with load()
155         function run($templ = false) {
156                 $template_string = $this->template;
157                 $template_file = $this->file;
158                 if($templ !== false) {
159                         if(strlen($templ) < 150 && file_exists($templ)) {
160                                 $template_file = $templ;
161                                 unset($template_string);
162                         } else {
163                                 $template_string = $templ;
164                         }
165                 }
166
167                 if(!$template_string) {
168                         if(!$template_file) {
169                                 print "sorry, no template to run\n";
170                                 exit(1);
171                         }
172
173                         $template_string = read_whole_file($template_file);
174                 }
175                 
176                 return template_run($template_string, $this->keyval);
177         }       
178
179         # same as run() except the output is print()ed
180         function output($templ = false) {
181                 print($this->run($templ));
182         }
183
184         # return the names of the top level subs, or an empty array
185         function top_sub_names() {
186                 if(isset($this->sub_subs['top_level_subs'])) {
187                         return $this->sub_subs['top_level_subs'];
188                 } else {
189                         return array();
190                 }
191         }
192
193         # return the contents of the top-level sub-templates
194         #
195         # this does not run the sub-templates, so if you've not called tem_show() on them, they will be blank.
196         #
197         # Return a hash.
198         #     keys: name of top level sub-template.
199         #     values: contents of said sub-template.
200         function top_subs() {
201                 $ret = array();
202                 $names = $this->top_sub_names();
203                 foreach($names as $name) {
204                         $ret[$name] = $this->get($name);
205                 }
206                 return $ret;
207         }
208 }
209
210 # Below are functions so you can use the above class without allocating or
211 # keeping track of it.
212
213 # get a reference to the current template object
214 function tem_init() { 
215         if(!$GLOBALS['wfpl_template']) {
216                 $GLOBALS['wfpl_template'] = new tem();
217         }
218 }
219                 
220 function tem_append($key, $value) {
221         tem_init();
222         $GLOBALS['wfpl_template']->append($key, $value);
223 }
224         
225 function tem_prepend($key, $value) {
226         tem_init();
227         $GLOBALS['wfpl_template']->prepend($key, $value);
228 }
229         
230 function tem_set($key, $value) {
231         tem_init();
232         $GLOBALS['wfpl_template']->set($key, $value);
233 }
234         
235 function tem_get($key) {
236         tem_init();
237         return $GLOBALS['wfpl_template']->get($key);
238 }
239
240 function tem_run($templ = false) {
241         tem_init();
242         return $GLOBALS['wfpl_template']->run($templ);
243 }
244
245 # depricated (renamed tem_show())
246 function tem_sub($sub_template_name) {
247         tem_show($sub_template_name);
248 }
249
250 function tem_show($sub_template_name) {
251         tem_init();
252         $GLOBALS['wfpl_template']->show($sub_template_name);
253 }
254
255 function tem_show_separated($sub_template_name) {
256         tem_init();
257         $GLOBALS['wfpl_template']->show_separated($sub_template_name);
258 }
259
260
261 function tem_load($filename) {
262         tem_init();
263         $GLOBALS['wfpl_template']->load($filename);
264 }
265
266 function tem_output($filename = false) {
267         tem_init();
268         $GLOBALS['wfpl_template']->output($filename);
269 }
270
271
272
273 # this is used in template_run() and should be of no other use
274 function template_filler($matches) {
275         $match = array_pop($matches);
276         list($tag, $enc) = explode('.', $match, 2);
277         $value = $GLOBALS['wfpl_template_keyval'][$tag];
278         if($enc) {
279                 $encs = explode('.', $enc);
280                 foreach($encs as $enc) {
281                         $enc = "enc_$enc";
282                         if(function_exists($enc)) {
283                                 $value = $enc($value, $tag);
284                         } else {
285                                 print "ERROR: encoder function '$enc' not found.<br>\n";
286                                 exit(1);
287                         }
288                 }
289         }
290         return $value;
291 }
292
293
294 # pass a template string and an associative array of the key/values and it
295 # returns the result.
296 function template_run($template, &$keyval) {
297         $GLOBALS['wfpl_template_keyval'] =& $keyval;
298         return preg_replace_callback('`<!--~([^~]*)~-->|~([^~]*)~|<span class="template">([^<]*)</span>|<p class="template">([^<]*)</p>`', 'template_filler', $template);
299 }
300
301 function tem_top_sub_names() {
302         tem_init();
303         return $GLOBALS['wfpl_template']->top_sub_names();
304 }
305
306 function tem_top_subs() {
307         tem_init();
308         return $GLOBALS['wfpl_template']->top_subs();
309 }
310
311 # replaces currently set template, and returns the old.
312 function tem_load_new($file) {
313         $old = $GLOBALS['wfpl_template'];
314         $GLOBALS['wfpl_template'] = new tem();
315         $GLOBALS['wfpl_template']->load($file);
316         return $old;
317 }
318
319 ?>