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