JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
renamed basics.php to misc.php
[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
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file contains generally useful template handling code. It is wrapped in
24 # an object so that if you want/need to you can make more than one instance of
25 # it and they won't step on each other's toes. Also there are a set of global
26 # functions at the bottom so you don't have to mess around with objects if you
27 # don't want to. The documentation will be on the object methods, but just know
28 # that each has a straight function wrapper at the bottom with 'tem_' prepended
29 # to the name.
30
31 # This is designed to be as simple as it can be for your project. The simple
32 # way to use it is to set some key/value pairs with tem_set() then call
33 # tem_output('filename.html') to output the page. A more complex example
34 # including the use of sub-templates can be found in tem_test.php
35
36 # FIXME: sub-sub templates need to be cleared when the sub template containing
37 # them is run
38
39 require_once('code/wfpl/encode.php');
40 require_once('code/wfpl/misc.php'); # to get read_whole_file()
41
42 class tem {
43         var $keyval;        # an array containing key/value pairs 
44         var $filename;      # template filename (sometimes unset)
45         var $template;      # contents of template
46         var $sub_templates; # tag-name/template-string pairs
47
48         # initialize variables
49         function tem() {
50                 $this->keyval = array('' => '~');
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         # grab a value you stuck in earlier with set()
60         function get($key) {
61                 return $this->keyval[$key];
62         }
63
64         # run the template engine on one of the sub-templates and append the result
65         # to the keyval in the main array. See tem_test.php for an example of how
66         # this can be used.
67         function sub($sub_template_name) {
68                 $this->keyval[$sub_template_name] .= template_run($this->sub_templates[$sub_template_name], $this->keyval);
69         }
70
71         # this is used by tem::load() and should be otherwise useless
72         function _load(&$in, &$out) {
73                 while($in) {
74                         # scan for one of: 1) the begining of a sub-template 2) the end of this one 3) the end of the file
75                         $n = strpos($in, '<!--~');
76                         if($n === false) { # not found
77                                 # we hit the end of the file
78                                 $out .= $in;
79                                 $in = '';
80                                 return;
81                         }
82
83                         # move everything up to (but not including) <!-- to the output
84                         $out .= substr($in, 0, $n);
85                         $in = substr($in, $n);
86
87                         # we found something.
88                         # is it an end tag?
89                         if(strcmp('<!--~end~-->', substr($in, 0, 12)) == 0) {
90                                 $in = substr($in, 12);
91                                 return;
92                         }
93
94                         $matches = array();
95                         # this limits sub_template names to 50 chars
96                         if(ereg('^<!--~([^~]*) start~-->', substr($in, 0, 65), $matches)) {
97                                 list($start_tag, $tag_name) = $matches;
98                                 $out .= '~' . $tag_name . '~';
99                                 $in = substr($in, strlen($start_tag));
100                                 $this->sub_templates[$tag_name] = '';
101                                 $this->_load($in, $this->sub_templates[$tag_name]);
102                         } else {
103                                 # it's not a start tag or end tag, so let's pass it through:
104                                 $out .= substr($in, 0, 5);
105                                 $in = substr($in, 5);
106                         }
107                 } #repeat
108         }
109
110         # This is useful when you have sub-templates that you want to mess with
111         # before the main template is run. But can also be used to simply specify
112         # the filename ahead of time.
113         function load($filename) {
114                 $this->filename = $filename;
115                 $tmp = read_whole_file($filename);
116                 $this->template = '';
117                 $this->_load($tmp, $this->template);
118         }
119                 
120         # Run the template. Pass a filename, or a string, unless you've already
121         # specified a template with load()
122         function run($templ = false) {
123                 if($templ !== false) {
124                         if(strlen($templ) < 150 && file_exists($templ)) {
125                                 $this->filename = $templ;
126                                 unset($this->template);
127                         } else {
128                                 $this->template = $templ;
129                         }
130                 }
131
132                 if(!$this->template) {
133                         if(!$this->filename) {
134                                 print "sorry, no template to run\n";
135                                 exit(1);
136                         }
137
138                         $this->template = read_whole_file($this->filename);
139                 }
140                 
141                 return template_run($this->template, $this->keyval);
142         }       
143
144         # same as run() except the output is print()ed
145         function output($templ = false) {
146                 print($this->run($templ));
147         }
148 }
149
150 # Below are functions so you can use the above class without allocating or
151 # keeping track of it.
152
153 # get a reference to the current template object
154 function tem_init() { 
155         if(!$GLOBALS['wfpl_template']) {
156                 $GLOBALS['wfpl_template'] = new tem();
157         }
158 }
159                 
160 function tem_set($key, $value) {
161         tem_init();
162         $GLOBALS['wfpl_template']->set($key, $value);
163 }
164         
165 function tem_get($key) {
166         tem_init();
167         return $GLOBALS['wfpl_template']->get($key);
168 }
169
170 function tem_run($templ = false) {
171         tem_init();
172         return $GLOBALS['wfpl_template']->run($templ);
173 }
174
175 function tem_sub($sub_template_name) {
176         tem_init();
177         $GLOBALS['wfpl_template']->sub($sub_template_name);
178 }
179
180 function tem_load($filename) {
181         tem_init();
182         $GLOBALS['wfpl_template']->load($filename);
183 }
184
185 function tem_output($filename = false) {
186         tem_init();
187         $GLOBALS['wfpl_template']->output($filename);
188 }
189
190
191
192 # this is used in template_run() and should be of no other use
193 function template_filler($matches) {
194         list($tag, $enc) = explode('.', $matches[1], 2);
195         $value = $GLOBALS['wfpl_template_keyval'][$tag];
196         if($enc) {
197                 $encs = explode('.', $enc);
198                 foreach($encs as $enc) {
199                         $enc = "enc_$enc";
200                         if(function_exists($enc)) {
201                                 $value = $enc($value);
202                         } else {
203                                 print "ERROR: encoder function '$enc' not found.<br>\n";
204                                 exit(1);
205                         }
206                 }
207         }
208         return $value;
209 }
210
211
212 # pass a template string and an associative array of the key/values and it
213 # returns the result.
214 function template_run($template, &$keyval) {
215         $GLOBALS['wfpl_template_keyval'] =& $keyval;
216         return preg_replace_callback(array('|<!--~([^~]*)~-->|', '|~([^~]*)~|', '|<span class="template">([^<]*)</span>|', '|<p class="template">([^<]*)</p>|'), 'template_filler', $template);
217 }
218
219
220 ?>