3 # Copyright (C) 2005 Jason Woofenden
5 # This file is part of wfpl.
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)
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
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
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
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
35 # FIXME: sub-sub templates need to be cleared when the sub template containing
38 require_once('code/wfpl/encode.php');
39 require_once('code/wfpl/misc.php');
40 require_once('code/wfpl/file.php');
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
49 # initialize variables
51 $this->keyval = array('' => '~'); # so that ~~ in the template creates a single ~
52 $this->sub_templates = array();
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;
60 # clear a value. Functionally equivalent to set($key, '') but cleaner and more efficient
61 function clear($key) {
62 unset($this->keyval[$key]);
65 # grab a value you stuck in earlier with set()
67 return $this->keyval[$key];
70 # depricated (renamed show())
71 function sub($sub_template_name) {
72 $this->show($sub_template_name);
75 # run the template engine on one of the sub-templates and append the result
76 # to the keyval in the main array. See tem_test.php for an example of how
78 function show($sub_template_name) {
79 $this->keyval[$sub_template_name] .= template_run($this->sub_templates[$sub_template_name], $this->keyval);
81 # after running a sub-template, clear its sub-templates
82 if(isset($this->sub_subs[$sub_template_name])) {
83 foreach($this->sub_subs[$sub_template_name] as $sub_sub) {
84 $this->clear($sub_sub);
89 # this is used by tem::load() and should be otherwise useless
90 function _load(&$in, &$out, &$parents, &$parent) {
92 # scan for one of: 1) the begining of a sub-template 2) the end of this one 3) the end of the file
93 $n = strpos($in, '<!--~');
94 if($n === false) { # not found
95 # we hit the end of the file
101 # move everything up to (but not including) <!-- to the output
102 $out .= substr($in, 0, $n);
103 $in = substr($in, $n);
105 # we found something.
107 if(strcmp('<!--~end~-->', substr($in, 0, 12)) == 0) {
108 $in = substr($in, 12);
109 $parent = array_pop($parents);
114 # this limits sub_template names to 50 chars
115 if(ereg('^<!--~([^~]*) start~-->', substr($in, 0, 65), $matches)) {
116 list($start_tag, $tag_name) = $matches;
118 # keep track of the tree
119 if(!isset($this->sub_subs[$parent])) {
120 $this->sub_subs[$parent] = array();
122 array_push($this->sub_subs[$parent], $tag_name);
123 array_push($parents, $parent);
126 $out .= '~' . $tag_name . '~';
127 $in = substr($in, strlen($start_tag));
128 $this->sub_templates[$tag_name] = '';
129 $this->_load($in, $this->sub_templates[$tag_name], $parents, $parent);
131 # it's not a start tag or end tag, so let's pass it through:
132 $out .= substr($in, 0, 5);
133 $in = substr($in, 5);
138 # like load() except you pass a string instead of a filename
139 function load_str($str) {
140 $this->template = '';
141 $parents = array('top_level_subs' => array());
142 $parent = 'top_level_subs';
143 $this->_load($str, $this->template, $parents, $parent);
146 # This is useful when you have sub-templates that you want to mess with
147 # before the main template is run. But can also be used to simply specify
148 # the filename ahead of time.
149 function load($filename) {
150 $this->filename = $filename;
151 $this->load_str(read_whole_file($filename));
154 # Run the template. Pass a filename, or a string, unless you've already
155 # specified a template with load()
156 function run($templ = false) {
157 $template_string = $this->template;
158 $template_file = $this->file;
159 if($templ !== false) {
160 if(strlen($templ) < 150 && file_exists($templ)) {
161 $template_file = $templ;
162 unset($template_string);
164 $template_string = $templ;
168 if(!$template_string) {
169 if(!$template_file) {
170 print "sorry, no template to run\n";
174 $template_string = read_whole_file($template_file);
177 return template_run($template_string, $this->keyval);
180 # same as run() except the output is print()ed
181 function output($templ = false) {
182 print($this->run($templ));
185 # return the names of the top level subs, or an empty array
186 function top_sub_names() {
187 if(isset($this->sub_subs['top_level_subs'])) {
188 return $this->sub_subs['top_level_subs'];
194 # return the contents of the top-level sub-templates
196 # this does not run the sub-templates, so if you've not called tem_show() on them, they will be blank.
199 # keys: name of top level sub-template.
200 # values: contents of said sub-template.
201 function top_subs() {
203 $names = $this->top_sub_names();
204 foreach($names as $name) {
205 $ret[$name] = $this->get($name);
211 # Below are functions so you can use the above class without allocating or
212 # keeping track of it.
214 # get a reference to the current template object
215 function tem_init() {
216 if(!$GLOBALS['wfpl_template']) {
217 $GLOBALS['wfpl_template'] = new tem();
221 function tem_set($key, $value) {
223 $GLOBALS['wfpl_template']->set($key, $value);
226 function tem_get($key) {
228 return $GLOBALS['wfpl_template']->get($key);
231 function tem_run($templ = false) {
233 return $GLOBALS['wfpl_template']->run($templ);
236 # depricated (renamed tem_show())
237 function tem_sub($sub_template_name) {
238 tem_show($sub_template_name);
241 function tem_show($sub_template_name) {
243 $GLOBALS['wfpl_template']->show($sub_template_name);
246 function tem_load($filename) {
248 $GLOBALS['wfpl_template']->load($filename);
251 function tem_output($filename = false) {
253 $GLOBALS['wfpl_template']->output($filename);
258 # this is used in template_run() and should be of no other use
259 function template_filler($matches) {
260 list($tag, $enc) = explode('.', $matches[1], 2);
261 $value = $GLOBALS['wfpl_template_keyval'][$tag];
263 $encs = explode('.', $enc);
264 foreach($encs as $enc) {
266 if(function_exists($enc)) {
267 $value = $enc($value, $tag);
269 print "ERROR: encoder function '$enc' not found.<br>\n";
278 # pass a template string and an associative array of the key/values and it
279 # returns the result.
280 function template_run($template, &$keyval) {
281 $GLOBALS['wfpl_template_keyval'] =& $keyval;
282 return preg_replace_callback(array('|<!--~([^~]*)~-->|', '|~([^~]*)~|', '|<span class="template">([^<]*)</span>|', '|<p class="template">([^<]*)</p>|'), 'template_filler', $template);
285 function tem_top_sub_names() {
287 return $GLOBALS['wfpl_template']->top_sub_names();
290 function tem_top_subs() {
292 return $GLOBALS['wfpl_template']->top_subs();
295 # replaces currently set template, and returns the old.
296 function tem_load_new($file) {
297 $old = $GLOBALS['wfpl_template'];
298 $GLOBALS['wfpl_template'] = new tem();
299 $GLOBALS['wfpl_template']->load($file);