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