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