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
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)
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.
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,
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
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
36 require_once('code/wfpl/encode.php');
39 var $keyval; # an array containing key/value pairs
40 var $filename; # template filename (sometimes unset)
41 var $template; # contents of template
42 var $sub_templates; # tag-name/template-string pairs
44 # initialize variables
46 $this->keyval = array('' => '~');
47 $this->sub_templates = array();
50 # set a key/value pair. if a ~tag~ in the template matches key it will be replaced by value
51 function set($key, $value) {
52 $this->keyval[$key] = $value;
55 # grab a value you stuck in earlier with set()
57 return $this->keyval[$key];
60 # run the template engine on one of the sub-templates and append the result
61 # to the keyval in the main array. See tem_test.php for an example of how
63 function sub($sub_template_name) {
64 $this->keyval[$sub_template_name] .= template_run($this->sub_templates[$sub_template_name], $this->keyval);
67 # this is used by tem::load() and should be otherwise useless
68 function _load(&$in, &$out) {
70 # scan for one of: 1) the begining of a sub-template 2) the end of this one 3) the end of the file
71 $n = strpos($in, '<!--~');
72 if($n === false) { # not found
73 # we hit the end of the file
79 $out .= substr($in, 0, $n);
80 $in = substr($in, $n);
84 if(strcmp('<!--~end~-->', substr($in, 0, 12)) == 0) {
85 $in = substr($in, 12);
90 # this limits sub_template names to 50 chars
91 if(ereg('<!--~([^~]*) start~-->', substr($in, 0, 65), $matches)) {
92 list($start_tag, $tag_name) = $matches;
93 $out .= '~' . $tag_name . '~';
94 $in = substr($in, strlen($start_tag));
95 $this->sub_templates[$tag_name] = '';
96 $this->_load($in, $this->sub_templates[$tag_name]);
98 # it's not a start tag or end tag, so let's pass it through:
99 $out .= substr($in, 0, 5);
100 $in = substr($in, 5);
105 # This is useful when you have sub-templates that you want to mess with
106 # before the main template is run. But can also be used to simply specify
107 # the filename ahead of time.
108 function load($filename) {
109 $this->filename = $filename;
110 $tmp = read_whole_file($filename);
111 $this->template = '';
112 $this->_load($tmp, $this->template);
115 # Run the template. Pass a filename, or a string, unless you've already
116 # specified a template with load()
117 function run($templ = false) {
118 if($templ !== false) {
119 if(strlen($templ) < 150 && file_exists($templ)) {
120 $this->filename = $templ;
121 unset($this->template);
123 $this->template = $templ;
127 if(!$this->template) {
128 if(!$this->filename) {
129 print "sorry, no template to run\n";
133 $this->template = read_whole_file($this->filename);
136 return template_run($this->template, $this->keyval);
139 # same as run() except the output is print()ed
140 function output($templ = false) {
141 print($this->run($templ));
145 # Below are functions so you can use the above class without allocating or
146 # keeping track of it.
148 # get a reference to the current template object
149 function tem_init() {
150 if(!$GLOBALS['wfpl_template']) {
151 $GLOBALS['wfpl_template'] = new tem();
155 function tem_set($key, $value) {
157 $GLOBALS['wfpl_template']->set($key, $value);
160 function tem_get($key) {
162 return $GLOBALS['wfpl_template']->get($key);
165 function tem_run($templ = false) {
167 return $GLOBALS['wfpl_template']->run($templ);
170 function tem_sub($sub_template_name) {
172 $GLOBALS['wfpl_template']->sub($sub_template_name);
175 function tem_load($filename) {
177 $GLOBALS['wfpl_template']->load($filename);
180 function tem_output($filename = false) {
182 $GLOBALS['wfpl_template']->output($filename);
187 function read_whole_file($name) {
188 $fd = fopen($name, 'r');
190 die("Failed to read file: '$name'");
192 $temp = fread($fd, filesize($name));
197 # this is used in template_run() and should be of no other use
198 function template_filler($matches) {
199 list($tag, $enc) = explode('.', $matches[1], 2);
200 $value = $GLOBALS['wfpl_template_keyval'][$tag];
203 if(function_exists($enc)) {
204 $value = $enc($value);
206 print "ERROR: encoder function '$enc' not found.<br>\n";
214 # pass a template string and an associative array of the key/values and it
215 # returns the result.
216 function template_run($template, &$keyval) {
217 $GLOBALS['wfpl_template_keyval'] =& $keyval;
218 return preg_replace_callback(array('|<!--~([^~]*)~-->|', '|~([^~]*)~|', '|<span class="template">([^<]*)</span>|', '|<p class="template">([^<]*)</p>|'), 'template_filler', $template);