JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added dwt_get(), messages only display once
[wfpl.git] / dwt.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
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)
10 #  any later version.
11 #
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
15 #  more details.
16 #
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
20
21 require_once('code/wfpl/file.php');
22
23 function dwt_reset() {
24         $GLOBALS['_dwt_keys'] = array('<!-- TemplateEndEditable -->');
25         $GLOBALS['_dwt_values'] = array('');
26 }
27
28 function dwt_init() {
29         if(isset($GLOBALS['_dwt_keys']) && isset($GLOBALS['_dwt_values'])) {
30                 return;
31         }
32         dwt_reset();
33 }
34
35 function dwt_load_str($str) {
36         $GLOBALS['_dwt_template'] = $str;
37         dwt_init();
38 }
39
40 function dwt_load($filename) {
41         dwt_load_str(read_whole_file($filename));
42 }
43
44 function dwt_set_raw($name, $value) {
45         $index = dwt_find_raw($name);
46         if($index) {
47                 $GLOBALS['_dwt_keys'][$index] = $name;
48                 $GLOBALS['_dwt_values'][$index] = $value;
49         } else {
50                 $GLOBALS['_dwt_keys'][] = $name;
51                 $GLOBALS['_dwt_values'][] = $value;
52         }
53 }
54
55 function dwt_set($name, $value) {
56         dwt_set_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
57 }
58
59 # returns index into arrays
60 function dwt_find_raw($name) {
61         for($i = 0; $i < count($GLOBALS['_dwt_keys']); ++$i) {
62                 if($GLOBALS['_dwt_keys'][$i] == $name) {
63                         return $i;
64                 }
65         }
66         return null;
67 }
68
69 # returns index into arrays
70 function dwt_find($name) {
71         return dwt_find_raw("<!-- TemplateBeginEditable name=\"$name\" -->");
72 }
73
74 function dwt_append_raw($name, $value) {
75         $index = dwt_find_raw($name);
76         if($index !== null) {
77                 $GLOBALS['_dwt_values'][$index] .= $value;
78         } else {
79                 dwt_set_raw($name, $value);
80         }
81 }
82
83 function dwt_append($name, $value) {
84         dwt_append_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
85 }
86
87 function dwt_prepend_raw($name, $value) {
88         $index = dwt_find_raw($name);
89         if($index !== null) {
90                 $GLOBALS['_dwt_values'][$index] = $value . $GLOBALS['_dwt_values'][$index];
91         } else {
92                 dwt_set_raw($name, $value);
93         }
94 }
95
96 function dwt_prepend($name, $value) {
97         dwt_prepend_raw("<!-- TemplateBeginEditable name=\"$name\" -->", $value);
98 }
99
100 function dwt_get_raw($name) {
101         $index = dwt_find_raw($name);
102         if($index !== null) {
103                 return $GLOBALS['_dwt_values'][$index];
104         } else {
105                 return false;
106         }
107 }
108
109 function dwt_get($name) {
110         return dwt_get_raw("<!-- TemplateBeginEditable name=\"$name\" -->");
111 }
112
113 function dwt_output($filename = null) {
114         if($filename !== null) {
115                 dwt_load($filename);
116         }
117         print(str_replace($GLOBALS['_dwt_keys'], $GLOBALS['_dwt_values'], $GLOBALS['_dwt_template']));
118 }
119
120 ?>