JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added pulldown support, added leftcheck
[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         # clear a value. Functionally equivalent to set($key, '') but cleaner and more efficient
61         function clear($key) {
62                 unset($this->keyval[$key]);
63         }
64
65         # grab a value you stuck in earlier with set()
66         function get($key) {
67                 return $this->keyval[$key];
68         }
69
70         # run the template engine on one of the sub-templates and append the result
71         # to the keyval in the main array. See tem_test.php for an example of how
72         # this can be used.
73         function sub($sub_template_name) {
74                 $this->keyval[$sub_template_name] .= template_run($this->sub_templates[$sub_template_name], $this->keyval);
75
76                 # after running a sub-template, clear its sub-templates
77                 if(isset($this->sub_subs[$sub_template_name])) {
78                         foreach($this->sub_subs[$sub_template_name] as $sub_sub) {
79                                 $this->clear($sub_sub);
80                         }
81                 }
82         }
83
84         # this is used by tem::load() and should be otherwise useless
85         function _load(&$in, &$out, &$parents, &$parent) {
86                 while($in) {
87                         # scan for one of: 1) the begining of a sub-template 2) the end of this one 3) the end of the file
88                         $n = strpos($in, '<!--~');
89                         if($n === false) { # not found
90                                 # we hit the end of the file
91                                 $out .= $in;
92                                 $in = '';
93                                 return;
94                         }
95
96                         # move everything up to (but not including) <!-- to the output
97                         $out .= substr($in, 0, $n);
98                         $in = substr($in, $n);
99
100                         # we found something.
101                         # is it an end tag?
102                         if(strcmp('<!--~end~-->', substr($in, 0, 12)) == 0) {
103                                 $in = substr($in, 12);
104                                 $parent = array_pop($parents);
105                                 return;
106                         }
107
108                         $matches = array();
109                         # this limits sub_template names to 50 chars
110                         if(ereg('^<!--~([^~]*) start~-->', substr($in, 0, 65), $matches)) {
111                                 list($start_tag, $tag_name) = $matches;
112
113                                 # keep track of the tree
114                                 if(!isset($this->sub_subs[$parent])) {
115                                         $this->sub_subs[$parent] = array();
116                                 }
117                                 array_push($this->sub_subs[$parent], $tag_name);
118                                 array_push($parents, $parent);
119                                 $parent = $tag_name;
120
121                                 $out .= '~' . $tag_name . '~';
122                                 $in = substr($in, strlen($start_tag));
123                                 $this->sub_templates[$tag_name] = '';
124                                 $this->_load($in, $this->sub_templates[$tag_name], $parents, $parent);
125                         } else {
126                                 # it's not a start tag or end tag, so let's pass it through:
127                                 $out .= substr($in, 0, 5);
128                                 $in = substr($in, 5);
129                         }
130                 } #repeat
131         }
132
133         # like load() except you pass a string instead of a filename
134         function load_str($str) {
135                 $this->template = '';
136                 $parents = array('top_level_subs' => array());
137                 $parent = 'top_level_subs';
138                 $this->_load($str, $this->template, $parents, $parent);
139         }
140
141         # This is useful when you have sub-templates that you want to mess with
142         # before the main template is run. But can also be used to simply specify
143         # the filename ahead of time.
144         function load($filename) {
145                 $this->filename = $filename;
146                 $this->load_str(read_whole_file($filename));
147         }
148                 
149         # Run the template. Pass a filename, or a string, unless you've already
150         # specified a template with load()
151         function run($templ = false) {
152                 $template_string = $this->template;
153                 $template_file = $this->file;
154                 if($templ !== false) {
155                         if(strlen($templ) < 150 && file_exists($templ)) {
156                                 $template_file = $templ;
157                                 unset($template_string);
158                         } else {
159                                 $template_string = $templ;
160                         }
161                 }
162
163                 if(!$template_string) {
164                         if(!$template_file) {
165                                 print "sorry, no template to run\n";
166                                 exit(1);
167                         }
168
169                         $template_string = read_whole_file($template_file);
170                 }
171                 
172                 return template_run($template_string, $this->keyval);
173         }       
174
175         # same as run() except the output is print()ed
176         function output($templ = false) {
177                 print($this->run($templ));
178         }
179
180         # return the contents of the top-level sub-templates
181         #
182         # this does not run the sub-templates, so if you've not called tem_sub() on them, they will be blank.
183         #
184         # Return a hash.
185         #     keys: name of top level sub-template.
186         #     values: contents of said sub-template.
187         function top_subs() {
188                 $ret = array();
189                 if(isset($this->sub_subs['top_level_subs'])) {
190                         foreach($this->sub_subs['top_level_subs'] as $name) {
191                                 $ret[$name] = $this->get($name);
192                         }
193                 }
194                 return $ret;
195         }
196 }
197
198 # Below are functions so you can use the above class without allocating or
199 # keeping track of it.
200
201 # get a reference to the current template object
202 function tem_init() { 
203         if(!$GLOBALS['wfpl_template']) {
204                 $GLOBALS['wfpl_template'] = new tem();
205         }
206 }
207                 
208 function tem_set($key, $value) {
209         tem_init();
210         $GLOBALS['wfpl_template']->set($key, $value);
211 }
212         
213 function tem_get($key) {
214         tem_init();
215         return $GLOBALS['wfpl_template']->get($key);
216 }
217
218 function tem_run($templ = false) {
219         tem_init();
220         return $GLOBALS['wfpl_template']->run($templ);
221 }
222
223 function tem_sub($sub_template_name) {
224         tem_init();
225         $GLOBALS['wfpl_template']->sub($sub_template_name);
226 }
227
228 function tem_load($filename) {
229         tem_init();
230         $GLOBALS['wfpl_template']->load($filename);
231 }
232
233 function tem_output($filename = false) {
234         tem_init();
235         $GLOBALS['wfpl_template']->output($filename);
236 }
237
238
239
240 # this is used in template_run() and should be of no other use
241 function template_filler($matches) {
242         list($tag, $enc) = explode('.', $matches[1], 2);
243         $value = $GLOBALS['wfpl_template_keyval'][$tag];
244         if($enc) {
245                 $encs = explode('.', $enc);
246                 foreach($encs as $enc) {
247                         $enc = "enc_$enc";
248                         if(function_exists($enc)) {
249                                 $value = $enc($value, $tag);
250                         } else {
251                                 print "ERROR: encoder function '$enc' not found.<br>\n";
252                                 exit(1);
253                         }
254                 }
255         }
256         return $value;
257 }
258
259
260 # pass a template string and an associative array of the key/values and it
261 # returns the result.
262 function template_run($template, &$keyval) {
263         $GLOBALS['wfpl_template_keyval'] =& $keyval;
264         return preg_replace_callback(array('|<!--~([^~]*)~-->|', '|~([^~]*)~|', '|<span class="template">([^<]*)</span>|', '|<p class="template">([^<]*)</p>|'), 'template_filler', $template);
265 }
266
267 function tem_top_subs() {
268         tem_init();
269         return $GLOBALS['wfpl_template']->top_subs();
270 }
271
272 ?>