4 # This file contains generally useful template handling code. It is wrapped in
5 # an object so that if you want/need to you can make more than one instance of
6 # it and they won't step on each other's toes. Also there are a set of global
7 # functions at the bottom so you don't have to mess around with objects if you
8 # don't want to. The documentation will be on the object methods, but just know
9 # that each has a straight function wrapper at the bottom with 'tem_' prepended
12 # This is designed to be as simple as it can be for your project. The simple
13 # way to use it is to set some key/value pairs with tem_set() then call
14 # tem_output('filename.html') to output the page. A more complex example
15 # including the use of sub-templates can be found in tem_test.php
17 # FIXME: sub-sub templates need to be cleared when the sub template containing
20 require_once('code/wfpl/encode.php');
21 require_once('code/wfpl/misc.php');
22 require_once('code/wfpl/file.php');
25 var $keyval; # an array containing key/value pairs
26 var $filename; # template filename (sometimes not set)
27 var $template; # contents of template
28 var $sub_templates; # tag-name/template-string pairs
29 var $sub_subs; # key: sub-template name value: array of names of the sub-templates of this one
31 # initialize variables
33 $this->keyval = array('' => '~'); # so that ~~ in the template creates a single ~
34 $this->sub_templates = array();
37 # set a key/value pair. if a ~tag~ in the template matches key it will be replaced by value
38 function set($key, $value) {
39 $this->keyval[$key] = $value;
42 # like set() but appends
43 function append($key, $value) {
44 $this->keyval[$key] .= $value;
47 # like set() but prepends
48 function prepend($key, $value) {
49 $this->keyval[$key] = $value . $this->keyval[$key];
52 # clear a value. Functionally equivalent to set($key, '') but cleaner and more efficient
53 function clear($key) {
54 unset($this->keyval[$key]);
57 # grab a value you stuck in earlier with set()
59 return $this->keyval[$key];
62 # depricated (renamed show())
63 function sub($sub_template_name) {
64 $this->show($sub_template_name);
67 # run the template engine on one of the sub-templates and append the result
68 # to the keyval in the main array. See tem_test.php for an example of how
70 function show($sub_template_name) {
71 $this->keyval[$sub_template_name] .= template_run($this->sub_templates[$sub_template_name], $this->keyval);
73 # after running a sub-template, clear its sub-templates
74 if(isset($this->sub_subs[$sub_template_name])) {
75 foreach($this->sub_subs[$sub_template_name] as $sub_sub) {
76 $this->clear($sub_sub);
81 function show_separated($sub_template_name) {
82 if($this->get($sub_template_name)) {
83 $this->show($sub_template_name . '_sep');
85 $this->show($sub_template_name);
88 # this is used by tem::load() and should be otherwise useless
89 function _load(&$in, &$out, &$parents, &$parent) {
91 # scan for one of: 1) the begining of a sub-template 2) the end of this one 3) the end of the file
92 $n = strpos($in, '<!--~');
93 if($n === false) { # not found
94 # we hit the end of the file
100 # move everything up to (but not including) <!-- to the output
101 $out .= substr($in, 0, $n);
102 $in = substr($in, $n);
104 # we found something.
106 if(strcmp('<!--~end~-->', substr($in, 0, 12)) == 0) {
107 $in = substr($in, 12);
108 $parent = array_pop($parents);
113 # this limits sub_template names to 50 chars
114 if(ereg('^<!--~([^~]*) start~-->', substr($in, 0, 65), $matches)) {
115 list($start_tag, $tag_name) = $matches;
117 # keep track of the tree
118 if(!isset($this->sub_subs[$parent])) {
119 $this->sub_subs[$parent] = array();
121 array_push($this->sub_subs[$parent], $tag_name);
122 array_push($parents, $parent);
125 $out .= '~' . $tag_name . '~';
126 $in = substr($in, strlen($start_tag));
127 $this->sub_templates[$tag_name] = '';
128 $this->_load($in, $this->sub_templates[$tag_name], $parents, $parent);
130 # it's not a start tag or end tag, so let's pass it through:
131 $out .= substr($in, 0, 5);
132 $in = substr($in, 5);
137 # like load() except you pass a string instead of a filename
138 function load_str($str) {
139 $this->template = '';
140 $parents = array('top_level_subs' => array());
141 $parent = 'top_level_subs';
142 $this->_load($str, $this->template, $parents, $parent);
145 # This is useful when you have sub-templates that you want to mess with
146 # before the main template is run. But can also be used to simply specify
147 # the filename ahead of time.
148 function load($filename) {
149 $this->filename = $filename;
150 $this->load_str(read_whole_file($filename));
153 # Run the template. Pass a filename, or a string, unless you've already
154 # specified a template with load()
155 function run($templ = false) {
156 $template_string = $this->template;
157 $template_file = $this->file;
158 if($templ !== false) {
159 if(strlen($templ) < 150 && file_exists($templ)) {
160 $template_file = $templ;
161 unset($template_string);
163 $template_string = $templ;
167 if(!$template_string) {
168 if(!$template_file) {
169 print "sorry, no template to run\n";
173 $template_string = read_whole_file($template_file);
176 return template_run($template_string, $this->keyval);
179 # same as run() except the output is print()ed
180 function output($templ = false) {
181 print($this->run($templ));
184 # return the names of the top level subs, or an empty array
185 function top_sub_names() {
186 if(isset($this->sub_subs['top_level_subs'])) {
187 return $this->sub_subs['top_level_subs'];
193 # return the contents of the top-level sub-templates
195 # this does not run the sub-templates, so if you've not called tem_show() on them, they will be blank.
198 # keys: name of top level sub-template.
199 # values: contents of said sub-template.
200 function top_subs() {
202 $names = $this->top_sub_names();
203 foreach($names as $name) {
204 $ret[$name] = $this->get($name);
210 # Below are functions so you can use the above class without allocating or
211 # keeping track of it.
213 # get a reference to the current template object
214 function tem_init() {
215 if(!$GLOBALS['wfpl_template']) {
216 $GLOBALS['wfpl_template'] = new tem();
220 function tem_append($key, $value) {
222 $GLOBALS['wfpl_template']->append($key, $value);
225 function tem_prepend($key, $value) {
227 $GLOBALS['wfpl_template']->prepend($key, $value);
230 function tem_set($key, $value) {
232 $GLOBALS['wfpl_template']->set($key, $value);
235 function tem_get($key) {
237 return $GLOBALS['wfpl_template']->get($key);
240 function tem_run($templ = false) {
242 return $GLOBALS['wfpl_template']->run($templ);
245 # depricated (renamed tem_show())
246 function tem_sub($sub_template_name) {
247 tem_show($sub_template_name);
250 function tem_show($sub_template_name) {
252 $GLOBALS['wfpl_template']->show($sub_template_name);
255 function tem_show_separated($sub_template_name) {
257 $GLOBALS['wfpl_template']->show_separated($sub_template_name);
261 function tem_load($filename) {
263 $GLOBALS['wfpl_template']->load($filename);
266 function tem_output($filename = false) {
268 $GLOBALS['wfpl_template']->output($filename);
273 # this is used in template_run() and should be of no other use
274 function template_filler($matches) {
275 $match = array_pop($matches);
276 list($tag, $enc) = explode('.', $match, 2);
277 $value = $GLOBALS['wfpl_template_keyval'][$tag];
279 $encs = explode('.', $enc);
280 foreach($encs as $enc) {
282 if(function_exists($enc)) {
283 $value = $enc($value, $tag);
285 print "ERROR: encoder function '$enc' not found.<br>\n";
294 # pass a template string and an associative array of the key/values and it
295 # returns the result.
296 function template_run($template, &$keyval) {
297 $GLOBALS['wfpl_template_keyval'] =& $keyval;
298 return preg_replace_callback('`<!--~([^~]*)~-->|~([^~]*)~|<span class="template">([^<]*)</span>|<p class="template">([^<]*)</p>`', 'template_filler', $template);
301 function tem_top_sub_names() {
303 return $GLOBALS['wfpl_template']->top_sub_names();
306 function tem_top_subs() {
308 return $GLOBALS['wfpl_template']->top_subs();
311 # replaces currently set template, and returns the old.
312 function tem_load_new($file) {
313 $old = $GLOBALS['wfpl_template'];
314 $GLOBALS['wfpl_template'] = new tem();
315 $GLOBALS['wfpl_template']->load($file);